5 Replies - 2277 Views - Last Post: 13 April 2011 - 11:05 AM

#1 pduran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-October 10

Question/Problem: Populating html table with mysql query results colum

Posted 13 April 2011 - 09:44 AM

I'm trying to populate a html table with mysql query results, I can get the results to come up no problem, however I would like them to populate the table in a horizontal method first then vertical (e.g. fill up all 4 columns first then drop down to next row).

This code gives me straight vertical result
$query = "SELECT * FROM `Stores` WHERE Category = 'retail' "; 
$result = mysql_query($query) or die(mysql_error()); 
?>
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>Stores</EM></caption>

<?php
	while($row = mysql_fetch_assoc($result)) {
	echo"<td>";
	echo  "<br/><img src=\"" . $row["Img2"] . "\" border=0 alt=\"
	" . $row["Name"] . "\" height=128px width=128px></a>" ;
	echo '<br/><br/> Store: '.$row['Name'];
	echo '<br/><br/> Deal: '.$row['Deal'];
	echo"</td></tr>";
	
	}
	
?>



So my question would be is there a way to set it to fill horizontally then vertically? (4 to 5 cells wide) Thanks for the help guys

Is This A Good Question/Topic? 0
  • +

Replies To: Question/Problem: Populating html table with mysql query results colum

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,215
  • Joined: 07-September 06

Re: Question/Problem: Populating html table with mysql query results colum

Posted 13 April 2011 - 10:13 AM

It sure is possible, you just need to put the correct HTML in the correct spots.

Try this:
$query = "SELECT * FROM `Stores` WHERE Category = 'retail' "; 
$result = mysql_query($query) or die(mysql_error()); 
?>
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>Stores</EM></caption>

<?php
	while($row = mysql_fetch_assoc($result)) {
	echo"<tr><td>";
	echo  "<img src=\"" . $row["Img2"] . "\" border=0 alt=\"
	" . $row["Name"] . "\" height=128px width=128px></td>" ;
	echo '<td>Store: '.$row['Name'];
	echo '</td><td>Deal: '.$row['Deal'];
	echo"</td></tr>";
	
	}
	
?>




That should create a table of 4 columns and n rows (1 for each row returned from the query).

One thing I noticed is that you have tags which are useless in the code provided above (such as the closing link tag without any opening). I would suggest making sure you don't do that as it will really confuse a web browser when it tries to render your pages (I removed them in the code I provided above). Additionally you should make sure to close all tags you open (there are some tags which are self-closing but in general anything with content in it isn't), like your table.
Was This Post Helpful? 0
  • +
  • -

#3 pduran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-October 10

Re: Question/Problem: Populating html table with mysql query results colum

Posted 13 April 2011 - 10:38 AM

I appreciate the help but I can create that, I'm looking for something where all the information is contained (img, store, deal) are all contained in one cell and depending upon the number of results more cell are added (again adding horizontally to a total of 4 then continuing on the next row). When I used that code it just gave me the img in the first cell, store in the second, and deal in the third and the started on the next line with the next result.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,215
  • Joined: 07-September 06

Re: Question/Problem: Populating html table with mysql query results colum

Posted 13 April 2011 - 10:55 AM

Ah, sorry I misunderstood what you were attempting.

Yes, that is still possible, it just takes a slightly different algorithm:

$query = "SELECT * FROM `Stores` WHERE Category = 'retail' "; 
$result = mysql_query($query) or die(mysql_error()); 
?>
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>Stores</EM></caption>

<?php
	$tableCount = 0;
	$tableRowOpen = false;
	while($row = mysql_fetch_assoc($result)) {
		if($tableCount == 0){
			echo "<tr>";
			$tableRowOpen = true;
		}
		echo"<td>";
		echo  "<img src=\"" . $row["Img2"] . "\" border=0 alt=\"
		" . $row["Name"] . "\" height=128px width=128px>" ;
		echo '<br/><br/> Store: '.$row['Name'];
		echo '<br/><br/> Deal: '.$row['Deal'];
		echo"</td>";
		if($tableCount + 1 == 4){
			$tableCount = 0;
			echo "</tr>";
			$tableRowOpen = false;
		}
		else{
			$tableCount++;
		}
	
	}
	if($tableRowOpen){
		echo "</tr>";
	}
?>




Here we just need to keep a count of how many table cells we have filled in (0 through 4 because 4 is the max number of columns we want). If the table cell we are on is 0 we need to start a new table row, this also means that we have a table row open. If the cell + 1 is 4 then we need to close the row.

The boolean is used for when we have only 3 elements in a row (or something less than the maximum) because the loop will finish before the row has been closed, at which point we have to close it manually outside the loop.

Hopefully that makes sense
Was This Post Helpful? 2
  • +
  • -

#5 pduran  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-October 10

Re: Question/Problem: Populating html table with mysql query results colum

Posted 13 April 2011 - 11:02 AM

Your the man, I've been pulling my hair out and wasting time trying to figure this out. Is there a way to give you kudos points or something like that on the site or some sort of thank you (obviously I'm new to the site)?
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,215
  • Joined: 07-September 06

Re: Question/Problem: Populating html table with mysql query results colum

Posted 13 April 2011 - 11:05 AM

Nope, if you want you can hand out rep, that is done by the little + and - buttons at the bottom of each post (that aren't made by you and you didn't already vote on).

I'm just glad I could help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1