zach
zach

Reputation:

URL as a Variable in PHP

I have a cool project where I need to upload an image via php/my_sql. That I can handle, but the images need to be linking to a certain url out of 100. In php can I save a url as a variable, then allow a drop-down menu of the 100 choices which point to a variable with a url?

Upvotes: 1

Views: 5155

Answers (2)

André Hoffmann
André Hoffmann

Reputation: 3553

I'm still not sure what you mean, but if you want to know how to store a url in a variable that is usually done in a string like this:

$url = "http://www.mysite.com/the/beautiful/image.gif";

You can also redirect to that url like this:

header('Location: '.$url);
die();

If you want the user to decide to which site to go, do it similar to what BraedenP posted:

<select id="urls" onchange="document.location.href=document.getElementById('urls').options[document.getElementById('urls').selectedIndex].value;">
<?php
$urls = array(
    'Image One'  => 'http://www.mysite.com/one.gif',
    'Image Two'  => 'http://www.mysite.com/two.gif',
    'Image Thee' => 'http://www.mysite.com/three.gif'
);
foreach($urls as $name=>$url){
    echo "<option value=\"{$url}\">{$name}</option>";
}
?>
</select>

Upvotes: 1

BraedenP
BraedenP

Reputation: 7215

The best choice would be to use an array:

$urls = array("url","url2","url3");

After you add all the 100 URLs in there, you can recurse through the array and output options into the tag.

<?php
echo "<select>";
foreach($urls as $current_url){
    echo "<option>" . $current_url . "</option>";
}
echo "</select>";
?>

That would go through the array, echoing all the URLs into the tag.

If you don't want to set the text in the dropdown to the actual URL, you could set the array using keys array("This URL" => "url") etc. and put the URL value into the "value" property of the tag, and using the key name as the value between the opening and closing tags of the list.

If you need an explanation of that as well, I can provide one.

Upvotes: 1

Related Questions