问题描述:

I am looking for the php "header" code and in-html-body-phpsnippet which will look for an exact string such as "redwidget" or "bluewidget" "greenwidget" etc in the address bar, and modify the output of the in-body <phpsnippet> based on which of those strings is there in the address bar

... in other words, I can define a list of parameters to look for in the address bar, and the <phpsnippet> will modify the in-body html output based on which it finds

ALSO if it finds none of them, there is a default or "fallback" output

What would the header script and also the in-body be?

My goal is to change the image displayed on a page based on which parameter it finds in the address bar

***Begin Example listing of outputs:

exact string "redwidgets" found in address bar -> <phpsnippet> outputs "/images/redwidget.jpg"

or exact string "greengoblin" found in address bar -> <phpsnippet> outputs "/images/greengoblin.jpg"

or exact string "bluewidgets" found in address bar -> <phpsnippet> outputs "/images/bluesteel.jpg"

or None of the above exact strings found in address bar -<phpsnippet> outputs "/images/defaultplaceholderimagethingy.jpg"

***End example

Thanks in advance!

网友答案:
<?php
$validKeywords = array("a", "b");
//ternary if/else: condition ? this if true : otherwise this;
$keyword = isset($_GET['keyword']) && in_array($_GET['keyword'], $validKeywords) ? $_GET['keyword'] : "defaultplaceholderimagethingy";
//format the output with sprintf and store it for later use
$output = sprintf("<img src='images/%s' alt='%s' />", $keyword.".jpg", "descriptive text");
print $output;
?>

Hope this helps.

网友答案:

Why don't you use GET parameters?

http://myurl/?phpsnippet=redwidget

In your code:

if (isset($_GET['phpsnippet']) {
    // use $_GET['phpsnippet'] which, in this case, equals 'redwidget'
} else {
    // display default
}
网友答案:

Ok I ALMOST figured this one out...

Here is my header code:

if (isset($_GET['keyword'])) {
    $keyword = $_GET['keyword'];

}

And my in-body html/php which replaces the image based on the keyword parameter: <img alt="" src="http://takeherhometonight.net/wp-content/uploads/<?php echo $keyword; ?>.jpg" border="0" width="300" height="314" />

The only thing I can't get with the above is a DEFAULT image, should a keyword parameter not be present.... any ideas?

相关阅读:
Top