Jump to content

[SOLVED] Array help needed


bionic25

Recommended Posts

if you want the whole code heres the page that holds it.

 


<?php
// Include functions
require_once('functions.php');
// Start the session
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jq.js"></script>

<script>
var refreshId = setInterval(function()
{
     $('#refresh')./*fadeOut("slow").*/load('getCart.php')/*.fadeIn("slow")*/;
}, 1000);
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<div id="refresh" style="float: right; width: 300px; background: #ccc;">

</div>
<h1>products</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" name="qty">
    <?php
dbcon();
productDump();
addItem();
onDelete();
?>
    </form>
</body>
</html>

 

heres the functions page

 

<?php

//FUNCTIONS

function dbcon(){
mysql_connect("db1", "**", "**") or die(mysql_error());
mysql_select_db("**") or die(mysql_error());
}

function getCart(){
echo "<h2>Basket:</h2>";
dbcon();
/*
//testing purposes
session_start();
$_SESSION['cart']="123,456,123,123";
*/

//eg entry ,123456*01
$items=$_SESSION['cart'];
$item=explode(',',$items);
foreach ($item as $x)
{
	$pidBit = substr($x, 0, 6);
	echo "$pidBit";
	$result = mysql_query("select * from products where id= $pidBit");
	while($r=mysql_fetch_array($result))
	{
		$title = $r['title'];
		$description = $r['description'];
		$price = $r['price'];
		echo "$title<br />$description<br />$price <a href=this.php?action=remove&pid=$pidBit>remove</a><hr />";
	}
}
}

function onDelete(){

if ($_GET['action']=='remove')
{
$delPid = $_GET['pid'];
$items=$_SESSION['cart'];
$item=explode(',',$items);
foreach ($item as $x)
	{
	if ($pidBit == $x)
		{
		//$items - $x = (session-pid)
		$rep = ",".$pidBit;	
		$newCart = str_replace($rep, "", $items);
/*
		//handles first item if that is item being deleted
		$newerCart = str_replace($x, "", $newCart);
*/
		$_SESSION['cart']=$newCart;
		}
	else
		{

		}
	}
		echo "new calculated string: $newCart";
		$ses=$_SESSION['cart'];
		echo " new session output: $ses";
}
else{}
}

function addItem(){

if ($_GET['action']=='add')
{
$newPid = $_GET['pid'];
$qty = $_GET['qty'];
$oldCart = $_SESSION['cart'];
$newCart = $oldCart.','.$newPid.'*'.$qty;
$_SESSION['cart']=$newCart;
}
else
{
}

}

function productDump(){
	$result = mysql_query("select * from products");
	while($r=mysql_fetch_array($result))
	{
		$title = $r['title'];
		$description = $r['description'];
		$price = $r['price'];
		$pid = $r['id'];
		echo "$title<br />$description<br />$price
		<select name=qty id=qty> 
			<option value='1'>1</option>
			<option value='2'>2</option>
			<option value='3'>3</option>
			<option value='4'>4</option>
			<option value='5'>5</option>
		</select>
		<input type=submit name=submit value=add /><hr />";
$qty = null;//ADD
if (isset($_GET['submit'])) {
$qty= (int)$_GET['qty'];//ADD the (int)
$url="this.php?action=add&pid=$pid&qty=$qty";
echo "$qty";
//echo "<script>window.location = \"$url\"</script>";
		/*
		echo "<script>window.location = \"$url\"</script>";
		*/
		}
		else
		{
		}
	}
}

?>

 

please note the ocde is still very much under construction and done from scratch with limited knowhow if it looks clumsy.

Link to comment
Share on other sites

Just figured it out.

 

Just bear with me here :-D

 

Your productdump() function loops through adding forms for each product id. So what you need to do is to be able to determine how they change each of the products... Since each product is using the same form, this means that same form gets repeated, so when you submit, you literally submit ALL forms. So we need to fix this by adding a unique identifier.

 

   function productDump(){
      $result = mysql_query("select * from products");
      while($r=mysql_fetch_array($result))
      {
         $title = $r['title'];
         $description = $r['description'];
         $price = $r['price'];
         $pid = $r['id'];
echo "<form action={$_SERVER['PHP_SELF']}' method='get' name='qty'>";//remove this line from the other page
         echo "$title<br />$description<br />$price
         <select name=qty id=qty> 
            <option value='1'>1</option>
            <option value='2'>2</option>
            <option value='3'>3</option>
            <option value='4'>4</option>
            <option value='5'>5</option>
         </select>
<input type=hidden name='pid' value=$pid'>
         <input type=submit name=submit value=add /></form><hr />";
$qty = null;//ADD
if (isset($_GET['submit'])) {
$qty= (int)$_GET['qty'];//ADD the (int)
$url="this.php?action=add&pid=$pid&qty=$qty";
echo "$qty";
//echo "<script>window.location = \"$url\"</script>";
         /*
         echo "<script>window.location = \"$url\"</script>";
         */
         }
         else
         {
         }
      }
   }

 

Then, you'll be able to determine the product id that you're handling with $_POST['pid'] and the qty with $_POST['qty']

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.