Hi,

I am trying to create a 3 column table, where each row will contain 3 'cells' which each contain the following info for a product - Product Name, Description, Product Link, Image and Price - so, in a 3x3 table I would have 9 products.

Any help much appreciated, I only started to look at MySQL and PHP less than 5 days ago, so it's been an incredibly steep learning curve but have come to a bit of a halt...spent hours playing with code I have come across on the web and reading various tutorials but still struggling big time...managed to do everything myself so far

Here is php code I have...

<?php
mysql_connect("xx", "xx", "xx") or die(mysql_error());
mysql_select_db("xx") or die(mysql_error());


// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM xx")
or die(mysql_error());  


while($row = mysql_fetch_array($result))
	{
		$product_name = $row['product_name'];
		$description = $row['description'];
		$deep_link = $row['deep_link'];
		$image_url = $row['image_url'];
		$search_price = $row['search_price'];
		
		echo "<h2>" . $product_name . "</h2>";?>
		<img src="<?php echo $image_url; ?>" ></img><BR /><BR />
		<?php echo "<b>Description: </b>" . $description . "<BR />";?>
		 <?php echo "<b>Price: </b>" . $search_price . "<BR />";?>
		<h4><a href="?php echo $deep_link; ?>">Click here to buy<a/></h4>
		<BR /><HR>
		<?php	
	}
?>

Recommended Answers

All 5 Replies

So what are you trying to do?

From the title of your post it seems you're trying to create a table in a database, but your code is just printing data from your database?

Ah sorry, I'm trying to display a table on a website which shows the products I have listed in my database. Hope that makes sense.

OK, I *think* I have the solution to my problem...but the first 'cell' in my html table is showing the 'headings' of my MySQL table, any ideas how to remove this?

Thanks,

<?php
mysql_connect("xx", "xx", "xx") or die(mysql_error());
mysql_select_db("xx") or die(mysql_error());


// Select the data from table_name
$result = mysql_query("SELECT product_name, description, deep_link, image_url, search_price FROM xx");

// Display the table
echo "<table border='0' cellspacing='16'>";

// Put results into table
$counter = 0;
while($row = mysql_fetch_array($result))
{

// Display the image
echo "<td WIDTH='112' align='left' valign='top'><a href=\"" .$row['deep_link'] . "\">

<span class='producttable'><img width='140' src=\"" . $row['image_url'] . "\" border=1 alt=\"" . $row["image_url"] . "\" title=\"" . $row["search_price"] . "\"></span>

<br>
<span class='producttext'>$row[product_name]</span></br><br>$row[description]

</br>

</span></a>";
echo "<br><span class='producttext'><strong>&pound;$row[search_price]</strong></span></br>";


$counter++; # increment count every iteration
if ( $counter % 3 == 0 )
{
echo "<tr />";
}
}
echo "</td>";
echo "</table>";

?>
Member Avatar for omirocks

Thax Dude u r realy good .......... i was serching for this code

Member Avatar for diafol

THis html looks a bit crook to me.

$result = mysql_query("SELECT ... FROM table LIMIT 9");
$x = 0; $table = '<table>'; $cols = 3;
while($data = mysql_fetch_assoc($result)){
    if($x == 0)$table .= '<tr>';

        $table .= "<td>
                    <a href='{$data['deep_link']}'>
                        <img width='140' src='{$data['image_url']}' title='{$data['search_price']}' alt='{$data['search_price']}' />
                        <span class='producttext'>
                            {$data['product_name']}
                        </span>
                        <br />
                        {$data['description']}
                    </a>
                    <br />
                    <span class='producttext'>
                        <strong>&pound;{$data['search_price']}</strong>
                    </span>
                    <br />
                </td>";

if($x == ($cols - 1))$table .= '</tr>';
    $x = ($x == ($cols - 1)) ? 0 : $x + 1;
}
$table = '</table>';


echo $table;

Try to avoid including inline properties/attributes - use CSS rules instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.