Ok let me first start off by saying, whoever wrote that thing should
be drug out in the street and shot.
BTW I'm not going to fix your script, you are
But I'm going to write in the fixes in such a way as that you learn
learn a little PHP,
rather than just copy/paste it, but the fixes to apply will hopefully
be obvious so bear with me
Since you appear to have no idea how this script functions, why don't
we start from scratch and see what happens.
The script starts out with HTML, I'm not a big fan of starting my
scripts the way that this thing is, so lets begin at the beginning.
<?php
/*<?php begins a PHP script, it's a matter of prefference but I try to
only use one call to the preprocessor.
Once my data is ready for output, I try to do it all in one fell swoop
like this
echo <<< EOF
This is my data and I'm proud of it, I can use $Strings and other
$Variables as well as all HTML
About the only thing I can't do inside of this sort of echo is a
function call
EOF;
Now we need to retrieve all of our variables
$basketvalues = array_values($HTTP_POST_VARS);
$basketkeys = array_keys($HTTP_POST_VARS);
//Are wrong the method is deprecated and no longer used
The proper method is
$x=0;
while(list($key,$value)=explode($_POST)){
$basketvalues[$x] = $value;
$basketkeys[$x] = $key;
/*Also looking forward we can see we will be posting this information
to a form, rather than duplicate our efforts, lets make a part of that
form right now.*/
$form .="<input type = \"hidden\" name=\"$key\" value=\"$value\">";
$x++; //Finally increment x
}
/*Alot of this was a little bit pointless by the way because the
simplest thing would be to just use extract, since we really don't
need to create the arrays basketvalues and basketkeys, so to prove a
point I will put in the following line, even though I usually consider
it sloppy, but for berevity we will use it anyways*/
extract($_POST);
//And unless you are offering items without an associated cost
$totalitems = count($cost);
/* Those two lines replace ALL of the code contained in this comment,
as well as negating all the other calls to BasketValues and BasketKeys
$totalitems=((count($basketkeys))-17)/11;
$instId = $basketvalues[array_search("instId", $basketkeys, true)];
$cartId = $basketvalues[array_search("cartId", $basketkeys, true)];
$cost = $basketvalues[array_search("cost", $basketkeys, true)];
$currency = $basketvalues[array_search("currency", $basketkeys,
true)];
$desc = $basketvalues[array_search("desc", $basketkeys, true)];
$country = $basketvalues[array_search("country", $basketkeys, true)];
$name = $basketvalues[array_search("name", $basketkeys, true)];
$address = $basketvalues[array_search("address", $basketkeys, true)];
$postcode = $basketvalues[array_search("postcode", $basketkeys,
true)];
$tel = $basketvalues[array_search("tel", $basketkeys, true)];
$email = $basketvalues[array_search("email", $basketkeys, true)];
And so now we have all of our variables*/
/*Next we want to do all of our math operations BEFORE we move on to
form creation*/
for ($n=0;$n<$totalitems;$n++){
$ss = $n+1;
$sss = "total$ss";
$message .= "<td>".$basketvalues[array_search($sss $basketkeys,true)]."</td>";
$message .= "</tr>";
$message .= "\n";
}
/*The above code should gives me nausea looking at it, so what do we
do instead?
Well the first question is to ask what it's doing in the first place.
It's a simple for loop, $n is a dummy counter and as long as it is
less than $totalitems,
it assigns $ss to the value of $n+1
then it creates a new string called $sss (gotta love these descriptive
variables)
next it appends $basketvalues[array_search($sss, $basketkeys)]
to $message in a new HTML column.
Figuring this out will make you a basket case, but lets break it down
even further.
According to PHP.net
array_search: Searches the array for a given value and returns the
corresponding key if successful.
PROTOTYPE: mixed array_search ( mixed needle, array haystack [, bool
strict])
Simplified: Searches haystack for needle and returns the key if it is
found in the array, FALSE otherwise.
So we are searching basketkeys for a key named total$ss with $ss being
the next number we would be counting.
This means on the first iteration of the loop we will be looking in
basketkeys for a key named total1
on the next, we will be looking for total2 etc and so forth.
Since these values are sent to us via post method,
this points to a serious flaw either in this script or the one calling
it.
This can't be right, it seems so counter-intuitive.
But, considering the rest of this script lets go forth with the
assumption the author was correct
and the calling script really IS passing us values with names like
total1, total2 etc.
This is what you should do instead
*/
for ($counter=0;$counter<=$totalitems;$counter++){
/*I am going to make a leap of faith here and assume that $cost is
what the author was orginally intending to return
also there is appearantly no grand total in the code but I will put
one here anyways just so we know*/
$message .="<tr><td>$cost[$counter]</td></tr>";
$Total +=$cost[$counter];
}
$message .="<tr><td>Grand Total :$Total</td></tr>";
Alright that should be enough of a rewrite to get you started. And
your fix is contained in here
I know you probably thought that I was going to rewrite the whole
script
But I'm getting tired, now and I can't guarantee bug free code. Also
the remaining mods to the script, are mostly a
matter of programmers choice.
However here is what I have found is the best way to write, analyze,
rewrite code to keep it easily maintained and understood.
First off decide what you WANT to do. (Obviously, but sometimes it is
forgotten)
Secondly make sure that you set up your data prior to using it.
Thirdly make sure your data is what you expect.
Finally Use your data to the best effect possible.
All of this usually follows the pattern.
Open Script,
Cull data from available sources
Analyze data (Modify,Add, Subtract, Mulitply to / from data)
Utilize Data (This is where you output to screen, mail,
database,whatever)
Close Script
Also make sure you follow best practices when coding, by making sure
your code is well commented and as simple as you can make it.
(not as comment much as my examples but you see the picture)
and also that ALL of your variables are descriptive, 4 letters or more
is my preffered choice.
Well Good Night folks I'm going to bed, please feel free to point out
any mistakes I may have made
With the exception of course of the open and close comments, I'm well
aware my commenting is overkill and would also trigger parse errors
"pee2pee" <no*********@you.com> wrote in message news:<3f*********************@news.dial.pipex.com> ...
Hi,
I have below code:
<html>
<head>
<title>Contacting Worldpay, Please wait.......</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body text="#CCCCCC">
<?php
$basketvalues = array_values($HTTP_POST_VARS);
$basketkeys = array_keys($HTTP_POST_VARS);
$totalitems=((count($basketkeys))-17)/11;
$instId = $basketvalues[array_search("instId", $basketkeys, true)];
$cartId = $basketvalues[array_search("cartId", $basketkeys, true)];
$cost = $basketvalues[array_search("cost", $basketkeys, true)];
$currency = $basketvalues[array_search("currency", $basketkeys, true)];
$desc = $basketvalues[array_search("desc", $basketkeys, true)];
$country = $basketvalues[array_search("country", $basketkeys, true)];
$name = $basketvalues[array_search("name", $basketkeys, true)];
$address = $basketvalues[array_search("address", $basketkeys, true)];
$postcode = $basketvalues[array_search("postcode", $basketkeys, true)];
$tel = $basketvalues[array_search("tel", $basketkeys, true)];
$email = $basketvalues[array_search("email", $basketkeys, true)];
?>
<form name='worldform1' method='post'
action='https://select.worldpay.com/wcc/transaction'>
<input type="hidden" name="instId" value="<?php echo $instId ?>">
<input type="hidden" name="cartId" value="1234">
<input type="hidden" name="cost" value="<?php echo $cost ?>">
<input type="hidden" name="currency" value="<?php echo $currency ?>">
<input type="hidden" name="desc" value="<?php echo $desc ?>">
<input type="hidden" name="country" value="<?php echo $country ?>">
<input type="hidden" name="name" value="<?php echo $name ?>">
<input type="hidden" name="address" value="<?php echo $address ?>">
<input type="hidden" name="postcode" value="<?php echo $postcode ?>">
<input type="hidden" name="county" value="<?php echo $county ?>">
<input type="hidden" name="tel" value="<?php echo $phone ?>">
<input type="hidden" name="email" value="<?php echo $email ?>">
<input type="hidden" name="testMode" value="101">
</form>
<font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<script language="Javascript">
document.worldform1.submit();
</script>
<br><br>Contacting WorlPay Secure Payment Server<br>
Please Wait ....</font>
<?php
echo "Total Items: $totalitems <br>";
$toadd="ma**@mail.com";
$subject="Subject";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: ma**@mail.com\nReply-To: ma**@mail.com\n";
$message ="<html>
<head>
<title>Carpet Runners</title>
</head>
<body>
Cart ID $cartId<br>
Cost $cost<br>
Currency $currency<br>
<br>
Invoice Name $name<br>
Invoice Address $address<br>
Invoice Postcode $postcode<br>
Invoice Email $email<br>
<br>
Delivery Name $delvName<br>
Delivery Address $delvAddress<br>
Delivery Postcode $delvPostcode<br>
Delivery E-Mail $delvEmail<br>
Delivery Telephone $delvPhone<br>
<br><br>
<table>";
$message .= "<tr><td>Total</td></tr>\n";
$message .= "<tr>";
for ($n=0;$n<$totalitems;$n++){
$ss = $n+1;
$sss = "total$ss";
$message .= "<td>".$basketvalues[array_search($sss, $basketkeys,
true)]."</td>";
$message .= "</tr>";
$message .= "\n";
}
$message .= "</table></body></html>";
echo $message;
?>
<?php
if (mail($toadd, $subject, $message, $headers)){
echo "Thankyou ".$dealname."<br>";
echo "Mail has been sent to Stairrods USA with your order<br>";
}
else
{
echo "Mail send failure.<br>";
}
?>
</body>
</html>
I get the e-mails, but the total isn't being shown. I believe:
$message .= "<td>".$basketvalues[array_search($sss, $basketkeys,
true)]."</td>";
is the code that displays the total, but it doesn't show up in the e-mail.
The top part works i.e. everything up to the one line of code above, then
nothing is displayed.
Please help, and many thanks ina dvance.
pee2pee
-----------------------------
http://janusz.monkey-it.co.uk