adding of php rows on rows on excel

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

GlendonPhilipp
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Feb 26, 2014 5:11 am

adding of php rows on rows on excel

Postby GlendonPhilipp » Thu Aug 07, 2014 9:18 pm

Code: Select all

<?PHP
  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.
include("dbconnect.php");
error_reporting(0);
session_start();
// Include PEAR::Spreadsheet_Excel_Writer


 

// Add a worksheet to the file, returning an object to add data to


$res = mysql_query("select * from services");
      $roww = mysql_num_rows($res);
      for ( $i=0;$i<$roww;$i++ )
{
   $array =  array (array ("size" =>  mysql_result($res,$i,"serviceid"), "color" => "gold")); 
}



function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  // file name for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($array as $row) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\n";
      $flag = true;
    }
    array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\n";
  }

  exit;
 
?>
but this executes only 1 data on my mysql rows,please help me

the output on excelplease see only 1 data is printed. please help me [img]
Attachments
untitled.JPG
output on excel format please see i need help on this
untitled.JPG (117.7 KiB) Viewed 94 times

GlendonPhilipp
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Feb 26, 2014 5:11 am

Re: adding of php rows on rows on excel

Postby GlendonPhilipp » Thu Aug 07, 2014 9:20 pm

i want all the serviceid data on mysql rows printed all below the column on size, anyways this are all test i change it after i got it properly this one

landi
php-forum Active User
php-forum Active User
Posts: 303
Joined: Thu Mar 15, 2012 3:59 pm

Re: adding of php rows on rows on excel

Postby landi » Fri Aug 08, 2014 12:30 am

Hi,
A few suggestions:
1) Before you try and add the data to excel, why not just output the values to the browser in a loop? This will reduce your code size that we have to wade through. Once this works, adding it to excel is a second problem.

2) If you want to use the old functions (see #3) and you're reading more and a single piece of data, you might be better off with mysql_fetch_assoc and such like. The php.net manual for this function contains an example of a loop to read multiple rows: (modified to suit yr context)

Code: Select all

   while($row=mysql_fetch_assoc($res)) {
       var_dump($row);
   }


3) From your code I suspect you are new at this (in which case welcome to php). Or using old code - in which case, you may want to read the warnings about using mysql_result instead of newer functions of mysqli and PDO_MySQL

Hope that helps

-A


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests