Jump to content

Give unique values inside dynamicList


Pavlos1316

Recommended Posts

  • Replies 72
  • Created
  • Last Reply

Hello,

 

I am watching again the tutorial that I used for my cart.

 

In the tutorial the author is using the dynaList to create items like that:

 

item 1  |  price  | View item details (which is a link to the item's page)

Now, that page with only the item 1 details in it, has the "Add to Cart" Button.

 

What I haven't realize when I was changing it in order to include the Add to Cart Button inside the dynaList is the way that the author was sending the user to the single product page.

 

The "View item details" link was

<a href="product.php?id=' . $id . '">View item details</a>

and after in the product page is getting that id with:

if (isset($_GET['id'])) {
// Connect to the MySQL database  
    include "scripts/connect_to_mysql.php"; 
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);

and continues by getting all the info to display that product and adding it to the cart (with the code and keys that I am using) without any problem.

 

What is changing in my dynaList and is not working compared to the above?

 

Another tip that I want:

Is it

value="' . $id . '"

exactly the same as

value="<?php echo $id; ?>"

?

Link to comment
Share on other sites

Hey that's fine going to a details page or whatever you want to do.  And you can change the form input back to having (name="item_id" value=$id) if that helps as well.  We'll just pick up the value on the processing section.

foreach($_POST as $ky => $val){
IF ($val!="Add To Cart"){
$key=$val;
}

Link to comment
Share on other sites

:) .... hmmm... No. I Don't want to do anything, but just adding an item and staying at my items page. That's why I tried to change the code a little bit. Else I would just copy paste the entire code as it was and finished (it was working as it was). I just said to mention it in case that it would make it easier to fix my code.

 

I cannot even make your code to work and I cannot think why since you tried it and is ok.

 

As for my last question.... Are they exactly the same?

 

Thank you

 

Link to comment
Share on other sites

Okay, well then you should just create your item page as you had it, without links to a details page.  Your form can place the $id in either the name or value spot of the input tag (what ever works for you) and we'll pickup the id either way.  As far as (My script) not working, I wasn't sure if you had the "cart_keys" session lines I posted above as that is what allows you to retrieve as view items in the cart. I assume you have all this on one page as you want to stay on the same page. 

 

So you don't mess anything you've got, try adding this to a new page.  Add DB connection info at the top and your table name for the $sql query.  Notice you'll need to change the link to clear session for testing echo "<a href='test9.php?end=t'>Clear Cart</a><br />";

session_start();
if (!isset($_SESSION["cart_keys"])){
$_SESSION["cart_keys"]=array(0);
} 
IF ($_POST['button']=="Add to Cart"){
foreach($_POST as $ky => $val){
IF ($ky!="button"){
$key=$ky;
}
} 
$i=0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array$key"]) || count($_SESSION["cart_array$key"]) < 1) {
    // RUN IF THE CART IS EMPTY OR NOT SET
	$_SESSION["cart_array$key"] =array(1);
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
IF (isset($_SESSION["cart_array$key"])){
    $i++;
}
IF ($i==1){
$v=$_SESSION["cart_array$key"][0];
$nv=$v+1;  
$_SESSION["cart_array$key"] = array($nv);
}
}

$ck=$_SESSION["cart_keys"];
if (in_array($key, $ck, true)){
// echo "Got key";
}
ELSE{
array_push($ck, $key);
$_SESSION["cart_keys"] =$ck;
}
}//end post
////  Display ////
foreach($_SESSION["cart_keys"] as $cartitem){
IF ($cartitem>0){
$iq=$_SESSION["cart_array$cartitem"][0];
echo "Item Ordered: $cartitem Quanitity: $iq<br />";
}
}
//////////Clear Cart for testing//////////// 
IF ($_GET['end']=='t'){
session_destroy();
}
echo "<a href='test9.php?end=t'>Clear Cart</a><br />";
////////////Add your table here//////////////
$sql = mysql_query("SELECT * FROM your_table ORDER BY id ASC"); 
//////////////////
$productCount = mysql_num_rows($sql);
//echo "$productCount";
// count the output amount
if ($productCount > 0) {
    $i=0;
	$dynamicListBody = "<table style='width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;'>";
	while($row = mysql_fetch_array($sql)){
		$id = $row["id"];
		$product_name = $row["product_name"];
		$details = $row["details"];
		$price = $row["price"];
		$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
	$dynamicListBody .= ($i==0) ? '<tr>':'';
	$dynamicListBody .= '<td width="10%">
				<img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" alt="' . $product_name . '" />
			     </td>
			     <td width="35%">
				<span class=itmttl>' . $product_name . '</span>
				<br />
				<span class=text>' . $details . '
				<br />
				€' . $price . '</span>
				<br />
			      <form id="bd_itm'. $id . '" name="bd_itm'. $id . '" method="post" action="#">
				<input type="hidden" name="'. $id . '" value="pid" />
				<input type="submit" name="button" value="Add to Cart" />
				<br />
				<br />
			      </form>
			     </td>';
	$dynamicListBody .= ($i==1) ? '</tr>':'';
	$i++;
	($i==2) ? $i=0:'';
	}
	$dynamicListBody.='</table>';
	} else {
		$dynamicListBody = "We have no products listed in our store yet";
		}
	mysql_close(); 
	echo "$dynamicListBody";

