Jump to content

Shopping cart help


avatar.alex

Recommended Posts

I am creating a shopping cart script and I need help with something

I want the url to look like shop.php?cat=catname&item=itemid

it so far looks like

shop.php?cat=catname

 

but i don't know what else to use like else or ifset of if or anything help please

<?php

include “connect.php”;

$query = “SELECT * FROM sms_cat”;

$results = mysql_query($query);


$num = mysql_fetch_array($results);

if($num > 0){

        
            $i = 1;

            $category = $_GET[‘cat’];

while($i < $num){

            $cat = $num[$i,‘cat’];



            $itemid = $num[$i,’cat’];

            $price = $num[$i,’price’];

            $name = $num[$i,’name’];

            

            if($cat == $category){

                        echo “<a herf=’shop.php?cat='.$cat.'&item='.$itemid.'’>”.$name.”</a><br />”;

            }
            
}

}

?>

Link to comment
Share on other sites

You don't need to escape variables in your URL.

 

echo "<a href=\"shop.php?cat=$cat&item=$itemid\">$name</a><br />\n";

 

Also change this line:

 

$results = mysql_query($query);

 

To:

 

$results = mysql_query($query) or die(mysql_error());

 

Change:

 

$num = mysql_fetch_array($results);

if($num > 0){

 

To:

 

$num = mysql_num_rows($results);

if($num > 0){

 

Below that add:

 

$row = mysql_fetch_array($results);

 

Change all your $num variables to $row.

 

Also change:

 

while($i < $num){

 

To:

 

while($i <= $num){
//in this you could also do
//while($row = mysql_fetch_array($results)){
//then manually select each row
//$sql = "SELECT * FROM `myTable` WHERE `id` =$row[id]";
//$res = mysql_query($sql) or die(mysql_error());
//$row2 = mysql_fetch_assoc($res);
//then change all the $num variables to $row2 and you wouldn't have to use $i

 

I also suggest using normal apostrophes and quotes, ' and "

Link to comment
Share on other sites

now it should look like this right

 

<?php

include “connect.php”;

$query = “SELECT * FROM sms_cat”;

$results = mysql_query($query) or die(mysql_error());



$num = mysql_num_rows($results);

if($num > 0){

$row = mysql_fetch_array($results);
        
            $i = 1;

            $category = $_GET[‘cat’];

while($i <= $num){

            $cat = $num[$i,‘cat’];



            $itemid = $num[$i,’cat’];

            $price = $num[$i,’price’];

            $name = $num[$i,’name’];

            

            if($cat == $category){

                        echo "<a href=\"shop.php?cat=$cat&item=$itemid\">$name</a><br />\n";

            }
            
}

}

?>

 

OK i am trying to make it so when they click the like it pulls all the info from the batabase

 

like if i was buying a book the like would look like shop.php?cat=books&item=php i need a item if statment like the

if($cat == $category){

on only to fit with this code

Link to comment
Share on other sites

Try this:

 

<?php

include "connect.php";
$query = "SELECT * FROM sms_cat";
$results = mysql_query($query) or die(mysql_error());


$num = mysql_num_rows($results);

if($num > 0){

$category = $_GET['cat'];


while($row = mysql_fetch_array($results)){
$sql = "SELECT * FROM `sms_cat` WHERE `id` =$row[id]";
$res = mysql_query($sql) or die(mysql_error());
$row2 = mysql_fetch_assoc($res);

	$cat = $row2['cat'];
	$itemid = $row2['itemid']; //or whatever your field is for itemid
	$price = $row2['price'];
	$name = $row2['name'];

            

	if($category){
		if($category == $cat){

		echo "<a href=\"shop.php?cat=$cat&item=$itemid\">$name</a><br />\n";

		}
	}
}
}

?>

Link to comment
Share on other sites

Are you trying to find all ITEMS in the category PHP?

 

Just do something like:

 

$item = $_GET['item'];

if($item){
$item = mysql_real_escape_string($item);
$sql = "SELECT * FROM `myTable` WHERE `item` ='$item'";
$res = mysql_query($sql) or die(mysql_error());

   if(mysql_num_rows($res) > 0){
      while($row = mysql_fetch_assoc($res)){
      echo "$row['name']<br>\n"; //do whatever to that line
      }
   }else {
   echo "No items exist";
   }
}else {
echo "You need to have an item defined";
}

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.