Forums

Dynamically load db contents

TilemahosG 02 Aug, 2013
Hello.

I'm trying to load some db contents that are stored in many different tables. I wrote the code below but it seems to have a problem that I can't find. Could any one help please?


The code I wrote is the following

<?php

echo '<table border="1"> <tr> <th>Όνομα έργου 1</th> <th>Όνομα έργου 2</th> <th>Όνομα έργου 3</th> <th>Όνομα συνθέτη</th> <th>Όνομα pdf αρχείου</th> </tr>';

$like_name = $form->data['composer_name'];
$db1 = &JFactory::getDBO();  
$query1 = "SELECT composer_id FROM compositer_names WHERE (full_name LIKE %'$like_name'%)";
$db1->setQuery($query1);
$composer_id = $db1->loadAssocList();

foreach ($composer_id as $c_id)
{
   $db2= &JFactory::getDBO();
   $query2 = "SELECT pdf_id FROM pdf_writtenby WHERE (composer_id = '$c_id')";
   $db2->setQuery($query2);
   $pdf_id = $db2->loadAssocList();

   foreach ($pdf_id as $p_id)
   {
      $db3= &JFactory::getDBO();
      $query3 = "SELECT composition_id FROM pdf_hasname WHERE (pdf_id = '$p_id')";
      $db3->setQuery($query3);
      $num_of_comps = $db3->getNumRows();
      $composition_id = $db3->loadAssocList();
      
      echo "<tr>";

      foreach ($composition_id as $comp_id)
      {
         $db4= &JFactory::getDBO();
         $query4 = "SELECT composition_name FROM composition_names WHERE (composition_id = '$comp_id')";
         $db4->setQuery($query4);
         $composition_name = $db4->loadResult();
         echo "<td>'$composition_name'</td>";
      }
      if($num_of_comps == 1)
      {
         echo "<td> </td><td> </td>";
      }
      else if($num_of_comps == 2)
      {
         echo "<td> </td>";
      }
      
      $db5= &JFactory::getDBO();
      $query5 = "SELECT composer_name FROM composer_names WHERE (composer_id = '$c_id')";
      $db5->setQuery($query5);
      $composer_name = $db5->loadResult();
      echo "<td>'$composer_name'</td>";

      $db6= &JFactory::getDBO();
      $query6 = "SELECT pdf_name FROM composition_pdf WHERE (pdf_id = '$p_id')";
      $db6->setQuery($query6);
      $pdf_name = $db6->loadResult();
      echo "<td>'$pdf_name'</td>";

      echo "</tr>";
      
   }
}


echo "</table>";
?>


The error message I get is the following:
'; $like_name = $form->data['composer_name']; $db1 = &JFactory::getDBO(); $query1 = "SELECT composer_id FROM compositer_names WHERE (full_name LIKE %'$like_name'%)"; $db1->setQuery($query1); $composer_id = $db1->loadObjectList(); foreach ($composer_id as $c_id) { $db2= &JFactory::getDBO(); $query2 = "SELECT pdf_id FROM pdf_writtenby WHERE (composer_id = '$c_id')"; $db2->setQuery($query2); $pdf_id = $db2->loadAssocList(); foreach ($pdf_id as $p_id) { $db3= &JFactory::getDBO(); $query3 = "SELECT composition_id FROM pdf_hasname WHERE (pdf_id = '$p_id')"; $db3->setQuery($query3); $num_of_comps = $db3->getNumRows(); $composition_id = $db3->loadAssocList(); echo ""; foreach ($composition_id as $comp_id) { $db4= &JFactory::getDBO(); $query4 = "SELECT composition_name FROM composition_names WHERE (composition_id = '$comp_id')"; $db4->setQuery($query4); $composition_name = $db4->loadResult(); echo ""; } if($num_of_comps == 1) { echo ""; } else if($num_of_comps == 2) { echo ""; } $db5= &JFactory::getDBO(); $query5 = "SELECT composer_name FROM composer_names WHERE (composer_id = '$c_id')"; $db5->setQuery($query5); $composer_name = $db5->loadResult(); echo ""; $db6= &JFactory::getDBO(); $query6 = "SELECT pdf_name FROM composition_pdf WHERE (pdf_id = '$p_id')"; $db6->setQuery($query6); $pdf_name = $db6->loadResult(); echo ""; echo ""; } } echo "

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\gca\administrator\components\com_chronoforms\form_actions\custom_code\custom_code.php(19) : eval()'d code on line 11