Link to comment
Share on other sites

You can also change the "clear cart" to a form, which will work better in a real application.  Just change the form action to the page name you're using.

//////////Clear Cart for testing////////////
echo "<form method=\"post\" action=\"test9.php\"><input type=\"hidden\" name=\"mode\" value=\"clear\" /><input type=\"submit\" name=\"button\" value=\"Clear Cart\" /></form>"; 
IF ($_POST['mode']=='clear'){
session_destroy();
}

Link to comment
Share on other sites

Hello Drummin,

 

I haven't test what you gave me still but I just wanted to say:

 

Thank you.

 

P.S. I had all the lines of the code as you were asking me to... Don't worry, at least I got that right :)

But I have all my scripts on external files. even the ones that you were giving me. I hope that wasn't the problem.

 

Going to test what you wrote now....

Link to comment
Share on other sites

ok... I tested it several times...

 

I had to comment out some of my jquery script in order not to block your php, but the only thing I get to display is the clear button.....

 

I have different external script pages for dynamicList.php and my cart-functions.php to add-remove-update the cart.

 

I was "blocking" the redirection to the cart with jq. Now even without the jq, letting the add button to redirect me, I don't get the items displayed... Only the Clear Cart Button....

Link to comment
Share on other sites

yes... I don't want to be redirected or refreshed.... But that I managed... The major problem, I think thats why I started this post (I can't remember anymore), it was because my add-to-cart function wasn't proccessing any other item but the first. And the suggestion was to get unique values for my items and keys...!

 

Even that I have just discovered using the firebug, that my pid is getting unique every time...!

 

This isn't what you are trying to do and help me with your scripts???

Link to comment
Share on other sites

More that likely, your sessions are now being set but because the page is not being reloaded, you don't see it.  Wow, and just think how easy this would be if you just submitted the info and added it to MySQL.  Hey that would be too easy.

Link to comment
Share on other sites

Drummin you re talking like I have solved this...  8) but I haven't... I have been playing around and these are my results:

ok... I will try my best to explain this.

 

1. all my cart-functions.php (add-show-remove)

<?php

//Add

header("location: ../cart.php");
exit();

//Show-Remove
?>

2. my dynamicList.php

<?php

//Show the list

<form name="bd_itm" id="bd_itm" method="post" action="help_scripts/cart-functions.php">
<input type="hidden" name="pid" id="pid" value="' . $id . '" />
<input type="submit" name="button" id="button' . $id . '" value="Add to Cart" />
?>

Here is what happening (i am testing in FireFox using Fire Bug)

If I click my <Add to Cart> button FB shows that my pid=is unique for each item

and I know that is has sent it into my cart-functions.php because it shows also the cart.php that I am sending it to.

 

BUT NO ITEM IS ADDED TO MY CART No errors Anywhere! I am using this code to display errors

<?php
error_reporting(E_ALL); 
ini_set("display_errors", 1);
?>

 

If I add my session_start(); inside my cart-functions.php at the top (I have it in my index.php for now), the first item of my dynaList is added in my cart. But  ONLY the first!

 

Is there the possibility to be a bug with dynamiclist not understanting that session is started and ignoring my <add.to.cart.script>?

 

Or is it just something that no-one has understand yet in order to help me fixing it?????

 

Thank you

Link to comment
Share on other sites

I see in your form you have the unique id as the value... SO on the processing script you need to use the VALUE.  Where I had

IF ($_POST['button']=="Add to Cart"){
foreach($_POST as $ky => $val){
IF ($ky!="button"){
$key=$ky;
}
}

You'll need to change it to use the value instead of the key

IF ($_POST['button']=="Add to Cart"){
foreach($_POST as $ky => $val){
IF ($ky!="button"){
$key=$val;
}
}

Link to comment
Share on other sites

ok... I found an other code which I was playing with some time ago. I tried it and it's partialy work with my dynamicList like this:

dynaList (the form part):

<form name="bd_itm" id="bd_itm" method="post" action="p_cart1.php?action=add&id=' . $id . '">
<input type="hidden" name="pid" id="pid" value="' . $id . '" />
<input type="submit" name="button" id="button" value="Add to Cart" />
</form>

And the cart-functions.php:

$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['id'];
	} else {
		$cart = $_GET['id'];
	}
	break;
//case 'update':

//case 'delete':

I now for sure that it is getting the $id and sends it to the cart.php (it has some othere problems but I don't want to get into that).

 

My question is, how can I join the (only the case 'add' I need):

$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
//MY add code that I will post below
}

with my add-to-cart code (and most likely make it work):

if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    // RUN IF THE CART IS EMPTY OR NOT SET
	$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
	// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
	foreach ($_SESSION["cart_array"] as $each_item) {
	      $i++;
	      foreach($array as $key => $value) {
			  if ($key == "item_id" && $value == $pid) {
				  // That item is in cart already so let's adjust its quantity using array_splice()
				  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
				  $wasFound = true;
			  } // close if condition
	      } // close while loop
       } // close foreach loop
	   if ($wasFound == false) {
		   array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
	   }
}
    exit();
}

Thank you

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • 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.