avatar.alex Posted May 30, 2007 Share Posted May 30, 2007 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 />”; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/ Share on other sites More sharing options...
pocobueno1388 Posted May 30, 2007 Share Posted May 30, 2007 I don't understand what you are wanting... Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/#findComment-265263 Share on other sites More sharing options...
marcus Posted May 30, 2007 Share Posted May 30, 2007 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 " Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/#findComment-265264 Share on other sites More sharing options...
avatar.alex Posted May 30, 2007 Author Share Posted May 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/#findComment-265272 Share on other sites More sharing options...
marcus Posted May 31, 2007 Share Posted May 31, 2007 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"; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/#findComment-265290 Share on other sites More sharing options...
avatar.alex Posted May 31, 2007 Author Share Posted May 31, 2007 No thats not it, crap I wish I could explain it better. im in Shop.php-->I click on an item link-->the items info is brought up becuase the url reads the ID shop.php?cat=cat&item=PHP I need the bolded in another if statment or watever funtion. Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/#findComment-265307 Share on other sites More sharing options...
marcus Posted May 31, 2007 Share Posted May 31, 2007 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/53664-shopping-cart-help/#findComment-265308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.