Jump to content

[SOLVED] Newbie - help with table please


Neilsaab

Recommended Posts

Hi guys,

 

I'm new to using php and MySQL and I'm stuck on one small part of creating the table I want.

 

I have created a database which will store details of cars for sale. In the table is a link to a photo of the car. It took me a while, as I said I'm new to this, but I managed to get some code to enable me to show the pic in a table. Because of the way I've done this I'm struggling to get the table to show more than the first row.

 

Could someone show me how to use a loop to print out all the rows from the dbase, or correct the coding I have done.

 

Thanks in advance.

 

Here is my code:

<?
//make the database connection
$conn = mysql_connect("localhost", "neil", "Norwich1969") 
or die('Could not connect: ' . mysql_error());
$select = mysql_select_db("nigelcars", $conn) 
or die('Could not select database');

//create query
$sql = "SELECT * FROM cars";
$result = mysql_query($sql)
 or die('Query failed: ' . mysql_error());
$mainRow = mysql_fetch_assoc($result);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";


$carId = $mainRow["carid"];
$pic = $mainRow["pic"];
$carDesc = $mainRow["cardesc"];
$price = $mainRow["price"];


//print table
print "<table width = 90% cellpadding=10 cellspacing=0 border=2>\n";
print "<tr><td align=center><b>ID</b></td><td align=center><b>Photo</b></td>
<td align=center><b>Description</b></td><td align=center><b>Price £</b></td>
</tr>";


print "<tr>\n";
print "<td align = center>$carId</td>";
print "<td align = center><img src=$pic></td>";
print "<td align = center>$carDesc</td>";
print "<td align = center>$price</td>";

print "</table>\n";

?>

Link to comment
Share on other sites

<?php
//make the database connection
$conn = mysql_connect("localhost", "neil", "Norwich1969") 
or die('Could not connect: ' . mysql_error());
$select = mysql_select_db("nigelcars", $conn) 
or die('Could not select database');

//create query
$sql = "SELECT * FROM cars";
$result = mysql_query($sql)
or die('Query failed: ' . mysql_error());

$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";

while($mainRow = mysql_fetch_assoc($result))
{
$carId = $mainRow["carid"];
$pic = $mainRow["pic"];
$carDesc = $mainRow["cardesc"];
$price = $mainRow["price"];


//print table
print "<table width = 90% cellpadding=10 cellspacing=0 border=2>\n";
print "<tr><td align=center><b>ID</b></td><td align=center><b>Photo</b></td>
<td align=center><b>Description</b></td><td align=center><b>Price £</b></td>
</tr>";


print "<tr>\n";
print "<td align = center>$carId</td>";
print "<td align = center><img src=$pic></td>";
print "<td align = center>$carDesc</td>";
print "<td align = center>$price</td>";

print "</table>\n";
}

?>

Link to comment
Share on other sites

You can also eliminate this

 

$carId = $mainRow["carid"];

$pic = $mainRow["pic"];

$carDesc = $mainRow["cardesc"];

$price = $mainRow["price"];

 

If you just add those 4 Arrays in your

 

print "<td align = center>$carId</td>";

print "<td align = center><img src=$pic></td>";

print "<td align = center>$carDesc</td>";

print "<td align = center>$price</td>";

 

code.

Link to comment
Share on other sites

That is fantastic guys. Thanks a lot. What a fast response as well  ;D

 

I knew I needed to use a while loop, just wasn't sure how to set it up with the variables.

 

Again thanks, I'll definately be sticking around these forums in case I get stuck again.

 

Neil

 

Link to comment
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.