Page 1 of 1

[SOLVED] Array from $_POST[]

Posted: Tue Jul 13, 2004 10:52 am
by Joe
I am trying to create an array with the posted variables for the online shop that I am creating but I seem to be having problems. As you know an each array element must be seperated like array(1,1,1) but because I am posting I am unable to seperate. However I did try to seperate the elements by doing:

Code: Select all

$sql = "SELECT * FROM images WHERE tag = '0'";
$result = mysql_query($sql) or die("Error!");
$numrows = mysql_num_rows($result);
$x = 0;

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $name = $row['name'];
 $name1 = $_POST[$name];
 $seperation = ", ";

 if ($numrows == $x + 1) $seperation = "";

 $array = array($name1$seperation);
 $subtotal = array_sum($array);

 echo "<br><b><u>Payment Choice</b></u><br>";
 echo $_POST['payment']."<br>";
 echo "SubTotal: $".$subtotal."<br>";
 echo "Shipping: $".$_POST['shipping']."<br>";
 echo "Total: $".$subtotal + $_POST['shipping']."<br>";
 $x++;
}
But I just keep getting the error at this line:

$array = array($name1$seperation);

Saying that I require a closing ')'

Is there any other ways around this...

Thanks

Posted: Tue Jul 13, 2004 11:04 am
by feyd
it's complaining because 2 variables cannot be combined like that.. however, if you just straight combine them, you'll get a one element array containing a string.

What's is $_POST[$name] in this instance? (the data I mean)

Posted: Tue Jul 13, 2004 11:10 am
by Joe
$_POST[$name] in this instance feyd is the products in the database such as meat, carrots, etc...

I also tried:

Code: Select all

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $name = $row['name'];
 $name1 = $_POST[$name];

 if ($name1 > '0')
 {
  $array = array($name1,);
  $subtotal = array_sum($array);
  $total = $subtotal + $_POST['shipping'];

  echo $_POST['payment']."<br>";
  echo "SubTotal: $".$subtotal."<br>";
  echo "Shipping: $".$_POST['shipping']."<br>";
  echo "Total: $".$total."<br><br>";
 }
}
But I just seem to get three reciepts with the original prices and shipping costs but I wish to combine it into one reciept. I get three reciepts as I chose three products...

Posted: Tue Jul 13, 2004 11:31 am
by feyd
can you post an example of the data contained in $_POST[$name]?

Posted: Tue Jul 13, 2004 11:52 am
by Joe
well some data includes:

carrots,
5sample,
3sample,
apples,

I was thinking perhaps the numbers were causing the probems!

Posted: Tue Jul 13, 2004 11:56 am
by feyd
since you are using a comma already to seperate them, how about [php_man]explode[/php_man]() ?

Posted: Tue Jul 13, 2004 12:36 pm
by Joe
OK feyd I will give that a shot pal! Thanks!

Posted: Tue Jul 13, 2004 1:00 pm
by Joe
OK well im not to sure if I am doing this right. It just shows individual reciepts for each product chosen and it does not add all of the prices together just each individual price and shipping cost.

Code: Select all

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $name = $row['name'];
 $name1 = $_POST[$name];

 if ($name1 > '0')
 {
  $array = "$name1 ";
  $subtotal = explode(" ", $array);
  $subtotal1 = array_sum($subtotal);
  $total = $subtotal1 + $_POST['shipping'];

  echo $_POST['payment']."<br>";
  echo "SubTotal: $".$subtotal1."<br>";
  echo "Shipping: $".$_POST['shipping']."<br>";
  echo "Total: $".$total."<br><br>";
 }
}
Thanks!

Posted: Tue Jul 13, 2004 1:04 pm
by feyd
how about this instead:

Code: Select all

<?php

while ($row = mysql_fetch_assoc($result)) 
{ 
  $name = $row['name'];
  $name1 = $_POST[$name]; 

  if ($name1 > '0') 
  { 
    $products = preg_split('#,\s*#', $name1); 
    $subtotal = array_sum($products); 
    $total = $subtotal + $_POST['shipping']; 

    echo $_POST['payment']."<br>"; 
    echo "SubTotal: $".$subtotal."<br>"; 
    echo "Shipping: $".$_POST['shipping']."<br>"; 
    echo "Total: $".$total."<br><br>"; 
  } 
}

?>

Posted: Tue Jul 13, 2004 1:08 pm
by Joe
Again its showing individual reciepts, like:

Payment Details
Money Order
SubTotal: $897
Shipping: $9
Total: $906

Money Order
SubTotal: $875
Shipping: $9
Total: $884

I think this must be because I am creating a while loop and its reading each individual $_POST. (Im not sure on this one).

Posted: Tue Jul 13, 2004 1:12 pm
by feyd
the way the code is right now, will do a result for each row found in the database..

Posted: Tue Jul 13, 2004 1:14 pm
by Joe
Ahhh I just made an edit saying that, very strange indeed feyd, lol.

Would you recommend anything to me in the attempt to solve this?

Posted: Tue Jul 13, 2004 1:17 pm
by feyd
something like this:

Code: Select all

<?php 

$subtotal = 0;
while ($row = mysql_fetch_assoc($result)) 
{ 
  $name = $row['name']; 
  $name1 = $_POST[$name]; 

  if ($name1 > '0') 
  { 
    $products = preg_split('#,\s*#', $name1); 
    $subtotal += array_sum($products); 
  } 
} 

    $total = $subtotal + $_POST['shipping']; 

    echo $_POST['payment']."<br>"; 
    echo "SubTotal: $".$subtotal."<br>"; 
    echo "Shipping: $".$_POST['shipping']."<br>"; 
    echo "Total: $".$total."<br><br>"; 

?>

Posted: Tue Jul 13, 2004 1:35 pm
by Joe
feyd, you are the best. Everything works 100% perfect. Thank you sooo much!

Joe 8)