stevegingercat Posted June 20, 2022 Share Posted June 20, 2022 Hi ya! I'm building a drop down menu to select "cats" categories it's working except that the "cats" that have / contain multi words are printing out like this - <select name="site_search"> <option value="Pen" case="">Pen Case</option> <option value="news">news</option> <option value="List" of="" names="">List of names</option> <option value="Library">Library</option> </select> This is the mysql and php that I'm using to create the above $result = mysqli_query($conn,"select distinct cats from URLS order by cats desc"); $total = mysqli_num_rows($result); while ($myrow = mysqli_fetch_array($result)) { echo "<option value=".$myrow["cats"].">".$myrow["cats"]."</option>"; } echo "</select>"; Server Version mysql Ver 15.1 Distrib 10.3.34-MariaDB, for debian-linux-gnueabihf (armv8l) using readline 5.2 Any thoughts with regard the above? TIA Steve Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/ Share on other sites More sharing options...
Solution maxxd Posted June 20, 2022 Solution Share Posted June 20, 2022 This is one of the major problems with echoing html from within php - your quotation marks are getting lost/confused. If you have to do this, remember using single quotes is perfectly appropriate for an echo statement in php even though it won't interpolate variables. In your case, this is fine as you're using concatenation. Try this: echo '<option value="'.$myrow['cats'].'">'.$myrow['cats'].'</option>'; This way it's easier to see that there are quotes around the option value so you know that spaces in the value won't break the entire thing (which is what's happening to you now). It's also a best practice in html to use double quotes for attribute values, so that's cool too. I made the quotes around the array indexes single quotes; I'm not entirely sure that's best practice for php but it's what I've seen most often and what I personally prefer, so... Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597473 Share on other sites More sharing options...
stevegingercat Posted June 20, 2022 Author Share Posted June 20, 2022 Beautiful! Thanks so much for that! It Works Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597474 Share on other sites More sharing options...
maxxd Posted June 20, 2022 Share Posted June 20, 2022 Groovy - I'm genuinely glad that works and helped you out. Now for bonus points, can you explain why it works? Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597475 Share on other sites More sharing options...
phppup Posted June 21, 2022 Share Posted June 21, 2022 @maxxd it works because the ECHO is 'printing' the HTML commands that are to be used. In this format, the PHP had to be interrupted by closing the quotes and adding a dot around a non-HTML item, which is a PHP variable, and then reopening the connection if more concatenation is required. Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597483 Share on other sites More sharing options...
ginerjm Posted June 21, 2022 Share Posted June 21, 2022 Another way of doing this is: echo "<option value='{$myrow['cats']}'>{$myrow['cats']}</option>"; Use the braces when you have an array value. Not needed for simple vars such as $x as in : echo "<option value='$x'>$x</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597495 Share on other sites More sharing options...
Barand Posted June 21, 2022 Share Posted June 21, 2022 Beware when enclosing a string attribute value with single quotes. If $myrow['cats'] = "O'Reilly books; echo "<option value='{$myrow['cat']}'>{$myrow['cat']}</option>"; then the generated HTML will be And the submitted GET value... Instead, echo "<option value=\"{$myrow['cat']}\">{$myrow['cat']}</option>"; -> Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597496 Share on other sites More sharing options...
ginerjm Posted June 21, 2022 Share Posted June 21, 2022 I have no difficulty getting that line of code to work with the value of O'Reilly. How the script handles a value like that when selected is perhaps another issue but getting it to display properly with my offering is not a problem. Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597498 Share on other sites More sharing options...
Barand Posted June 21, 2022 Share Posted June 21, 2022 I know the <select> dropdown looks fine and the options display correctly but view the page source code. The problem comes when the form is submitted and the value is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597499 Share on other sites More sharing options...
ginerjm Posted June 21, 2022 Share Posted June 21, 2022 I see now what you are pointing out. But - should the situation ever involve values having double quotes in them, unlike O'Reilly, there's gonna be a problem then. As I said earlier we can manage to output it ok, it's just the input processing that's the issue in either case. Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597500 Share on other sites More sharing options...
mac_gyver Posted June 21, 2022 Share Posted June 21, 2022 the issue of any external, unknown, dynamic value being output on a web page, possibly containing html special characters that would break the html syntax, should be handled by applying htmlentities, with the ENT_QUOTES flag, to value when it is output. this will allow any single-quote, double-quote, <, >, or & in the actual value to work. they will be converted, by the browser, back to the actual literal character when the value is submitted. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597502 Share on other sites More sharing options...
maxxd Posted June 21, 2022 Share Posted June 21, 2022 I initially went with concatenation because best practices in HTML recommend using double quotes for attribute values so far as I recall. All that having been said, obviously this becomes much easier using a template framework like Twig or Blade. <option value="{{ $myrow['cats'] }}">{{ $myrow['cats'] }}</option> Not only that, both escape output by default. Quote Link to comment https://forums.phpfreaks.com/topic/314943-quotes-not-quite-right/#findComment-1597524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.