loops again

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

loops again

Post 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>';
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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);
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post 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?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
Post Reply