PDA

View Full Version : listing table content by fields


2cx
07-29-2004, 06:07 AM
Hi, a friend of mine recommended these forums for possible help:

i've got a mysql db with a table called tshirts . Im trying to display all the shirts by category on one page in a table 2 colums wide, been told i need a do while loop ? ive looked up on php.net but didn't really make much sense to me.


while($row= mysql_fetch_array($rs) )
{
$listt .="<td width=25%><div align=center><b>".$row["tshirtname"]."</b><br>";
$listt .="<img src=".$row["mainpicture"]." >";
$listt .="<br>".$row["tshirtdesc"]."<br><b><font size=4>".$row["price"]."</font></b><br>";
$listt .="</td>";

the above code just displays all the shirts in a long horizontal line, thats as far as i can get, ive not got that much knowledge of php or mysql so sorry if this is a stupid question, thanks for any help in advance.

raf
07-29-2004, 10:53 AM
Welcome here!

this is a purely PHP question that should have posted in the php forum.

i don' understand how everything can be one long horizontal line since you have linebreaks on about every line.

if you need it in 2 columns, then you need something like


$cell='first';
$listt = '<table>
<tr><td colspan="2">Your title</td>';
while($row= mysql_fetch_assoc($rs) ){
if ($cell='first'){
$listt .= '</tr><tr>';
$cell = 'last';
}else{
$cell = 'first';
}
$listt .='<td width="25%"><div align="center"><b>'.$row["tshirtname"].'</b><br />';
$listt .='<img src="'.$row['mainpicture'].'" >';
$listt .='<br />'.$row['tshirtdesc'].'<br /><b><font size="4">'.$row['price'].'</font></b><br />';
$listt .='</td>';
}
if ($cell='first'){
$listt .= '</td></tr>';
}
$listt .= '</table>';


if you need a more dynamic version where you can easely modify the number of columns, then run a search in the php forum.

2cx
07-29-2004, 07:36 PM
Hi thanx for the quick reply , sorry wrong forum.


I tried what you gave me below, but now they are just in one big vertical line on a page, not 2 colums?
i search or 5 on php bored but cudnt find anything.?

raf
07-29-2004, 11:34 PM
My bad. To much copy pasting so i forgot a = in the condition. Try

$cell='first';
$listt = '<table>
<tr><td colspan="2">Your title</td>';
while($row= mysql_fetch_assoc($rs) ){
if ($cell=='first'){
$listt .= '</tr><tr>';
$cell = 'last';
}else{
$cell = 'first';
}
$listt .='<td width="25%"><div align="center"><b>'.$row["tshirtname"].'</b><br />';
$listt .='<img src="'.$row['mainpicture'].'" >';
$listt .='<br />'.$row['tshirtdesc'].'<br /><b><font size="4">'.$row['price'].'</font></b><br />';
$listt .='</td>';
}
if ($cell=='last'){
$listt .= '<td></td>';
}

$listt .= '</tr></table>';

2cx
07-30-2004, 08:15 AM
did the trick,
many thanks !

raf
07-30-2004, 01:52 PM
You're welcome!

Happy coding.