Pomoc - Szukaj - U¿ytkownicy - Kalendarz
Pe³na wersja: spreadsheet_writer problem
Forum PHP.pl > Forum > PHP
calebos
Czesc

Mam problem z drukowaniem nazw kolumn z mysql'a z tym modulem.

Zwraca mi blad :
Fatal error: Call to a member function columnCount() on a non-object in C:\wamp\www\spred2.php on line 116


  1. <?php
  2. $result=mysql_query('SELECT * from coredump1 limit 50');
  3. $rowCount=0;
  4. for ($x=0; $x<$result->columnCount(); $x++) {
  5. $meta = $result->getColumnMeta($x);
  6. $sheet->write($rowCount, $x, $meta['name']);
  7. ?>





Pozniej jak juz drukuje zawartosc tablicy to jest ok
  1. <?php
  2. while($item = mysql_fetch_array($result,MYSQL_ASSOC))
  3.  {
  4.  // Write each item to the sheet
  5.  
  6.  
  7.  
  8.  array_push($item,$variable);
  9.  if($item['T1'] > 0)
  10.  {
  11.  $cart->writeRow($currentRow,0,$item,$titleFormat);
  12.  
  13.  // Remember Excel starts counting rows from #1!
  14.  $excelRow = $currentRow + 1;
  15.  
  16.  // Create a PHP string containing the formula
  17.  $formula = '=PRODUCT(B' . $excelRow . ':C' . $excelRow .')';
  18.  
  19.  // Add the formula to the row
  20.  $cart->writeFormula($currentRow,60,$formula,$titleFormat);
  21.  }else{
  22.  $cart->writeRow($currentRow,0,$item,$selectFormat);
  23.  
  24.  // Remember Excel starts counting rows from #1!
  25.  $excelRow = $currentRow + 1;
  26.  
  27.  // Create a PHP string containing the formula
  28.  $formula = '=PRODUCT(B' . $excelRow . ':C' . $excelRow .')';
  29.  
  30.  // Add the formula to the row
  31.  $cart->writeFormula($currentRow,60,$formula,$selectFormat);
  32.  }
  33.  
  34.  $currentRow++;
  35.  
  36. }
  37. ?>
ayeo
Najpierw przypisujesz id zasobów (mysql_query) do zmiennej $result, a potem to result traktujesz jak obiekt: $result->countColumn();
Pozdrawiam!
calebos
Rozumiem ze skopalem cos 'obiektowo'.

Moglbys jeszcze minimalnie wskazac jak to zrobic aby w petli byla jednak tablica tak jak to mam w samej zawartosci tego excela?
Chyba ze jest jakis madry sposob aby uzyc tego co znalazlem i miec mozliwosc warunkowania wewnatrz tych petl ?
W tym co wpisalem wczesniej latwo udalo mi sie uzyskac kolorowanie wierszy po warunku.
Nie wiem jak to zrobic w tej wersji co znalazlem.
  1. Oryginal
  2. <?php
  3. // include class file
  4. include 'Spreadsheet/Excel/Writer.php';
  5.  
  6. // initialize reader object
  7. $excel = new Spreadsheet_Excel_Writer();
  8.  
  9. // send client headers
  10. $excel->send('country.xls');
  11.  
  12. // add worksheet
  13. $sheet =& $excel->addWorksheet('SQL_Output');
  14.  
  15. // attempt a connection
  16. try {
  17.  $pdo = new PDO('mysql:dbname=world;host=localhost', 'user', 'pass');
  18. } catch (PDOException $e) {
  19.  die("ERROR: Could not connect: " . $e->getMessage());
  20. }
  21.  
  22. // read data from database
  23. // convert into spreadsheet
  24. $rowCount = 0;
  25. $sql = "SELECT * FROM country";
  26. if ($result = $pdo->query($sql)) {
  27. // get header row
  28. for ($x=0; $x<$result->columnCount(); $x++) {
  29. $meta = $result->getColumnMeta($x);
  30. $sheet->write($rowCount, $x, $meta['name']);
  31. }
  32. // get data rows
  33. $rowCount++;
  34. while($row = $result->fetch()) {
  35. foreach ($row as $key => $value) {
  36. $sheet->write($rowCount, $key, $value);
  37. }
  38. $rowCount++;
  39. }
  40. } else {
  41. echo "ERROR: Could not execute $sql. " . print_r($pdo->errorInfo());
  42. }
  43.  
  44. // close connection
  45. unset($pdo);
  46.  
  47. // save file to disk
  48. if ($excel->close() === true) {
  49. echo 'Spreadsheet successfully saved!';
  50. } else {
  51. echo 'ERROR: Could not save spreadsheet.';
  52. }
  53. ?>


*bump*
To jest wersja lo-fi g³ównej zawarto¶ci. Aby zobaczyæ pe³n± wersjê z wiêksz± zawarto¶ci±, obrazkami i formatowaniem proszê kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.