Create Dynamic table from query and export result to csv

Discussion in 'PHP' started by tubalcain, Aug 21, 2008.

  1. #1
    I don't know where to put this so moderate away!
    The code snippet will create a dynamically sized table displaying the results of an mysql query from the standard mysql database from the table called help_keyword loaded with xampp it will also export the data to a csv file called "test.csv" I know very original.
    I have only been doing PHP for a month or so forgive the standard of programming.
    You will need , obviously, to insert your username and password into the Connect to database section replace "your username " and "your password" with .....well your username and your password.
    If it doesn't work it's your fault not mine don't blame me it works for me no liabilty etc....
    
    <?php
     echo"<table border=1 cellspacing=0 cellpadding=2 style=background:#DDFF66>";
     echo "<th colspan=4 >Search Criteria</th>";
     echo "<tr>";
    echo "<th>User Name</th><th>Week Number</th><th>Project</th><th>Type</th><TR>";
    echo" <TD align=middle>Criteria 1</TD><TD align=middle>Criteria 2</TD><TD align=middle>Criteria 3</TD><TD align=middle>Criteria 4</TD><tr>";
    /////////////////////////////////////////////////////////////////////////////
    //require ('dbconnect.php');normally in another file
    $db = new mysqli ('localhost','your username','your password','mysql');
    
    if (mysqli_connect_errno())
    {
      $errcode=mysqli_connect_errno();
      
    	echo 'Error: could not connect to database. Please try again later.';
    	exit;
    	}
    else
    {
    $errcode=mysqli_connect_errno();
      //	echo 'Error No. = '.$errcode  ;
    	}
    //////////////////////////////////////////////////////////////////////////	
    //require ('dbsqlquery.php');normally in another file
    $sql="SELECT * FROM `help_keyword`";
    ////////////////////////////////////////////////////////////////////////
    echo $sql;
    $result=$db->query($sql);
    $headerdone=false;
    $num_results = $result->num_rows;
    echo "<td colspan=4 >No. of records found =$num_results</td>";
     echo "</TR>";
     echo " </table>";
    echo"<table width=1024 border=1 cellspacing=0 cellpadding=0 style=background:#DDFF66>" ;
    for($j=0; $j<$num_results;$j++) {
    $i=0;
    $colname =$result->fetch_assoc();//get column names
    foreach ($colname as $fieldname => $fieldvalue) {//count columns from here
    if($headerdone==false){//build headers in bold
    $UCfieldname=ucfirst($fieldname);
    echo "<TH>".$UCfieldname."</th>";
    $list = ($list.$UCfieldname.",");//build string 
    } 
    if ($fieldname <>"")//add column names to array
    {
    $colvalues[$i]=ucfirst($fieldvalue);//Caps on first letter
    if ($fieldvalue=="") {
    $colvalues[$i]=0;
    }
    $i=$i+1;
    $numcols=$i;//count columns
    }
    }//Headers complete
    $list=$list." \n";//add linefeed
    $headerdone=true;
    echo "<TR>";
    for ($addcols=0;$addcols<$numcols;$addcols++) {
    echo "<td  align = middle>".$colvalues[$addcols]."</td>";
    $list = ($list.$colvalues[$addcols].",");
    }
    }//end of query results
    $file = fopen("test.csv","w");
    //send final constructed string to csv file
    fputs($file,$list);
    fclose($file);
    echo "</table>";
    echo"<TR>";
    	?> 
    <h2>
    <?php echo $message?></h2>
    </form><br />
    </body>
    </html>
    
    
    Code (markup):
    Seriously I have just tested it and the only change before posting was the username etc.. :) I hope it works for anyone who needs it.
     
    tubalcain, Aug 21, 2008 IP