Web Hosting Talk







View Full Version : Help - I'm not a programmer!


jarchee
01-21-2010, 02:19 PM
Hi,

I'm just a beginner and have no clue of what I'm writing here. anyways I need help with this small php program.

I want to make this work.

I have four (4) probable values that want to replace them with 4 other values if one occurs. everytime there is a chance of occurring for one of them:

Free Shipping - Free Shipping
FedEx - Priority Overnight
FedEx - Ground
Canada Post (Recommended for residential users) - Canada Post

I want to say:
if the value is "Free Shipping - Free Shipping" then replace(change) it with "FREE SHIPPING", but if the value is "FedEx - Priority Overnight" replace(change) it with "FEDEX-OVERNIGHT", but if the value is "FedEx - Ground" replace(change) it with "FEDEX-GROUND", but if the value is "Canada Post (Recommended for residential users) - Canada Post" replace(change) it with "CANADA POST".

what I have done is:


$original = $order->getData('shipping_description');
if ($original = "Free Shipping - Free Shipping") {
$sp1 = substr_replace($original, "FREE SHIPPING", 0);
$value = $sp1;
} elseif ($original = "FedEx - Priority Overnight") {
$sp2 = substr_replace($original, "FEDEX-OVERNIGHT", 0);
$value = $sp2;
} elseif ($original = "FedEx - Ground") {
$sp3 = substr_replace($original, "FEDEX-GROUND", 0);
$value = $sp3;
} elseif ($original = "Canada Post (Recommended for residential users) - Canada Post") {
$sp4 = substr_replace($original, "CANADA POST", 0);
$value = $sp4;
}


but it returns "FREE SHIPPING" every time.

Please advise

linuxclark
01-21-2010, 03:04 PM
Hi,

I'm just a beginner and have no clue of what I'm writing here. anyways I need help with this small php program.

I want to make this work.

I have four (4) probable values that want to replace them with 4 other values if one occurs. everytime there is a chance of occurring for one of them:

Free Shipping - Free Shipping
FedEx - Priority Overnight
FedEx - Ground
Canada Post (Recommended for residential users) - Canada Post

I want to say:
if the value is "Free Shipping - Free Shipping" then replace(change) it with "FREE SHIPPING", but if the value is "FedEx - Priority Overnight" replace(change) it with "FEDEX-OVERNIGHT", but if the value is "FedEx - Ground" replace(change) it with "FEDEX-GROUND", but if the value is "Canada Post (Recommended for residential users) - Canada Post" replace(change) it with "CANADA POST".

what I have done is:


$original = $order->getData('shipping_description');
if ($original = "Free Shipping - Free Shipping") {
$sp1 = substr_replace($original, "FREE SHIPPING", 0);
$value = $sp1;
} elseif ($original = "FedEx - Priority Overnight") {
$sp2 = substr_replace($original, "FEDEX-OVERNIGHT", 0);
$value = $sp2;
} elseif ($original = "FedEx - Ground") {
$sp3 = substr_replace($original, "FEDEX-GROUND", 0);
$value = $sp3;
} elseif ($original = "Canada Post (Recommended for residential users) - Canada Post") {
$sp4 = substr_replace($original, "CANADA POST", 0);
$value = $sp4;
}
but it returns "FREE SHIPPING" every time.

Please advise

if ($original == "Free Shipping - Free Shipping") {
} elseif ($original == "FedEx - Priority Overnight") {

change = to ==

tim2718281
01-21-2010, 03:21 PM
The "=" sign is assignment.

So in your line

if ($original = "Free Shipping - Free Shipping") {

the value of $original is replaced with "Free Shipping - Free Shipping"

For the comparison test, "==" is required:

if ($original == "Free Shipping - Free Shipping") {

jarchee
01-21-2010, 03:51 PM
Yes Thanks,

"==" solved everything.

thank you

foobic
01-21-2010, 06:25 PM
On a more general note, take a look at the associative arrays examples on the manpage (http://au.php.net/manual/en/function.array.php). Your code snippet looks like it could benefit from using them.

mattle
01-22-2010, 08:02 AM
This is also a great case for using arrays with str_replace(). Read more here: http://php.net/manual/en/function.str-replace.php (3rd part of example 1)