Lazada Indonesia

Showing posts with label Tutorial PHP. Show all posts
Showing posts with label Tutorial PHP. Show all posts

Thursday, June 12, 2014

Cara Export Data Mysql ke Excel (.csv) dengan PHP

<?php
include('koneksi.php');

//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

//select table to export the data
$select_table=mysql_query('select * from nama_tabel');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';


// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\\r|\\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';
}

//make new row and line
echo "\r\n";
}
?>

Wednesday, May 7, 2014

Membuat Segitiga di PHP

1. Segitiga
 













<?php 
for($i = 1; $i <= 5; $i++) { for($j = 5; $j >= $i; $j--) { echo "* "; } echo "<br />"; } ?>


<?php for($i = 1; $i <= 5; $i++) { for($j = 1; $j <= $i; $j++) { echo "* "; } echo "<br />"; } ?>

2. Belah ketupat

*
**
***
****
***
**
*
cara membuat seperti bentuk di atas cukup mudah hanya dengan membutuhkan 16 baris tulisan saja.
berikut codingnya
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
  echo"<table align='center'><tr><td align='center'>";
    for($i=0;$i<=3;$i++){
       for($a=$i;$a>=0;$a--){
         print('*');
       }
    echo"<br/>";
   }
  for($i=0;$i<=2;$i++){
    for($a=$i;$a<=2;$a++){
      print('*');
    }
  echo"<br/>";
  }
 echo"</td></tr></table>";
?>