How To Show Data From Database Using PHP CodeIgniter + AngularJS

Hi.. this time I created simple web to show some data from database using PHP CodeIgniter and angularJS. Here is the screenshot of the result page:

page-result

I use MySQL as my db. Here is the table that I’ll use for sample:

books-tbl

First of all, I added some angular javascript files to my js folder. Since I use bootstrap for my csss,I added some bootstrap css too to my css folder. Now we ready for codes 🙂

Model
For books_model, I created function to get all books in getAllBooks() function. Continue reading “How To Show Data From Database Using PHP CodeIgniter + AngularJS”

Advertisements

How To Dynamically Set Value On Cells Using PHPExcel

I am quite new coding using PHP. But I’ll try to write simple snap code about how to set value of cell dynamically. You may need it when you have to show some data from query result or any array to XLS / XLSX format. But on this article, I decided using xls format instead.

First of all, put PHPExcel lib to your CI project. You can follow this example. If you are done, then you ready to do the next step.

<?

#.. some code

function doPrintXLSReport($reportDate){
	//load PHPExcel Library
	$this->load->library('Excel');
 	$objPHPExcel = new PHPExcel();
		 
	// Set document properties
	$objPHPExcel->getProperties()->setCreator("Ariestania")
					 ->setLastModifiedBy("Ariestania")
					 ->setTitle("Sample Report")
					 ->setSubject("Sample Report")
					 ->setDescription("Sample Report")
					 ->setKeywords("Sample Report")
					 ->setCategory("Sample Report");
		 		 
	//set active sheet and the title. 0 means first sheet that I will use
	$objPHPExcel->setActiveSheetIndex(0);
	$objPHPExcel->getActiveSheet()->setTitle('Sample Report');

	//create style of the cell
	$styleThinBlackBorderOutline = array(
		    'borders' => array(
		        'outline' => array(
		            'style' => PHPExcel_Style_Border::BORDER_THIN,
		            'color' => array('argb' => 'FF000000'),
		        ),
		    ),
		);
	
	//get some data from DB 
	$detail = $this->dbhandler->getTrxDatas($reportDate);

	//set A1 as the first cell
	$current_col = 0;
	$current_row = 1;

	if($detail){
		foreach($detail as $det){
			//set value of cell
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($current_col, $current_row, $det['TRX_DATE']);
			//set cell style
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($current_col, $current_row)->applyFromArray($styleThinBlackBorderOutline);
			//move to next column
			$current_col++;
			//set value and style of the cell
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($current_col, $current_row, $det['TRX_DETAIL']);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($current_col, $current_row)->applyFromArray($styleThinBlackBorderOutline);

			//move to next row
			$current_row++;
			//reset column back to A
			$current_col = 0;
		}
	}

	//do the download process
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	ob_end_clean() ;
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="SampleReport.xls"');
	header('Cache-Control: max-age=0');		 
	$objWriter->save('php://output');
	exit;
}

#.. some code

?>

Hope this simple sample can help. Happy coding 🙂

Penerapan MVC yang Baik dalam Yii Framework

Model

  • Memiliki property sesuai dengan data yang diwakili serta code untuk validasinya.
  • Boleh berisi manipulation code namun tidak boleh memperumit dan memperbesar scala model.
  • Tidak boleh berisi logic yang berhubungan langsung dengan end user seperti penggunaan $_GET, $_POST, atau variabel sejenis yang berhubungan erat dengan request end-user.
  • Menghindari meng-embed HTML atau code presentasional lainnya.

View

  • Mengandung code presentasional, seperti HTML, dan code PHP sederhana untuk melintasi, memformat dan me-render data.
  • Menghindari code yang melakukan query DB secara langsung.
  • Menghindari akses langsung $_GET, $_POST, atau variabel sejenisnya yang mewakili request end user.
  • Boleh mengakses langsung properti dan method dari controller dan model. Namun, harus dilakukan hanya untuk tujuan presentasi.

Controller

  • Boleh mengakses $_GET, $_POST dan variabel PHP lain yang merepresentasikan user request.
  • Membuat berbagai instance model dan mengatur siklus hidupnya.
  • Menghindari menampung statement SQL.
  • Menghindari HTML apapun atau markup presentasional.

How To Generate Code on Yii Framework

First of all, you need to prepare:

  • Yii framework files. You can download from the site
  • Apache.
  • PHP (5.1 or above).

Now we moving forward. The steps are:

  1. Extract yii and put it on folder to your web-accessible root directory. Then check the feature on http://localhost/yii/requirements/ which will be supported with yii on your system. On my computer looks like this:Yii 1
    Yii 2
  2. If you want to using other database, you need set the libs so yii can support the feature. Continue reading “How To Generate Code on Yii Framework”