Jump to content

Give unique values inside dynamicList


Pavlos1316

Recommended Posts

Hello,

 

I am using this dynamicList to display items in my page:

$sql = mysql_query("SELECT * FROM products WHERE category='body' ORDER BY id ASC");
$productCount = mysql_num_rows($sql);
// count the output amount
if ($productCount > 0) {
    $i=0;
	$dynamicListBody = '<table 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" name="bd_itm" method="post" action="">
				<input type="hidden" name="pid" id="pid" value="'. $id . '" />
				<input type="submit" name="button" id="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();
?>

How can I assign unique id & name values to my button code so it will be unique for each item???? I need it unique because I am calling that pid in my add.To.Cart.function after and is not working as is.

<input type="hidden" name="pid" id="pid" value="'. $id . '" />
<input type="submit" name="button" id="button" value="Add to Cart" />

 

Thank you

Link to comment
Share on other sites

  • Replies 72
  • Created
  • Last Reply

not working as it is.... No error massages... If I press the first add to cart button... It Works.

 

If I press any other, it sends me back to my homepage with no erros....

 

oh... I use this jquery so my page won't refresh or send me to cart.php when I click AddToCart. For this I need unique ids I think:

$('#button').click(function() {
    $.post('help_scripts/cart_functions.php', {
     pid : $('#pid').val()
    }, function(rsp) {
       // make your php script return some xml or json that gives the result
       // rsp will be the response
     });
        return false;
        // so the page doesn't POST
});

Link to comment
Share on other sites

You're going to have to swap name and value so the unique id is for the name and pin is the value.  On the pick up side of your post you will need to use

a foreach statement to pick up the $key value.  Something like this.

foreach($_POST as $key => $val){
//DO SOMETHING WITH THIS $key ID//
}

Link to comment
Share on other sites

You're going to have $_POST['button'] as well so you might qualify $key before doing anything with it.  I'm sure more qualified coders will have a more efficient code but try this.

foreach($_POST as $key => $val){
IF (is_int($key)){
//DO SOMETHING WITH THIS $key ID//
}
}

Link to comment
Share on other sites

Hello,

On the pick up side of your post

You mean I include my whole add-to-cart.code (below) inside that foreach.code?

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++;
	      while (list($key, $value) = each($each_item)) {
			  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();
}

Link to comment
Share on other sites

another thing you can try is to see if the id's are attaching themselves like you want

 

for ex

 

<input type="hidden" name="pid" id="pid_<?php echo $id?>" value="'. $id . '" title="pid_<?php echo $id?>" />

 

would allow you to hover over it and see the id

 

if the id's are correct i would assume the problem is somewhere else

 

once your id's are correct and unique its a matter of detecting the click

 

im not sure what your using but when I do it in jquery i use the live function for dynamic tables

Link to comment
Share on other sites

i see your using jquery you can adapt this example, the jquery you showed in the 2nd post will not detect a dynamic list

 

<script>
$('#yourtablename input[id^=pid_]:not(.ui-pid_)').live("click",function(){ 

var idData = $(this).attr('id');
var id_data=idData.split('_');
$("#voucher_id").val(id_data[1]);
// do something

});
</script>	

Link to comment
Share on other sites

this can also be used

 

$('#yourtablename input[id^=pid_]:not(.ui-pid_)').live("click",function(){

 

$('#yourtablename input[name^=pid_]:not(.ui-pid_)').live("click",function(){

 

$('#yourtablename select[id^=pid_]:not(.ui-pid_)').live("click",function(){

 

depending on if you use a id, name or <select > dropdown list

Link to comment
Share on other sites

Hey forgive me Pavlos for not reading your code carefully enough.  I thought you were posting several items at a time.  Please disregard my earlier posts.  Just looking at your first code, I get "white space errors" because of the line returns.  I removed those returns and formatted in a style I would use.  Maybe this will work for you.

$sql = mysql_query("SELECT * FROM products WHERE category='body' ORDER BY id ASC");
$productCount = mysql_num_rows($sql);
// count the output amount
if ($productCount > 0) {
    $i=0;
	$dynamicListBody = "<table 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\" name=\"bd_itm\" method=\"post\" action=\"#\"><input type=\"hidden\" name=\"pid\" value=\"$id\" /><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();

Link to comment
Share on other sites

I will shoot myself...

I will shoot myself...

I will shoot myself...

I will shoot myself...

I will shoot myself...

 

I was testing what you and gevensen telling me to..... but I just noticed that I was testing without proccesing my form...

<form id="bd_itm" name="bd_itm" method="post" action=""> I must of left the action empty in one of my copy/paste.... Goooooood....

 

I have to test what you and gevesen advised me, all over again!!!!!!!  :'(  :'( stupid... stupid... stupid... me!!!!

Link to comment
Share on other sites

I feel pretty bad about my posts as well.  The white space error was my fault as I had placed your code inside a page I already had to access a DB table already defined.  I messed up.  You do have empty <span class=itmttl></span> and duplicate id="bd_itm", id="pid" and id="button" for each item and the form action is not defined.  But as far as the original issue of id not being passed, that seems fine.

Link to comment
Share on other sites

When posting, is this value not being picked up?

According to this:

if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];

yes pid is posting...

 

In both versions I see the pid value for each item

Both versions????

 

your first though, (even if you say that it was for several item at a time) seems more logic to me.... Which part of my code should I put inside //DO SOMETHING WITH THIS $key ID// so I test it again:

foreach($_POST as $key => $val){
IF (is_int($key)){
//DO SOMETHING WITH THIS $key ID//
}
}

You do have empty <span class=itmttl></span>

This I don't see... I think I don't have it...

duplicate id="bd_itm", id="pid" and id="button"

Now this is what I am trying to change :) To get unique values in case this is my error...

Link to comment
Share on other sites

duplicate id="bd_itm", id="pid" and id="button"

Now this is what I am trying to change :) To get unique values in case this is my error...

I don't think you need these html id's at all.  You just need to process the $_POST['pid'] and add it to the cart.

Link to comment
Share on other sites

I will put everything in an order (because I messed up this post) :) Sorry if I stunned you.

 

I have my:

 

1. dynamic list

Using it to display my items plus my ADD BUTTON for each item:

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

 

2. add-to-cart function

This is clear.

 

3. jquery

Using it to be able to add an item without having the page being refreshed or sending me to the cart.php

 

The problem...

The first item in my list is added in the cart normaly with no refreshing etc...

Any other item will also get added to the cart BUT resulting to a Blank Page MY PROBLEM.

 

Now the most likely cause for this (as I suspect), is that I am not rendering my button (and maybe my form also) with Unique ID.

[or not? I don't know any more]

*If you like I can repost the codes also (in an order this time)

Link to comment
Share on other sites

Well I don't know much about jquery but if you need the form id="bd_itm" to process, then you'll need to have a unique value.  By just adding $id to the end should give you a unique id for each as in id="bd_itm'. $id . '" though not sure if that will be what you need.  Personally I'd just do a straight post to a processing page and direct them back to the product page, which will re-query and show items in their cart.

Link to comment
Share on other sites

Well I don't know much about jquery but if you need the form id="bd_itm" to process, then you'll need to have a unique value.  By just adding $id to the end should give you a unique id for each as in id="bd_itm'. $id . '" though not sure if that will be what you need.  Personally I'd just do a straight post to a processing page and direct them back to the product page, which will re-query and show items in their cart.

once you get past the what in the world am i doing jquery basically supercharges php, no more page refreshes unless you want to

Link to comment
Share on other sites

Drummin...

I will try the form id variation also... as for style... That I know :)

Hey well, sorry I wasn't much help.  This topic was here for days without anyone responding so I thought I see about it.  I will stay out and let those more qualified respond.

Don't say that... Any try to help is appreciated!!!!

Nobody knows everything... Thats why we ask, and we play around with the codes.

 

gevensen...

since you seem to know quite a lot about jquery, where do you think is my promblem?

 

I will try again what you previwsly posted (if you read few posts ago I didn't proccesing my form, when I was trying your solutions.)

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.