Web Hosting Talk







View Full Version : [PHP] extract information from url.


-Edward-
09-12-2007, 08:35 PM
Hi Folks,

Something is bugging me and I cannot seem to figure out (It's probably very easy but it's bugging me).

Take the following example:

url.php?gap=TNFXX

I want to grab the TNF from the URL and show a picture.

I've got various 3 letter combinations stored in an Array and if for example that 3 letter combination is in the URL it shows that image on that page.

Another example:

url.php?gap=FNFXX

If FNF is present it shows image 1.

url.php?gap=SMSXX

If SMS is present it shows image 2.

Can anyone help me? I've tried using str_replace but that doesn't seem to work for me once I use an if statement to jump between ?gap= and ?step=.

Hope that makes sense ive been looking at this for over an hour and cannot come up with any suitable code that works.

-Edward-
09-12-2007, 08:48 PM
A little bit more info..

This is my Array:

$pimages = array
(
"TNF" => "images/tnf.gif",
"SMS" => "images/sms.gif",
);


This is my if statement:

if(strlen($images[$dept]) > 0)
{
echo $images[$dept];
}
elseif ($pimages[$product_code])
{
echo $pimages[$product_code];
}
else
{
echo "$default_image";

}

Bangalore Job Mob
09-12-2007, 08:56 PM
http://www.php.net/manual/en/function.substr.php

Burhan
09-12-2007, 08:59 PM
$gap = $_GET['gap'];
$pimages = array
(
"TNF" => "images/tnf.gif",
"SMS" => "images/sms.gif",
);
$img = @$pimages[substr($gap,0,3)] ? $pimages[substr($gap,0,3)] : 'images/default.gif';

echo '<img src="'.$img.'" alt="image" />';

-Edward-
09-12-2007, 09:02 PM
http://www.php.net/manual/en/function.substr.php

Thank you! I knew it was going to be easy!

-Edward-
09-12-2007, 09:03 PM
$gap = $_GET['gap'];
$pimages = array
(
"TNF" => "images/tnf.gif",
"SMS" => "images/sms.gif",
);
$img = @$pimages[substr($gap,0,3)] ? $pimages[substr($gap,0,3)] : 'images/default.gif';

echo '<img src="'.$img.'" alt="image" />';


Thanks, I might try that as well.

However Using the link posted above your post I did:

$product_code = substr($_GET['product_code'], 0, 3);

and that did the trick for me.

worldveil
09-12-2007, 11:00 PM
yes, you have to use the substr there very important