[SOLVED] Array from $_POST[]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

[SOLVED] Array from $_POST[]

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

can you post an example of the data contained in $_POST[$name]?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

well some data includes:

carrots,
5sample,
3sample,
apples,

I was thinking perhaps the numbers were causing the probems!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since you are using a comma already to seperate them, how about [php_man]explode[/php_man]() ?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

OK feyd I will give that a shot pal! Thanks!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>"; 
  } 
}

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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).
Last edited by Joe on Tue Jul 13, 2004 1:13 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the way the code is right now, will do a result for each row found in the database..
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>"; 

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

feyd, you are the best. Everything works 100% perfect. Thank you sooo much!

Joe 8)
Post Reply