1

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?

5
  • 1
    What exactly do you have a problem with? Creating the url, the dropbox or the relation between the url and the image? Commented Sep 12, 2009 at 21:37
  • Well, basically is it possible to assign a URL in a var? Commented Sep 12, 2009 at 21:40
  • What do you mean by assign? How to redirect to a url? Commented Sep 12, 2009 at 21:41
  • can you have var_url_one = domain.com ?? Commented Sep 12, 2009 at 21:43
  • Yes, you can assign a URL value to a variable... But you have to use the proper syntax: $url_one = "domain.com"; Commented Sep 12, 2009 at 22:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Your above code allows the user to select one of the array options? Then in the image code I would just put in a code to grab the array the end-user just selected?
The script above simply populates a dropdown menu with the values in the array. If you want to do more with it, you'll have to tell me what you're trying to do with the image... Are you trying to link the image to the URL, or what?
By reading your comment to the above answer, you'll need to store this information in a database. The best thing to do would be to have the URLs in a "urls" table in the database. Then you'd assign the id column of that table to be a primary key. In another table with the images, you could assign the image to a particular URL by JOIN'ing it with the urls table and assigning it to the ID of whatever URL you want the image to link to. Then you'd have to output that information from the database into anchor/image tags in your HTML. If you're a beginner, this isn't going to be an easy task.
If you need help developing the database, connectivity, and output, I can give you my contact information and help you out with that.
yes link image to url. i am trying to set up an 'admin' area where they do all of this, then on the actual site it brings up using php inlcude their data. so i would like for them to simply select an option from a drop down menu which would be a variable of a url
|
1

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>

5 Comments

sorry i am new to PHP. Basically the user will upload an image, however that image needs to point to a url of the online shop catalog. SO, I need to store 100 URL 'options' so when they upload an image they can assign an url to it. So I guess I can assign 100 variables to 100 different urls?
I'm sorry, but I still don't quite understand what you actually want to do. Assign a url to the image? What do you mean by that? Your image will be stored at a certain path in the file system and that determines its url. Save the path of the image in the database and you're done.
No, I belive he wants the users to select an image and hyperlink it to a pre-determined URL which would be in the dropdown list.
Can you verify that this is what you want zach?
@Andre - let me get cracking on the code and see if I can make it work :) Thank you so much for your help!