Jump to content


Photo

Display a two column layout of mysql results


  • Please log in to reply
4 replies to this topic

#1 replaya

replaya

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 28 August 2005 - 01:08 PM

I have some product details in the mysql database. I trying to display them into 2 or 3 columns depending on the width of my page design.

At the moment it is displaying as a single columm.

I would like it display across the page

| Details of prod 1 | Details of prod 2 |
| Details of prod 3 | Details of prod 4 |
| Details of prod 5 | Details of prod 5 |

not

| Details of prod 1 |
| Details of prod 2 |
| Details of prod 3 |
| Details of prod 4 |

Heres the link to my page
<?php
//$id = $_REQUEST['product_id'];

$query = "SELECT * FROM Goldframes"; //a query of my products
$results = mysql_query($query)
or die(mysql_error());
?>
<table width="458" border="0" cellpadding="0" cellspacing="0" class="bg_color">
<tr>
<?php
//variables
$count = 0; 

while($s = mysql_fetch_array($results)){
$name = $s['product_name']; //gives each field a variable
$desc = $s['product_desc'];
$id = $s['product_id'];
$image_src = $s['Image_Path'];

//splitting the tables
if($count % 2 <> 0) {

extract($s);

echo  "<tr class=\"forgotext\">";
echo   "<td width=\"200\" class=\"forgotext\"><span class=\"forgotext\"></span>";
echo  $id;
echo "</td>";
echo "<td width=\"258\">"; 
echo $name;
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<img src=\"$image_src\"" .$id.">"; 
echo "</td>";
echo "<td valign=\"top\"><p class=\"newregistration\">";
echo $desc;
echo "</p>";
echo     "</td>";
echo   "</tr>";
echo   "<tr>";
echo     "<td>&nbsp;</td>";
 echo    "<td>&nbsp;</td>";
echo   "</tr>";
}else{

echo  "<tr class=\"forgotext\">";
echo   "<td width=\"200\" class=\"forgotext\"><span class=\"forgotext\"></span>";
echo  $id;
echo "</td>";
echo "<td width=\"258\">"; 
echo $name;
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<img src=\"$image_src\"" .$id.">"; 
echo "</td>";
echo "<td valign=\"top\"><p class=\"newregistration\">";
echo $desc;
echo "</p>";
echo     "</td>";
echo   "</tr>";
echo   "<tr>";
echo     "<td>&nbsp;</td>";
 echo    "<td>&nbsp;</td>";
echo   "</tr>";
}
$count++;

}

?>


</tr>
</table>

I would really appreaciate your thoughts . Thankyou in advance

#2 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 28 August 2005 - 02:16 PM

Ok well first it'll only do it in 2 columns unless you tell it to. So in your output, try this:

$split = 0;
echo '<tr>';
while($s = mysql_fetch_array($results)){
$name = $s['product_name']; //gives each field a variable
$desc = $s['product_desc'];
$id = $s['product_id'];
$image_src = $s['Image_Path'];
$split = $split + 1;

if ($split == 1){ 
echo '<td>'.$name.'<br>'.$desc.'</td>';
}
else if ($split == 2){
echo '<td>'.$name.'<br>'.$desc.'</td></tr>';
}else{
$split = 0;
echo '<tr><td>'.$name.'<br>'.$desc.'</td>';
}

So when the first row pops out it makes a cell, when the second it'll put it beside it, then when the thrid comes it'll make a new row.

Edited by Lang, 28 August 2005 - 02:19 PM.


#3 replaya

replaya

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 28 August 2005 - 04:47 PM

You are and absolute star. One day I hope to be as good as you.

My Webpage

I can't get the concept of <tr> <td> in php. In dreamweaver, it would layout it out like this
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr> In php it so different.

Right clever clogs. how would I do it so one table would show like this

$id | $name
$image_path | $description

and thanks agian

#4 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 28 August 2005 - 07:56 PM

Let me get things straight. You want a table, with 2 columns. Each cell is a different product.
Now, you want each product to display in a similar fasion? If that's the case, just add another table inside the cells
i.e.
<table>
  <tr>
    <td>
    <table>
      <tr>
        <td>
        $id
        </td>
        <td>
        $name
        </td>
      </tr>
      <tr>
        <td>
        $image_path
        </td>
        <td>
        $description
        </td>
      </tr>
    </table>
    </td>
  </tr>
</table>


#5 replaya

replaya

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 29 August 2005 - 04:44 AM

thanks for replying,

I tried implementing the code so that each cell contains a different product. That seems to be working, in seperating the product details.

But now it won't split it into two columns along the page My Webpage
[COLOR=green]<table width="450">
<?php
$split = 0;
echo '<tr><td>';
while($s = mysql_fetch_array($results)){
$name = $s['product_name']; //gives each field a variable
$desc = $s['product_desc'];
$id = $s['product_id'];
$image_src = $s['Image_Path'];

$split = $split + 1;
if ($split == 1){
//*******************Langs Methods**********************
//echo '<td class=\"signtext\">'.$name.'<br>'.$desc.'<br>';
//echo "<img src=\"$image_src\"".$id.">";
//echo '</td>';
// *******************implementing rc69 method with tables
echo '<table><tr><td>';
echo       $id;
echo       '</td>';
echo       '<td>';
echo       $name;
echo       '</td>';
echo     '</tr>';
echo     '<tr>';
echo       '<td>';
echo "<img src=\"$image_src\"".$id.">";
echo       '</td>';
echo       '<td>';
echo       $desc;
echo       '</td></tr></table>';
}
else if ($split == 2){
//echo '<td>'.$name.'<br>'.$desc.'<br>';
//echo "<img src=\"$image_src\"".$id.">";
//echo '</td></tr>';
echo '<table><tr><td>';
echo       $id;
echo       '</td>';
echo       '<td>';
echo       $name;
echo       '</td>';
echo     '</tr>';
echo     '<tr>';
echo       '<td>';
echo "<img src=\"$image_src\"".$id.">";
echo       '</td>';
echo       '<td>';
echo       $desc;
echo       '</td></tr></table>';

}else{
$split = 0;
//echo '<tr><td>'.$name.'<br>'.$desc.'</td>';echo "<img src=\"$image_src\"".$id.">";
echo '<table><tr><td>';
echo       $id;
echo       '</td>';
echo       '<td>';
echo       $name;
echo       '</td>';
echo     '</tr>';
echo     '<tr>';
echo       '<td>';
echo "<img src=\"$image_src\"".$id.">";
echo       '</td>';
echo       '<td>';
echo       $desc;
echo       '</td></tr></table>';
	}
}
?>

[COLOR=green]</table>

thankyou for your help so far, but I still need ya. lol

Much appreciated. thankyou




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users