laide234 Posted July 16, 2006 Share Posted July 16, 2006 Here is my code...$sql = 'SELECT * FROM `tbl_category` WHERE `cat_parent_id` = 0 ';$result = mysql_query($sql);$row = mysql_fetch_array($result); <select name="mainCategory" id="mainCategory"> <option value = "0" selected> -- None -- </option> <?php while ($row = mysql_fetch_array($result)) { print ( "<option value = " ); printf ($row["cat_id"]); print ( ">" ); printf ($row["cat_name"]); print ( "</option>\n" ); } ?> </select>Now the Query returns 9 rows, however, only 8 options (+ "None", making 9 options) show up in the drop down list. Why??? ??? Quote Link to comment Share on other sites More sharing options...
pixy Posted July 16, 2006 Share Posted July 16, 2006 I honestly don't know what "printf" does, but here's a shot...$query = "SELECT * FROM tbl_category WHERE cat_parent_id=0";$result = mysql_query($query);echo '<select name="mainCategory" id="mainCategory"><option value="0" selected>-- NONE --</option>';while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<option value='.$row['cat_id'].'">'.$row['cat_name'].'</option>';}echo '</select>';?> Quote Link to comment Share on other sites More sharing options...
laide234 Posted July 16, 2006 Author Share Posted July 16, 2006 Thanks pixy.I tried your code but there was no difference... :-[P.S. The PHP printf( ) function works much like the print( ) function in that it outputs (prints) a string (argument) to the web browser. However, you will use the printf( ) function when you want to control how this argument will be formatted.see [url=http://www.bellaonline.com/articles/art30296.asp]http://www.bellaonline.com/articles/art30296.asp[/url]using printf....print ($row["cat_name"]);print ( "</option>\n" );becomes...printf ($row["cat_name"], "</option>" ); Quote Link to comment Share on other sites More sharing options...
pixy Posted July 16, 2006 Share Posted July 16, 2006 Maybe you should take a screenshot of your database? Perhaps there's a problem with the values--since the PHP code looks valid to me. Quote Link to comment Share on other sites More sharing options...
laide234 Posted July 16, 2006 Author Share Posted July 16, 2006 I'm sure it is not the DB because when I order the query, it skips either number 1 or 9. Quote Link to comment Share on other sites More sharing options...
pixy Posted July 16, 2006 Share Posted July 16, 2006 Try plugging the query directly into the database and see what it gives you:SELECT * FROM `tbl_category` WHERE cat_parent_id=0; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 17, 2006 Share Posted July 17, 2006 The reason the OP was only getting 8 out of the 9 rows is that this line[code]<?php $row = mysql_fetch_array($result); ?>[/code]is before the while loop. Remove that line and you should be ok.BTW, instead of using 5 statements to output the line, use one:[code]<?php '<option value='.$row['cat_id'].'">'.$row['cat_name'].'</option>'; ?>[/code]as pixy suggested.Ken Quote Link to comment Share on other sites More sharing options...
laide234 Posted July 17, 2006 Author Share Posted July 17, 2006 Thanks kenrbnsn!!Somehow I didnt see that I had typed "$row = mysql_fetch_array($result)" twice. ::) Quote Link to comment 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.