Jump to content


Sorting mysql results into tables


  • Please log in to reply
13 replies to this topic

#1 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 27 March 2006 - 02:15 AM

I need to sort my results so that It shows 3 results per row. I can Only get it to make one huge line down my page with 1 result per row.

What I want

Result 1 | Result 2 | Result 3
Result 4 | Result 5 | Result 6

What I get

Result 1
Result 2
Result 3
Result 4
Result 5
Result 6

#2 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 27 March 2006 - 03:33 AM

Can I see your code?
Given enough eyeballs all bugs are shallow. My Website

#3 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 27 March 2006 - 04:12 AM

<?
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>";



}
?>


#4 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 27 March 2006 - 05:44 PM

can anyone help?

#5 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 27 March 2006 - 09:27 PM

I have done this before, but at work now.

Will respond later after finding a snippet that shows what you are looking for.

#6 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 28 March 2006 - 04:00 AM

I thought for sure somebody could help by now :(

#7 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 28 March 2006 - 04:03 AM

OK, probably the easiest way to explain this is to show you the html version:

<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>

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

$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>";
?>

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!

#8 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 28 March 2006 - 04:31 AM

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]

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>";


#9 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 28 March 2006 - 04:57 AM

[!--quoteo(post=359145:date=Mar 27 2006, 11:31 PM:name=D1proball)--][div class=\'quotetop\']QUOTE(D1proball @ Mar 27 2006, 11:31 PM) View Post[/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]

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>";
[/quote]

I think you have <tr> tags where you dont want them. Try this:
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>";




#10 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 28 March 2006 - 05:01 AM

now it changed it to this [a href=\"http://www.acadvocates.com/itemsearch.php\" target=\"_blank\"]http://www.acadvocates.com/itemsearch.php[/a]

#11 sKunKbad

sKunKbad
  • Members
  • PipPipPip
  • Advanced Member
  • 1,832 posts
  • LocationTemecula, CA, USA

Posted 28 March 2006 - 05:02 AM

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.
Brian's Web Design - Temecula

Freedom is only available through death.

#12 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 28 March 2006 - 05:07 AM

[!--quoteo(post=359152:date=Mar 28 2006, 12:01 AM:name=D1proball)--][div class=\'quotetop\']QUOTE(D1proball @ Mar 28 2006, 12:01 AM) View Post[/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.



#13 Guest_D1proball_*

Guest_D1proball_*
  • Guests

Posted 28 March 2006 - 05:20 AM

still isnt working this simple task has become annoying lol

#14 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 28 March 2006 - 02:04 PM

[!--quoteo(post=359161:date=Mar 28 2006, 12:20 AM:name=D1proball)--][div class=\'quotetop\']QUOTE(D1proball @ Mar 28 2006, 12:20 AM) View Post[/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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users