-1

i am quite new to php and i have this shopping cart up which shows images. product name and details from mysql database. the code for it is the following:

<table border="0" cellpadding="2px" width="600px">
    <?
        $result=mysql_query("select * from products");
        while($row=mysql_fetch_array($result)){

    ?>
    <tr>
        <td><img src="<?=$row['picture']?>" /></td>
        <td>    <b><?=$row['name']?></b><br />
                <?=$row['description']?><br />
                Price:<big style="color:green">
                    £<?=$row['price']?></big><br /><br />
                <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
        </td>
    </tr>
    <tr><td colspan="2"><hr size="1" /></td>
    <? } ?>
</table>

What i want is for a user to be able to click on the picture/name of the product in which it should transfer the user to another page with the selected product being shown.

By the way, my mysql table consists of the following fields:

serial name description price picture

how should i go about doing this? thank u

Jahed
  • 75
  • 1
  • 3
  • 13

3 Answers3

2

Make a link like this:

<a href="product.php?product_id=<?=$row['serial']?><?=$row['name']?></a>

Then in the product.php page insert this:

$id = $_GET['product_id'];
$result=mysql_query("select * from products WHERE serial = '$id'");
$row=mysql_fetch_array($result);

Under that code you can add stuff like the picture and price etc etc :)

The serial of the product is being held ni the URL (product_id) and then called by the $_GET variable. SO product_id=1 will load the product with the serial number 1.

If you look at the URL of this current page it has the number 8142009 in the middle. If you change it to 8142008 it will load the previous question before yours. This example works in the exact same way.

Peter Stuart
  • 2,362
  • 7
  • 42
  • 73
0

You need to use either POST or GET parameters to do this.

I'll leave the actual logic of displaying the product up to you, but if you look at web addresses, they sometimes take the form of http://www.example.com/index.php?foo=bar&stuff=no. In this case, everything after the "?" is called the query string and has a "&" delimited list of variables. In this case, there are two variables, foo and stuff, which have the values of bar and no, respectively.

This will also call a script called "index.php". PHP exposes a superglobal (that is, a variable which is magically just available from everywhere) called $_GET. This is an array containing a mapping between GET variable names and their values. In this case, it would be the array ("foo"=>"bar", "stuff"=>"no"), so you can access the required values using $_GET['foo'] and $_GET['stuff'].

POST parameters are similar, but slightly different. These are submitted from HTML forms (with the "method" attribute set to "post"). In this case, the superglobal $_POST is filled out in the same way, but the array keys are the names of input elements in the HTML and their values are the values of the respective elements, so <input type="hidden" name="foo" value="bar" /> would create an entry in $_POST called "foo" with the value "bar".

From this, you should be able to have a decent go at implementing what you need.

slugonamission
  • 9,562
  • 1
  • 34
  • 41
  • Also, there are other useful superglobals such as `$_COOKIE` and `$_SESSION`, but I'll leave the reading around these to you as they're not 100% applicable to the question. – slugonamission Nov 15 '11 at 19:38
0

you will probably need a new field in your mysql table to store product page url, then do the following

<table border="0" cellpadding="2px" width="600px">
    <?
        $result=mysql_query("select * from products");
        while($row=mysql_fetch_array($result)){

    ?>
    <tr>
        <td><a href="<?=$row['product_page']?>"><img src="<?=$row['picture']?>" /></a></td>
        <td>    <b><?=$row['name']?></b><br />
                <?=$row['description']?><br />
                Price:<big style="color:green">
                    £<?=$row['price']?></big><br /><br />
                <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
        </td>
    </tr>
    <tr><td colspan="2"><hr size="1" /></td>
    <? } ?>
</table>

or you could easily use peters answer lol

Liam Allan
  • 1,115
  • 1
  • 8
  • 13