shadiadiph Posted December 19, 2008 Share Posted December 19, 2008 sorry just another simple question what is the correct syntax for this it is only displaying the first object supp;ied by the database?? <td class="right"> <? $sql = "SELECT * FROM tblcatdetails order by category asc"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { $catid = $row["intCatID"]; $category = $row["category"]; ?> <? echo '<select name="category"><option value="'.$intCatID.'">'.$category.'</option> </selected>'; } ?> </td> Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/ Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 You have your select tags inside the loop. The only thing you should be looping is the option tags. Also, you used the wrong var name for value. <td class="right"> <?php $sql = "SELECT * FROM tblcatdetails order by category asc"; $result = mysql_query($sql); echo "<select name='category'>"; while ($row = mysql_fetch_assoc($result)) { $catid = $row["intCatID"]; $category = $row["category"]; echo "<option value='$catid'>$category</option>"; } echo "</select>"; ?> </td> Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719837 Share on other sites More sharing options...
ngreenwood6 Posted December 19, 2008 Share Posted December 19, 2008 You did not define the $intCatID variable. But this would be the correct way: <td class="right"> <?php $sql = "SELECT * FROM tblcatdetails ORDER BY category ASC"; $result = mysql_query($sql); echo '<select name="category">'; while ($row = mysql_fetch_array($result)) { $catid = $row["intCatID"]; $category = $row["category"]; echo '<option value="'.$catid.'">'.$category.'</option>'; } echo '</select>'; ?> </td> I see a bunch of errors. I do not know why you ended the php ?> and then just restarted it <? you never needed to end it. With your queries you need to uppercase ORDER BY and ASC(anything that is built into mysql). You wanted to fetch the array instead of assoc. You also should be using <?php instead of <? because it is not used by all hosts. Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719841 Share on other sites More sharing options...
shadiadiph Posted December 19, 2008 Author Share Posted December 19, 2008 thanks for that i am sure i shall have another question in the next two minutes lol Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719847 Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 I see a bunch of errors. I do not know why you ended the php ?> and then just restarted it <? you never needed to end it. With your queries you need to uppercase ORDER BY and ASC(anything that is built into mysql). You wanted to fetch the array instead of assoc. You also should be using <?php instead of <? because it is not used by all hosts. It is not necessary to uppercase sql keywords. It's done purely for programmer readability. sql doesn't care either way. You do not need to use fetch_array instead of fetch_assoc. fetch_array returns double the data: an array with associative indexing and an array with numerical indexing. fetch_assoc returns just the associative version (fetch_row returns just the numerical indexed version). There's even an optional 2nd argument for fetch_array to only return associative or numeric, not both, effectively making it exactly like fetch_assoc or fetch_row, anyways. Unless you for some reason need both (which really, you don't), it's better to just use fetch_row or fetch_assoc, so you aren't using twice the memory. Using <?php ... ?> is preferred over the <? ... ?> short tags, for portability. It's not that not all servers don't have it, it's that not all servers have it enabled in php.ini. Last time I checked, that setting is on by default, so chances of you finding a server that doesn't have it set are minute. If your server has it enabled and you aren't planning on porting your code somewhere else, it really doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719859 Share on other sites More sharing options...
ngreenwood6 Posted December 19, 2008 Share Posted December 19, 2008 I stand corrected about the fetch_assoc part I misunderstood the whole fetch_array and fetch_assoc but thanks for clarifying however as far as the <?php and the uppercasing I was letting him know that so that because that is the standards that should be gone by. it takes an extra second to type php and its alot easier to read a query that is uppercased properly. Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719868 Share on other sites More sharing options...
.josh Posted December 19, 2008 Share Posted December 19, 2008 Wasn't trying to nitpick. It's just that you started out by saying "I see a bunch of errors" and then said "you need to do this" and "you need to do that," which implied that his script wasn't working because of those things. At least, that's how I interpreted it. Could be just me >.> Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719872 Share on other sites More sharing options...
ngreenwood6 Posted December 19, 2008 Share Posted December 19, 2008 Your right I did word it incorrectly and I was just trying to push the correct method to do things while he is starting to learn coding so that if anyone ever has to read it or use it down the road they will be able to read it easily and it will be portable like you said. Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719874 Share on other sites More sharing options...
shadiadiph Posted December 19, 2008 Author Share Posted December 19, 2008 Thanks guys very helpful mm this one might be inappropriate for this section is there anyway to make this work in php or do i need javascript? It isn't automatically changing i didn't really expect it to. too much to hope for i guess. I cnat think of anyway to write this you cany put where intCatID='$catid' =selected can you? right now its displaying the wrong items from a different row in '.$subcategory.' <td class="right"> <? $sql = "SELECT * FROM tblcatdetails ORDER BY category ASC"; $result = mysql_query($sql); echo '<select name="category">'; while ($row = mysql_fetch_assoc($result)) { $catid = $row["intCatID"]; $category = $row["category"]; echo '<option value="'.$catid.'">'.$category.'</option>'; } echo '</select>'; ?> </td></tr> <tr><td class="left">Sub Category:</td> <td class="right"> <? $sql = "SELECT * FROM tblsubcatdetails where intCatID='$catid' ORDER BY subcategory ASC"; $result = mysql_query($sql); echo '<select name="subcategory">'; while ($row = mysql_fetch_assoc($result)) { $subcatid = $row["intSubCatID"]; $subcategory = $row["subcategory"]; echo '<option value="'.$subcatid.'">'.$subcategory.'</option>'; } echo '</select>'; ?> </td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/137714-solved-selected-echo-option-problem/#findComment-719876 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.