Jump to content

Displaying records from a mySQL DB

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
37 replies to this topic

#1
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
Hello all

I am currently trying to teach myself some PHP and mySQL work in order to build a DB driven website.

I have a table in my database which stores information about a product eg (product name, price, description, image(set as a blob))

What I want to do is list all the items in this table in a webpage with the image showing following by the name and price of the item under it, something similar to the way Game.co.uk website displays their product information

I also would like when an item has been clicked on to show a new page with all the item's details.

Are there ay templates any knows of or has which I could go by as I am struggling to get this working

Kind Regards

Akin

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
You'd need 3 pages. The products page which displays all products, the image which will display the image, and the product details page which will show all the details about the product.

<?PHP
//display all products
$query = mysql_query("SELECT * FROM `products`") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
    //format the output
    echo '<a href="fullProduct.php?id='.$row['id'].'"><img  src="image.php?id='.$row['id'].'">'.$row['name'].'</a>';
}
<?PHP
//display image
$id = $_GET['id'];//FILTER!!
$query = mysql_query("SELECT `image` FROM `products` WHERE `id`='$id'") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
    $im = imagecreatefromstring($row['image']);
    if ($im !== false) {
        header('Content-Type: image/png');
        imagepng($im);
        imagedestroy($im);
    }
    else {
        echo 'An error occurred.';
    }
}
<?PHP
//display full product
 $id = $_GET['id'];//FILTER!!
 $query = mysql_query("SELECT * FROM `products` WHERE `id`='$id'")  or die(mysql_error());
 while($row=mysql_fetch_assoc($query)) {
     //format the output
 }
 


#3
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
Excellent thats worked like a charm thanks alot mate just another quick thing

When im formatting the output I want each image to appear side by side/horizontally with the details of the items below the image so far I have this

...//display all products
$query = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
//format the output
echo '<table border="1" cellpadding="10">
<tr>
<td><a href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'"> </td>
</tr>
<td>'.$row['name'].'</a><br />£'.$row['price'].'</td>
</tr>
</table>';
}

This displays the details below the images as I want but each record is shown vertically, how would I get the images to appear side by side?

Thanks

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Try this
<?PHP //display all products
$query = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
    //format the output
    echo '<table border="1" cellpadding="10">
    <tr>
    <td><a href="fullProduct.php?id='.$row['id'].'"><img  src="image.php?id='.$row['id'].'"></a></td>
    <td><a  href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br  />£'.$row['price'].'</td>
    </tr>
    </table>';
} 


#5
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
I have tryed that already, that displays the image in one column with the details in a column next to it

I have been toying with it for some time now and cannot get it to display the images in one row and the details in a row below

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Oh, sorry. I misunderstood.
 <?PHP //display all products
$query = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
while($row=mysql_fetch_assoc($query)) {
    //format the output
    echo '<table border="1" cellpadding="10">
    <tr>
        <td><a href="fullProduct.php?id='.$row['id'].'"><img  src="image.php?id='.$row['id'].'"></a></td>
    </tr>
    <tr>
        <td><a  href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br  />£'.$row['price'].'</td>
    </tr>
    </table>';
}  
I think you just forgot to close the tr tag.

#7
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
That is still producing the images below one another and not side by side

The code was exactly the same as I had and it does seem to make sence but displays as soo

[IMAGE]
[Image details]
[IMAGE]
[Image details]
[IMAGE]
[Image details]

Where it should be


[IMAGE] [IMAGE] [IMAGE]
[Image details] [Image details] [Image details]

:cursing:

#8
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
 <?PHP //display all products
$query = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
echo '<table border="1" cellpadding="10"><tr>';
while($row=mysql_fetch_assoc($query)) {
    //format the output
    echo '<td><a href="fullProduct.php?id='.$row['id'].'"><img  src="image.php?id='.$row['id'].'"></a><br />'.
        '<a  href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br  />£'.$row['price'].'</td>';
}
echo '</tr></table>';
}


#9
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
Thats the one

There was an extra } in there but took it out and worked!

Thanks alot it is very much appreciated!!

:thumbup:

#10
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
How would I be able to specify a new row to start? as currently the data outputs in one continious row.

I have put the table in a div tag and the width has been set to 600px yet the table continues past the tags specified width and carrys on showing in one continous line

#11
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

aakinn said:

How would I be able to specify a new row to start? as currently the data outputs in one continious row.

I have put a width attribute on the table but this does not seem to make any difference. When I add more records it continues to carry on off the side of the page
You'd simply use an if statement:

$i = 0;
while($row=mysql_fetch_assoc($query)) {
    //format the output
    echo '<td><a  href="fullProduct.php?id='.$row['id'].'"><img   src="image.php?id='.$row['id'].'"></a><br />'.
        '<a   href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br   />£'.$row['price'].'</td>';
    if(($i%4)==3) {
        echo '</tr><tr>';
    }
    $i++;
}


#12
aakinn

aakinn

    Newbie

  • Members
  • PipPip
  • 24 posts
Ive not seen that clause in an if statement before what exactly does it doo? It dont look quite right to me and an error is being thrown on line 56

Parse error: syntax error, unexpected T_ECHO in /home/ka751/public_html/WAT/products.php on line 56

Line 56 being

"echo '</tr><tr>';"

But something is telling me its with the clause in the IF statement