j.silver Posted October 18, 2014 Share Posted October 18, 2014 Hi everyone. I'm trying to echo the values of an array inside HTML's <option> tag. The following should be echod: <option value="Moose Jaw,SK,Canada">Moose Jaw,SK,Canada</option> <option value="Prince Albert,SK,Canada">Prince Albert,SK,Canada</option> <option value="Regina,SK,Canada">Regina,SK,Canada</option> <option value="Saskatoon,SK,Canada">Saskatoon,SK,Canada</option> The code I attempted was: <!DOCTYPE html> <html> <body> <?php $sk=array("Saskatoon","Regina","Moose Jaw", "Prince Albert"); sort($sk); foreach ($sk as $value) { echo '<option value="'.$value.',SK,Canada">'.$value.',SK,Canada</option>'; echo "<br>"; } ?> </body> </html> Pls. note that the values of the array are not sorted alphabetically. The output, however, should be sorted in ascending format and be within the <option> tag after adding the province and country. Though the select element will be there in the final code, I ignored it here because I am able to manage it separately. I now want to do what I explained only, which I am unable to yet. Any help will be highly appreciated. Link to comment Share on other sites More sharing options...
niche Posted October 18, 2014 Share Posted October 18, 2014 (edited) That's what I see what are your developer tools saying? Edited October 18, 2014 by niche Link to comment Share on other sites More sharing options...
j.silver Posted October 18, 2014 Author Share Posted October 18, 2014 The minute I begin typing <option> tag, one array value disappears from the output. By the time the echo code line is complete, as written above, no values are printed, just a blank page. Link to comment Share on other sites More sharing options...
niche Posted October 18, 2014 Share Posted October 18, 2014 I just added the SELECT tag. It has 4 options in alpha order. <!DOCTYPE html><html><body><select><?php$sk=array("Saskatoon","Regina","Moose Jaw", "Prince Albert");sort($sk);foreach ($sk as $value) {echo '<option value="'.$value.',SK,Canada">'.$value.',SK,Canada</option>';echo "<br>";}//<option value="Moose Jaw,SK,Canada">Moose Jaw,SK,Canada</option>//<option value="Prince Albert,SK,Canada">Prince Albert,SK,Canada</option>//<option value="Regina,SK,Canada">Regina,SK,Canada</option>//<option value="Saskatoon,SK,Canada">Saskatoon,SK,Canada</option>?></select></body></html> Link to comment Share on other sites More sharing options...
j.silver Posted October 18, 2014 Author Share Posted October 18, 2014 Many thanks niche for the help and speedy response. Yes, it works fine when <select> is added. However, I am keen to get it work without the <select> tag for a reason too lengthy to address, hence not worth mentioning. I hope it is not impossible to achieve it without <select>, and any assistance will be highly appreciated. Link to comment Share on other sites More sharing options...
j.silver Posted October 18, 2014 Author Share Posted October 18, 2014 Another thing is that the above code (with select) makes the output in a drop down menu. The final output I am looking for should be a complete sorted lines of output (not in a drop down menu), exactly as: <option value="Moose Jaw,SK,Canada">Moose Jaw,SK,Canada</option><option value="Prince Albert,SK,Canada">Prince Albert,SK,Canada</option><option value="Regina,SK,Canada">Regina,SK,Canada</option><option value="Saskatoon,SK,Canada">Saskatoon,SK,Canada</option> Link to comment Share on other sites More sharing options...
niche Posted October 18, 2014 Share Posted October 18, 2014 sounds like a job for either radio buttons or check boxes depending on your situation http://www.w3schools.com/html/html_forms.asp Link to comment Share on other sites More sharing options...
j.silver Posted October 18, 2014 Author Share Posted October 18, 2014 Thanks again niche. It is not radio, nor check box. It is indeed a drop down menu, but not in its final shape. I am looking for a code that will produce the list of options as given earlier, i.e., a complete list of options sorted alphabetically and ready to be inserted within the select tags at a later stage. Now, I want an output in a complete list, sorted alphabetically exactly as given earlier. Link to comment Share on other sites More sharing options...
niche Posted October 18, 2014 Share Posted October 18, 2014 So, you want the drop down options without the drop down. Right? Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 Exactly. Link to comment Share on other sites More sharing options...
niche Posted October 19, 2014 Share Posted October 19, 2014 What does your form look like? Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 I am not working through a form niche. I am using a separate script (the one in my first post). This is an independent and stand-alone script. Link to comment Share on other sites More sharing options...
niche Posted October 19, 2014 Share Posted October 19, 2014 What to you want to do with those options? Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 I am having them separate in a stand-alone script to continue updating and modifying them in time and away from the final code. Once complete and in their final list of options, insert them within the select tags of the final code. You might ask: why then you don't include them within the select tags in the final code and keep adding to the final code. The answer is: because other future options will have different value type than their predecessors, so if I will add them to the same select tags it will mess up the whole list. Link to comment Share on other sites More sharing options...
Ingolme Posted October 19, 2014 Share Posted October 19, 2014 If you want it to update in real-time PHP's not going to do it. Send the data to Javascript and have Javascript work with it. You can sort data alphabetically using the sort() function. Link to comment Share on other sites More sharing options...
niche Posted October 19, 2014 Share Posted October 19, 2014 make your options dynamic so you don't have to wait for a complete list. Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 Many thanks Ingolme for your input. I never thought PHP might be limited in echoing such a result. I regret that I do not know JS. If you may help me with the code that you think would do the requirement, it would be appreciated and might be my first step towards learning JS. Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 Dear niche, I really thank you very much for the sincere efforts. I tried to make it dynamic, but couldn't work due to the different types of data. The only option left for me really is to make it a stand alone script, get it to sort the data separately, then only insert it within the final code. Link to comment Share on other sites More sharing options...
niche Posted October 19, 2014 Share Posted October 19, 2014 (edited) are you using a localhost lake wampserver that has a database? Edited October 19, 2014 by niche Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 Yes niche. Apache/2.4.7 (Win32) PHP/5.5.9 mysql 5.0.11 Link to comment Share on other sites More sharing options...
niche Posted October 19, 2014 Share Posted October 19, 2014 Why not base your inputs on a sql query? Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 Though I have not really thought of or tried to base my inputs on a sql query, I think it will not workas I want it to be because my end result should be a list of <options></options>, both tags inclusive. Link to comment Share on other sites More sharing options...
dsonesuk Posted October 19, 2014 Share Posted October 19, 2014 <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>show html on page using php</title> <style type="text/css"> </style> </head> <body> <?php $checked = ''; if (isset($_GET['pre_or_html']) && !empty($_GET['pre_or_html'])) { $checked = ' checked="checked"'; } ?> <form method="get"> <label>HTML select</label><input type="checkbox" <?php echo $checked; ?> name="pre_or_html" value="html"> <input type="submit"> </form> <?php $sk = array("Saskatoon", "Regina", "Moose Jaw", "Prince Albert"); sort($sk); $txt = ""; foreach ($sk as $value) { $txt.='<option value="' . $value . ',SK,Canada">' . $value . ',SK,Canada</option>' . "n"; } if (isset($_GET['pre_or_html']) && !empty($_GET['pre_or_html'])) { echo '<select>' . $txt . '</select>'; } else { echo '<pre>' . htmlspecialchars($txt) . '</pre>'; } ?> </body></html> Link to comment Share on other sites More sharing options...
dsonesuk Posted October 19, 2014 Share Posted October 19, 2014 For inputting values for options <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>show html on page using php</title> <style type="text/css"> </style> </head> <body> <?php $checked = ''; $sk = array("Saskatoon", "Regina", "Moose Jaw", "Prince Albert"); if (isset($_POST['currentArray']) && !empty($_POST['currentArray']) && count(unserialize($_POST['currentArray'])) > count($sk)) { $sk = unserialize($_POST['currentArray']); } if (isset($_POST['new_option']) && !empty($_POST['new_option']) && !in_array($_POST['new_option'], $sk)) { array_push($sk, $_POST['new_option']); } if (isset($_POST['pre_or_html']) && !empty($_POST['pre_or_html'])) { $checked = ' checked="checked"'; } $currentArray = $sk; echo count($currentArray); if (isset($_POST['currentArray'])) { echo '<br>values: '; print_r($currentArray); } sort($sk); ?> <form method="post"> <input type="hidden" value='<?php echo serialize($currentArray); ?>' name="currentArray"> <label>HTML select</label><input type="checkbox" <?php echo $checked; ?> name="pre_or_html" value="html"> <label>New option</label><input type="text" name="new_option" value=""> <input type="submit"> </form> <?php $txt = ""; foreach ($sk as $value) { $txt.='<option value="' . $value . ',SK,Canada">' . $value . ',SK,Canada</option>' . "n"; } if (isset($_POST['pre_or_html']) && !empty($_POST ['pre_or_html'])) { echo '<select>' . $txt . '</select>'; } else { echo '<pre>' . htmlspecialchars($txt) . '</pre>'; } ?> </body></html> Link to comment Share on other sites More sharing options...
j.silver Posted October 19, 2014 Author Share Posted October 19, 2014 Dear dsonesuk,Many thanks for a well-thought scripts. The only thing we might still need to nail down is making the form accept multiple entries. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now