Jump to content

Sorting mysql results into tables


Recommended Posts

Guest D1proball
[code]<?
include("connect.php");
$query  = "SELECT * FROM accessories";
$result = mysql_query($query) or die("Error: " . mysql_error());


print"";

while($row = mysql_fetch_array($result)){
    print"
    <table width=\"31%\" height=\"88\" border=\"1\">
  <tr>
    <td width=\"6%\" rowspan=\"3\"><img src=\"/images/$row%5Bimage%5D.gif\" width=\"80\" height=\"80\"></td>
    <td width=\"94%\"><strong>Name</strong>: $row[name]</td>
  </tr>
  <tr>
    <td height=\"31\"><strong>Price</strong>: $row[price]</td>
  </tr>
  <tr>
    <td height=\"23\"><strong>Origin</strong>: $row[origin]</td>
  </tr>
</table>";



}
?>[/code]
OK, probably the easiest way to explain this is to show you the html version:

[code]
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  <tr>
    <td width="33%">first 1</td>
    <td width="33%">second1</td>
    <td width="34%">third1</td>
  </tr>
  <tr>
    <td width="33%">first2</td>
    <td width="33%">second2</td>
    <td width="34%">thirdd2</td>
  </tr>
</table>[/code]

To do the same thing in php with some values pulled from mysql do this:

[code]
$result = mysql_query("SELECT first, second, third FROM tablename");
$row = mysql_fetch_row($result);
<?
echo "<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">";
while($row=mysql_fetch_row($result))
{
echo "<tr>"; //each tr tag is going to create a new row
echo "<td>".$row[0]."</td>"; //each td tag is going to create a new column
echo "<td>".$row[1]."</td>"; //the row[#] is going to pull in the value from that part of the array
echo "<td>".$row[2]."</td>";
echo "</tr>";//this will close the row off
}
echo "</table>";
?>
[/code]

So as long as there are values in the query it will continue to create a new row with three columns. Obviously you can add an additional "echo "<td>".$row[#]."</td>";" if you needed to add another coolumn.

Hope that helps!
Guest D1proball
This is what I'm doing now and I'm still getting the same results. You can check out my site here. [a href=\"http://www.acadvocates.com/itemsearch.php\" target=\"_blank\"]http://www.acadvocates.com/itemsearch.php[/a]

[code]print"<table align=center cellspacing=3 cellpadding=3 border=0>";

while($row = mysql_fetch_array($result)){
    print"

     <tr>
    <td width=\"94%\"><strong>Name</strong>: $row[name]</td>
  </tr>
  <tr>
    <td height=\"31\"><strong>Price</strong>: $row[price]</td>
  </tr>
  <tr>
    <td height=\"23\"><strong>Obtained from</strong>: $row[origin]</td>
  </tr>";




}
print"</table>";[/code]
[!--quoteo(post=359145:date=Mar 27 2006, 11:31 PM:name=D1proball)--][div class=\'quotetop\']QUOTE(D1proball @ Mar 27 2006, 11:31 PM) [snapback]359145[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This is what I'm doing now and I'm still getting the same results. You can check out my site here. [a href=\"http://www.acadvocates.com/itemsearch.php\" target=\"_blank\"]http://www.acadvocates.com/itemsearch.php[/a]

[code]print"<table align=center cellspacing=3 cellpadding=3 border=0>";

while($row = mysql_fetch_array($result)){
    print"

     <tr>
    <td width=\"94%\"><strong>Name</strong>: $row[name]</td>
  </tr>
  <tr>
    <td height=\"31\"><strong>Price</strong>: $row[price]</td>
  </tr>
  <tr>
    <td height=\"23\"><strong>Obtained from</strong>: $row[origin]</td>
  </tr>";
}
print"</table>";[/code]
[/quote]

I think you have <tr> tags where you dont want them. Try this:
[code]print"<table align=center cellspacing=3 cellpadding=3 border=0>";

while($row = mysql_fetch_array($result)){
    print"

     <tr>
    <td width=\"94%\"><strong>Name</strong>: $row[name]</td>
    <td height=\"31\"><strong>Price</strong>: $row[price]</td>
    <td height=\"23\"><strong>Obtained from</strong>: $row[origin]</td>
  </tr>";
}
print"</table>";[/code]


Is that the output your were looking for? I'm working on a similar output right now, and being new to php I found the code will work perfectly for what I need to do, except that I will be using a CSS layout. Never really liked table layouts. Good luck.
[!--quoteo(post=359152:date=Mar 28 2006, 12:01 AM:name=D1proball)--][div class=\'quotetop\']QUOTE(D1proball @ Mar 28 2006, 12:01 AM) [snapback]359152[/snapback][/div][div class=\'quotemain\'][!--quotec--]
now it changed it to this [a href=\"http://www.acadvocates.com/itemsearch.php\" target=\"_blank\"]http://www.acadvocates.com/itemsearch.php[/a]
[/quote]


You may need to adjust the width% you have on the first column. It says it should take up 94% of the screen, which is why everything else is slammed over to the right like that. That would also cause the wrapping of some of the text also.

[!--quoteo(post=359161:date=Mar 28 2006, 12:20 AM:name=D1proball)--][div class=\'quotetop\']QUOTE(D1proball @ Mar 28 2006, 12:20 AM) [snapback]359161[/snapback][/div][div class=\'quotemain\'][!--quotec--]
still isnt working this simple task has become annoying lol
[/quote]


I looked at the html source for the page after as it is and it's missing the <table> tags. That is why all the data is just flooded across the screen now.

Actually, I can see that everything except the <tr> and <td> tags are missing. There's no <html>, <body>, etc...tags.

I manually inserted the table tags and the standard html tags and tried it with the source and that appears to work fine.

Only other thing I see is that you have a comma appearing after each numeric value.

Without seeing the php code I can't tell why the table tags are not in there or why the comma is appearing.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.