Jump to content

Recommended Posts

I've been using this code to split records from a db into columns when it displays the data. Items are shown with 3 on a row and then a new row starts. It works really well but if ever there is only one item on a row it causes me a problem in this instance. I have far left and far right columns which display a background image, which is a border. The problem is this only works if there are three columns visible in a row.

 

Is it possible to manipulate the following code so that there are always three columns present, they're just empty if there's no more records to fill them? I've tried various methods but no luck.

 

$i=1;
    while($row = mysql_fetch_array($Result)){
        
if($i%3==1)
{
echo "<tr valign='top'><td width='4' background='images_set_second/second_page_r2_c1.gif'></td>
<td width='4' colspan='3'></td>
";
}

echo("<td width='260'><div class='body' align='center'><b>");
   echo("<div align='center'>$row[product_name]</div>");
   echo("</b>");
   echo($row["product_sub_type"]);
   echo("<br><div align='center'><a href=javascript:MM_openBrWindow('images_enlarged.php?id=$row[id]','enlarge','width=750,height=600,resizable=no,status=no,toolbar=no,scrollbars=yes')><img border='1' src='POPUP URL'></a><br><img border='0' src='images/click_image_to_enlarge.jpg'></div><br><br>");
echo("</div></td>");
    
if($i%3==0)
{

    echo("<td width='3' colspan='4'></td>
<td width='3' background='images_set_second/second_page_r2_c11.gif'></td>
</tr>");
}
$i++;
}}

Link to comment
https://forums.phpfreaks.com/topic/55979-creating-columns/
Share on other sites

I'm no nearer to solving this. I've pasted the whole code below. Basically what I need this to do is to make sure that when there aren't three columns present in a row, a column is still created. The code works perfectly when there is data to fill three columns, but when there is only one or two columns of data, it makes the table look odd. How can I manipulate the following code to create a column regardless of whether there is data to fill it? Sorry it's a bit long by the way!

 

echo("
<table cellspacing='0' cellpadding='0' width='770'>
<tr>
<td width='4' background='images_set_second/second_page_r2_c1.gif'>
<td class='body' colspan='5'>     <img src='page_titles/$word.jpg'><br><br></td>
<td width='3' colspan='5'></td>
<td width='3' background='images_set_second/second_page_r2_c11.gif'></td>
</tr>
$Filter
");

if ($Filter) {

$NumRows = mysql_query("select * from products_db where product_sub_type LIKE '$word' OR product_sub_type LIKE '$word2' OR product_sub_type LIKE '$word3' OR product_sub_type LIKE '$word4' order by product_name") or die("Sorry Couldn't Complete Query_1");
$NumRows = mysql_num_rows($NumRows);

if ($NumRows == "0")    {
  echo ("<font face=\"Verdana\" size=\"2\" color='#003366'>Sorry, we do not currently have any products listed in this section.</font>
");

}
else  {
$NumRows = mysql_query("select * from products_db where product_sub_type LIKE '$word' OR product_sub_type LIKE '$word2' OR product_sub_type LIKE '$word3' OR product_sub_type LIKE '$word4' order by product_name") or die("FIX ME");
$NumRows = mysql_num_rows($NumRows);
$Result = mysql_query("select * from products_db where product_sub_type LIKE '$word' OR product_sub_type LIKE '$word2' OR product_sub_type LIKE '$word3' OR product_sub_type LIKE '$word4' order by product_name") or die("Sorry Couldn't Complete Query_2");

    $i=1;
    while($row = mysql_fetch_array($Result)){
     
if($i%3==1)
{
echo "<tr>";
}

echo("<td colspan='12'>");
   echo($row["$product_sub_brand $product_name"]);
echo("</td>");
    
if($i%3==0)
{
    echo("</tr>");
}
$i++;
}

echo("
<tr valign='top' align='left'>
    <td colspan='12'></td>
</tr>
");
}}
else {
$NumRows=mysql_query("select * from products_db WHERE product_sub_type LIKE '$word' OR product_sub_type LIKE '$word2' OR product_sub_type LIKE '$word3' OR product_sub_type LIKE '$word4' order by product_sub_brand");
$NumRows=mysql_num_rows($NumRows);
$Result = mysql_query("select * from products_db where product_sub_type LIKE '$word' OR product_sub_type LIKE '$word2' OR product_sub_type LIKE '$word3' OR product_sub_type LIKE '$word4' order by product_sub_brand") or die("");
$i=1;
    while($row = mysql_fetch_array($Result)){
        
if($i%3==1)
{
echo "<tr valign='top'><td width='4' background='images_set_second/second_page_r2_c1.gif'></td>
<td width='4' colspan='3'></td>";
}

echo("<td class='body' width='255'><div align='center'><b>");
   echo("<div align='center'>$row[product_sub_brand]</div>");
   echo("</b>");
   echo($row["product_name"]);
   echo("<br><div align='center'><a href=javascript:MM_openBrWindow('images_enlarged.php?id=$row[id]','enlarge','width=750,height=600,resizable=yes,status=no,toolbar=no,scrollbars=yes')><img border='1' src='IMG URL HERE'></a><br><font face=\"Verdana\" size=\"1\"><i>Click Image to Enlarge</i></font></div><br><br>");
echo("</div></td>
");
    
if($i%3==0)
{

echo("<td width='3' colspan='4'></td>
<td width='3' background='images_set_second/second_page_r2_c11.gif'></td>
</tr>
");

}

$i++;
}}}
echo("</table>");

Link to comment
https://forums.phpfreaks.com/topic/55979-creating-columns/#findComment-277993
Share on other sites

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.