Page 1 of 1

loops again

Posted: Sat Jan 20, 2007 4:46 am
by garry27
hi, i'm trying to build a table dyanmically by adding stuff from a database but the code isn't returning anything. Please can someoone take a look to see where i'm going wrong. garry

Code: Select all

/*************** query db for pub info for pubs in list ****************************/
  db_connect();
  $sql = mysql_query("SELECT Pubs.pubID, Pubs.pubName, Pubs.pubPostcode from Pubs 
	                  WHERE $sql_str ") 
				        or die ('query2 invalid on table_data.php: ' . mysql_error() );
  $row = mysql_fetch_assoc($sql);
  $numrows = mysql_num_rows($sql);
  
  
  
   
  
  /******************************** build html table rows for each pub in list***********************************/
  $i=0;
  $table_rows = array();
  while($row)
  {
   $i++;
   extract($row);
     $cellA = "<a href='#' img class='list-btn'><img src='images/up_arr.jpg' width='13' height='12'> 
                   <a href='#' img class='list-btn' ><img src='images/down_arr.jpg' width='13' height='12'> </a></td>";	
     $cellB = $pubName . ', ' . $pubPostcode;
     $cellC = "<a href='#'img class='list-btn' ><img src='images/cross.jpg' width='13' height='12'></a>";
 
     $table_rows[] = 
                   "<tr id='tr1' class='tr1'>
                      <td class='td1'>
	         $cellA  
	      </td>    
                      <td class='td1'>  
	         $cellB      
	      </td>
                      <td class='td1'>
	         $cellC
	      </td> 
                    </tr>";  
  }

  /*************** create tbody tags and add array of rows  **************************************/ 
  $table_rows = implode(" ", $table_rows );						
  $table =   '<tbody>'
			    . $table_rows .
             '</tbody>';

Posted: Sat Jan 20, 2007 5:29 am
by Mordred
At a first glance:

Code: Select all

while($row)
should be

Code: Select all

while ($row = mysql_fetch_assoc($sql))
Also remove the first call to fetch_assoc.

There might be other issues, see what's inside var_dump($row);

Posted: Sat Jan 20, 2007 5:41 am
by garry27
that's brilliant, thanks mate.

do you know why it wan't working before when i declared the assoc erray before the loop?

Posted: Sat Jan 20, 2007 8:42 am
by Mordred
garry27 wrote:do you know why it wan't working before when i declared the assoc erray before the loop?
Yes, I do :twisted:

Read the documentation carefully, you'll see why as well. Btw our old code should have displayed at least one row, that's why I adviced you to check what's in $row.