Call Stack
# Time Memory Function Location
1 0.0018 383480 {main}( ) ..\index.php:0
2 0.1889 7258624 JSite->dispatch( ) ..\index.php:42
3 0.2030 7622248 JComponentHelper::renderComponent( ) ..\application.php:197
4 0.2103 7635016 JComponentHelper::executeComponent( ) ..\helper.php:351
5 0.2115 7712520 require_once( 'C:\wamp\www\gca\components\com_chronoforms\chronoforms.php' ) ..\helper.php:383
6 0.2211 8060920 process( ) ..\chronoforms.php:95
7 0.2211 8060920 CFChronoForm->process( ) ..\chronoforms.php:106
8 0.2212 8062464 CFChronoForm->_processEvents( ) ..\chronoform.php:237
9 0.2212 8062784 CFChronoForm->_processAction( ) ..\chronoform.php:262
10 0.2213 8062952 CFChronoForm->runAction( ) ..\chronoform.php:287
11 0.2261 8121704 CfactionCustomCode->run( ) ..\chronoform.php:312
12 0.2269 8147840 eval( '?>
Όνομα έργου 1 Όνομα έργου 2 Όνομα έργου 3 Όνομα συνθέτη Όνομα pdf αρχείου
'$composition_name' '$composer_name' '$pdf_name'
"; ?>' )..\custom_code.php:19

Thanks in advance!
GreyHead 03 Aug, 2013
Hi TilemahosG,

I think you have the quotes mis-placed here, the % needs to be inside the ''
$query1 = "SELECT composer_id FROM compositer_names WHERE (full_name LIKE %'$like_name'%)";
Please try:
$query1 = "SELECT `composer_id` FROM `compositer_names` WHERE `full_name` LIKE '%{$like_name}%' ;";

Bob
TilemahosG 03 Aug, 2013
First of all, thanks for the reply.

I tried your suggestion, but still nothing. I get the same message. I think it has something to do with the $db1->loadAssocList() and foreach() statements on line 9 and 11 accordingly.

Any suggestions?
GreyHead 03 Aug, 2013
Hi TilemahosG,

My best guess is that the foreach error is because the query is failing and there is no data.

This is where you get to debug your code :-(

Bob
TilemahosG 03 Aug, 2013
OK. Your guess was sooooo right...

I accidentally tried to load composer_id field of the table. There was no composer_id field. Instead the name of the field in the table (in db) was name_id. That's why it was not loading anything and was getting the error message. I fixed it and now it is loading things.

Still though I get another error message. It can't load number of rows in the
$num_of_comps = $db3->getNumRows();
.

Any idea why? As I have read in forums for php and joomla docs I put this line before $db3->loadAssocList(); in order to work.

Thanks again.
TilemahosG 03 Aug, 2013
Solved it.

I have forgotten to put the code
$db->query();
. This is why it didn't return me the number of rows.

As for the output I still get an empty table and I don't know why. Maybe it's the echo code...

Thanks anyway.

P.S. if anyone could help me show the elements of the table (with the echo instruction) I'd be very glad!!!
GreyHead 03 Aug, 2013
Hi TilemahosG,

You may don't need the number of rows; if you get some data you can use count() on the results array,

There's nothing very special about the display. Assuming that you got your results into $results using loadAssocList():
<table>
  <thead>
    <tr>
      <td>Col 1</td>
      <td>Col 1</td>
      <td>Col 1</td>
   </tr>
  </thead>
  <tbody>
<?php
foreach ( $results as $r ) {
  echo "
<tr>
      <td>{$r['col_1']}</td>
      <td>{$r['col_2']}</td>
      <td>{$r['col_3']}</td>
</tr>
";
}
?>
  </tbody>
</table>

Bob
This topic is locked and no more replies can be posted.