PHP Comparing URL Text

8 replies
Hi,

Im trying to get a webpage to show an image depending on which keyword the user had used to get to my page. Im doing a PPC click campaign.

For example

When a user clicks my ad they got through the url

http://mysite.co.uk/?keyword={keyword}

Now what I need to do is display an image that is relevent to that keyword on my page.

e.g

http://mysite.co.uk/?keyword=shetland pony

Now I want the code to grab "shetland pony" and display a picture of one that I will have stored in the wordpress library.

But the code needs to be robust enough to understand it needs to look for that word anywhere.

eg

http://mysite.co.uk/?keyword=scottish shetland pony

Should also bring out the pony picture. If none of the keywords match my IF statements it should show nothing at all.

Hope that makes sense
#comparing #php #text #url
  • Profile picture of the author Brandon Tanner
    I haven't tested it but I believe something like this might do the trick...

    <?php

    $keyword = $_GET["keyword"];

    if (stristr($keyword,"pony"))
    {$image = "pony.jpg";}

    elseif (stristr($keyword,"horse"))
    {$image = "horse.jpg";}

    elseif (stristr($keyword,"mule"))
    {$image = "mule.jpg";}

    else {$image = "";}

    ?>

    <!-- HTML below is where image should appear -->

    <img src="<?php echo $image; ?>" />
    Signature

    {{ DiscussionBoard.errors[3507433].message }}
    • Profile picture of the author marine1983
      Great so I could load that into a php plugin on wordpress and then place short code to where I want the picture to be?

      I will give that a go. Thanks for your help
      Signature

      {{ DiscussionBoard.errors[3507629].message }}
      • Profile picture of the author Brandon Tanner
        Originally Posted by marine1983 View Post

        Great so I could load that into a php plugin on wordpress and then place short code to where I want the picture to be?

        I will give that a go. Thanks for your help
        I never use WordPress's interface for uploading or editing files, so I couldn't tell ya (I use FTP for everything).

        But I just thought of one other thing... some browsers will show a "no image" icon if you don't specify an actual image in the HTML image tag. So it might be best to make a 1 pixel image the same color as the background (and call it "blank.jpg") and then use that for the last "else" statement in the php code.

        For example...

        else {$image = "blank.jpg";}

        Also, the code in my original post assumes that the images are in the same directory. If they are in a different directory you would have to alter the code to reflect that. Or, just use an absolute URL for the img src tag...

        <img src="http://www.YourWebsite.com/images/<?php echo $image; ?>" />
        Signature

        {{ DiscussionBoard.errors[3507912].message }}
        • Profile picture of the author Tashi Mortier
          Okay, I'm trying to post some code samples here but WarriorForum no likey.

          I did some improvements on Brandon's Code, you can look at it here:

          quakenet:#php - nopaste #467629> -- Tashi was too lazy to write a description.

          This way the img tag will only be printed if there is an image and by using / as the first part of the url you are automatically telling the browser to use the root directory of the web server. That way you are not bound to a certain domain and everything remains portable.
          Signature

          Want to read my personal blog? Tashi Mortier

          {{ DiscussionBoard.errors[3508410].message }}
  • Profile picture of the author marine1983
    Thank you for taking the time to help me out. Much appreciated.
    Signature

    {{ DiscussionBoard.errors[3529118].message }}
    • Profile picture of the author marine1983
      Hi,

      I have tried your above codes. To be honest Im completely lost. Where is this image folder. Do I need to manually create the folder in the public root of my website domain?

      Im using wordpress as my CMS. And using a plugin to parse the PHP code through a shortcode command.

      I cant for the life of me get an image to display
      Signature

      {{ DiscussionBoard.errors[3577424].message }}
      • Profile picture of the author SteveJohnson
        What exactly do you mean by this?

        Originally Posted by marine1983 View Post

        ... using a plugin to parse the PHP code through a shortcode command.
        Shortcodes are intended to go within a post.

        So you've written a shortcode handler, housed in a plugin, that catches the $_GET value, supposedly finds the image, then spits the image tag?

        First thing to check is that your code is actually working. Instead of echoing the image tag, echo the $_GET value wrapped in an <h1> tag. It should appear within your post.

        If that works, then you need to find your images. Where did you upload them to? If you used the media uploader, the image path will be something like '/wp-content/uploads/2011/03/' assuming you uploaded them in March.

        Why don't you post your plugin code somewhere that we can look at it. Might shed a little light on what's actually going on.
        Signature

        The 2nd Amendment, 1789 - The Original Homeland Security.

        Gun control means never having to say, "I missed you."

        {{ DiscussionBoard.errors[3578321].message }}
        • Profile picture of the author marine1983
          Thanks for taking the time to reply. I have managed to make it work. PHP is so alien to me lol Im very new and having a hard time understanding it.
          Signature

          {{ DiscussionBoard.errors[3583330].message }}

Trending Topics