tags:

views:

45

answers:

3

I want to know the reasons why this variable is passing empty.

<form action="cart.php" method="POST">
<input style="width:10px; margin-left:9px; " name="price[]" type="checkbox" value="' . $variety['price'].'_'. $variety['variety']. '_'. $product['name'] . ' "  /></form>

Can you see $product['name'] how can I print the it's value after extracting it's values in cart.php as

extracts values

list($aDoor, $variety,$productname) = split('_', $_POST['price']);
$aDoor = array();
$variety = array();
$productname= array();

foreach ($_POST['price'] as $p)
{
  list($a, $b,$c) = explode('_', $p);
  $aDoor[] = $a;
  $variety[] = $b;
  $productname[] = $c;
} 

Now below the foreach loop how can I echo the print of productname once..?

A: 

Now below the foreach loop how can I echo the print of productname once..?

print_r ($productname);

But if you want to see each value of product name inside the loop:

foreach ($_POST['price'] as $p)
{
  list($a, $b,$c) = explode('_', $p);
  $aDoor[] = $a;
  $variety[] = $b;
  $productname[] = $c;

  echo $c . '<br />'; // show product name
} 

Also, i don't see your echoing this line of code with php:

<input style="width:10px; margin-left:9px; " name="price[]" type="checkbox" value="' . $variety['price'].'_'. $variety['variety']. '_'. $product['name'] . ' "  /></form>

in which case this is not how to show value in above line:

value="' . $variety['price'].'_'. $variety['variety']. '_'. $product['name'] . ' "

instead you need to wrap it in php tags:

value="<?=$variety['price'].'_'. $variety['variety']. '_'. $product['name']?>"
Sarfraz
it is not echoing here but i have it in the real code thank you very much I will try print_r($productname) and echo $c;
jona
jona
Solution What I did was to add another form input tag and send it "hidden" then assign the $product['name'] to the value of the input hidden tag. Then in cart.php received it like $productname= ['name']; then print(#productname); and solution.
jona
@jona: how about creating a new textbox or hidden field for it, then it will be there only once?
Sarfraz
that what I did, i put it hidden and then now it prints out once and the varieties will print according to users selection.
jona
Thank you Sarfraz!
jona
@jona: that is great news and thank you !!!
Sarfraz
+1  A: 
foreach($productname as $name) {
  echo $name . '<br />';
}

or if you want to associate the product names with their other values in $aDoor and $variety you could do:

foreach($productname as $index => $name) {
  echo 'Name: ' . $name . '<br />';
  echo 'Variety: ' . $variety[$index] . '<br />';
  echo 'Price: ' . $aDoor[$index] . '<br />';
}

EDIT:

If I can take your comment to mean that all of the names are the same in the $productname array then you can do this instead:

if(count($productname) > 0) {
  echo 'Product Name: ' . $productname[0] . '<br />';
  foreach($variety as $index => $name) {
    echo $name . ': $' . $aDoor[$index] . '<br />';
  }
}
Cryo
he said below the foreach loop....
Sarfraz
@Sarfraz True. I took that to mean outside. If he meant inside then you've got it so we've got it covered both ways.
Cryo
@Cryo: i think you are right :)
Sarfraz
That code is fine above thanks! The only thing guys is that if there are two varietis there will be two prices. The way I have it set up is one name for two or three variety and right now it is displaying the same name for one variety and its price and the same name for the other variety. I maybe didn't explain myself good. It is display name one time and the rest of the varieties that the user chooses. hope i have explain myself now. the code above is fine but with name outside and printing one time.
jona
and then the rest of the varieties like:Air jordanwoman: $50Men: $30kids: $15Air jordan is the name and the varieties and price below.
jona
@jona I updated my answer to what I think would solve your problem, let me know.
Cryo
A: 

As I stated in another of your questions, you cannot use split() on an array. You're forcing PHP to treat $_POST['price'] as an array by naming it "price[]" in your form. WHen you split on an array:

$arr = array('a', 'b', 'c');
list($a, $b, $c) = split($arr);

you end up with the following:

$a = 'Array';
$b = NULL;
$c = NULL;

You will have to do the following:

list($aDoor, $variety,$productname) = explode('_', $_POST['price'][0]);
                                                                  ^^^   (note the array notation)

You're also not creating a $product variable in your extractions cript. You are creating a $productname array, but $productname is not the same as $product['name']

Marc B