Jump to content

Recommended Posts

hello freaks

i am fetch the value through database but unable to take in format what i am want here my fetching code

 

while ($row2=mysql_fetch_array($res2))

{

echo "<table width=100% cellpadding=0 cellspacing=0><tr><td><td><a href='home1.php?sub_cat_id=$row2[id]'>$row2[cat_name]</a></td></td></tr></table>";

//echo "<td></td><tr></tr>";

}

and output came like this

------------------------

Animation Outsourcing

Business Process Outsourcing

Graphic Design and Multimedia

Callcenter Outsourcing

 

                    but i want result like this

 

Animation Outsourcing                                      Business Process Outsourcing

 

 

Graphic Design and Multimedia                            Callcenter Outsourcing

 

 

 

 

Hi,

 

The problem is that each row of your result set is appearing in its own table rather than a different rows of the same table. You want to do something like this:

 

// Start table definition
echo "<table>";
while ($row2=mysql_fetch_array($res2))
{
   echo "<tr>";
   echo "<td><a href='home1.php?sub_cat_id=$row2[id]'>$row2[cat_name]</a></td>";
   echo "</tr>";
}
// end table definition
echo "</table>";

 

However, this'll still produce results in one column, but at least they're all part of the same table. We can embellish this with basic structure by keeping a count of how many columns you want per row, and every time we reach that number we start a new row:

 

$COLS_PER_ROW = 2;
$colCount = 0;
$numRows = mysql_num_rows ($res2);
// Start table definition
echo "<table>";
while ($row2=mysql_fetch_array($res2))
{
   // Start a row whenever our count is divisible by the number of items in each row
   if ($colCount % $COLS_PER_ROW == 0)
   {
      echo "<tr>";
   }
   echo "<td><a href='home1.php?sub_cat_id=$row2[id]'>$row2[cat_name]</a></td>";
   // End the row when the last space in the row has been filled, or when there are no more items in the result set.
   if ($colCount % $COLS_PER_ROW == $COLS_PER_ROW - 1 || $colCount == $numRows - 1)
   {
      echo "</tr>";
   }
   $colCount++;
}
// end table definition
echo "</table>";

 

This is based on some code I have for displaying picture thumbnails in a table for a photo gallery page on my website. Haven't tested the code snippet above so may contain the odd typo, but the structure is about right I reckon.

 

Regards,

Darren.

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.