PHP newbie here again looking for some more help. I am trying to create a html table using information queried from a database. I would like three columns and the number of rows would be dynamic based on information gathered during the query. Briefly, it would look like:
picture1 | picture2 | picture3
==================
lablel1 | label2 | label3
==================
picture4 | etc. etc.

What I am getting right now is:
picture1 | picture1 | picture1
==================
picture2 | picture2 | picture2

The loop is working, if only incorrectly; and I can't figure out how to apply the corresponding labels on the next row. Here's my code:

if (($result)||(mysql_errno == 0)) 
{ 
  echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"; 
  if (mysql_num_rows($result)>0) 
  {  
    //display the data 
    while ($rows = mysql_fetch_array($result,MYSQL_ASSOC)) 
    { 
	   echo "<tr>";
       //loop thru the serials to create three columns
	   $i = 0;
	   while ($i < '3')
	   {
      	echo "<td><img src='/images/{$rows['FileDirectory']}/{$rows['Graphic']}.jpg' width='150' height='112' /></td>"; 
	  	$i++;
	   }
	   echo "</tr>";
    } 
  }else{ 
    echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>"; 
  } 
  echo "</table>"; 
}else{ 
  echo "Error in running query :". mysql_error(); 
}

Any help is appreciated. Thanks.

Recommended Answers

All 6 Replies

Hi.

How can you expect to see "label" if in your code there is no $row for example.
I mean you print only $rows and $rows extracted form DB as I can see.

Show us your query, pls.

Hi.

How can you expect to see "label" if in your code there is no $row for example.
I mean you print only $rows and $rows extracted form DB as I can see.

Show us your query, pls.

I wasn't expecting to see a row with the "labels" on them. I couldn't figure out how to make that happen. When everything I tried to get the pictures to appear properly failed, I stopped work on it. Thats why the labels are not in the code. Here is the query:

$series = $_GET['series'];

$query = "SELECT * FROM tblTitles WHERE FileDirectory = '$series'"; //Make the query
$result = @mysql_query ($query); // Run the query

There are only four rows in tblTitles: Label, Series, FileDirectory, and Graphic.

Thanks for looking at the problem.

And what format do you exactly want ?

picture1 | picture2 | picture3
==================
lablel1 | label2 | label3
==================


Three pictures in row next the three labels and next pictures 4-6 and labels 4-6 ?

And what format do you exactly want ?

picture1 | picture2 | picture3
==================
lablel1 | label2 | label3
==================


Three pictures in row next the three labels and next pictures 4-6 and labels 4-6 ?

Etc., etc. Yes. Since its dynamic, I have no idea how many rows there would be. Another thing I just thought of is left-over cells, i.e. if the number of pictures/labels is not divisible by 3 there will be a couple of left-over pictures that would need blank cells to complete the row.

This is something I could never quite get my head around, I resorted to adding 3 images in my database and printing them out that way. Would also be interested if anyone has an answer

change:

$i = 0;	   
while ($i < '3')

to:

$i = 0;	   
while ($i < 3)

That may help a little

regards Niek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.