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
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 10:52 am
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 13, 2004 11:04 am
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)
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 11:10 am
$_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...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 13, 2004 11:31 am
can you post an example of the data contained in $_POST[$name]?
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 11:52 am
well some data includes:
carrots,
5sample,
3sample,
apples,
I was thinking perhaps the numbers were causing the probems!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 13, 2004 11:56 am
since you are using a comma already to seperate them, how about [php_man]explode[/php_man]() ?
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 12:36 pm
OK feyd I will give that a shot pal! Thanks!
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 1:00 pm
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!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 13, 2004 1:04 pm
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>";
}
}
?>
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 1:08 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 13, 2004 1:12 pm
the way the code is right now, will do a result for each row found in the database..
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 1:14 pm
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 13, 2004 1:17 pm
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>";
?>
Joe
Forum Regular
Posts: 939 Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow
Post
by Joe » Tue Jul 13, 2004 1:35 pm
feyd, you are the best. Everything works 100% perfect. Thank you sooo much!
Joe