Newbie: Silly Splitting Results Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darkdrums
Forum Newbie
Posts: 3
Joined: Mon Dec 20, 2004 4:53 am
Location: Staffordshire, UK

Newbie: Silly Splitting Results Issue

Post 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

?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 = '';
      }
}
darkdrums
Forum Newbie
Posts: 3
Joined: Mon Dec 20, 2004 4:53 am
Location: Staffordshire, UK

Post 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.
Post Reply