Page 1 of 1

Newbie: Silly Splitting Results Issue

Posted: Mon Dec 20, 2004 5:09 am
by darkdrums
Hello everybody!

I'm new round these parts, and to PHP, so be gentle. Anyway....

I'm trying to split my mySQL results from my database table so that, not un-similar to a certain large online retail Co., I have a four-column display with the image in the one row and a description in the row below. E.g. the first table row would be something like: image1, image2, image3, image4. Then the next row would be description1, description2, description3 and description4. Get the idea?

I can successfully output my results in to a four-column table but not the image/description separation part. If any one is still reading and could give me a point in the direction I should be going I would be most grateful.

Here is what I have so far:

Code: Select all

<?php

mysql_connect($db_host, $db_user, $db_password,$db_name) or
  die("Could not connect: " . mysql_error());
mysql_select_db("prod_test");

$result = mysql_query("SELECT * FROM products");
$num_rows = mysql_num_rows($result);
$i = 0;
echo "<table>";		//open items table

// start output
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) &#123;
	if ($i%4==0)
	&#123;
		echo "<tr>";
	&#125;
	echo("<td>".$row&#1111;'sku']."</td>");
	$i++;
	if($i%4==0)
	&#123;
  		echo "</tr>";
	&#125;
&#125;

echo "</table>";	//close items table
echo "Number of records ".$num_rows."<br>";

mysql_free_result($result);		//end sql statment and free memory

?>

Posted: Mon Dec 20, 2004 4:00 pm
by timvw
instead of echoing directly, store the string in memory (for a little while anyway)

Code: Select all

$count = 0;
$row_image = '';
$row_description ='';

// retrieve stuff in $result
while ($row = mysql_fetch_assoc($result))
{
    $count++;
    $row_image .= "<td>{$row['image']}</td>";
    $row_description .= "<td>{$row['description']}</td>";

     if ($count == 4) 
     {
        echo  "<tr>{$row_image}</tr>";
        echo "<tr>{$row_description}</Tr>";

        $count = 0;
        $row_image = '';
        $row_description = '';
      }
}

Posted: Tue Dec 21, 2004 4:06 am
by darkdrums
Thankyou so much timvw. :D Didn't realise it was that simple!

Like I said I am new to this PHP thing and have found myself feeling around in the dark a little.