NathanLedet Posted October 2, 2008 Share Posted October 2, 2008 I'm working on a small CMS and I've been following the tutorials at Lynda.com. When I'm on the page to Add a Page, I want to set its position (the order), so I have a drop down list which counts the number of records inside the database, and then adds 1 (if I have 3 entries, I want "4" to be highlighted so it will automatically be put in the last position) The code I currently have will count the rows, and then select the position it's currently in: $page_count = mysql_num_rows($page_set); for($count=1; $count <= $page_count+1; $count++){ echo "<option value=\"{$count}\""; if ($sel_page['position'] == $count){ echo " selected"; } echo ">{$count}</option>"; } I want to modify that code so that when I'm adding a new entry, it automatically chooses the last available option. Thanks for the help Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Where is your $sel_page['position'] variable being set? Also, why not just do this: if (($page_count+1) == $count){ echo " selected"; } Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted October 2, 2008 Author Share Posted October 2, 2008 Where is your $sel_page['position'] variable being set? It's set elsewhere in the page and returns a number from the database. that number is the position: 1, 2, 3 etc. Also, why not just do this: if (($page_count+1) == $count){ echo " selected"; } I dunno... Ask Kevin Skoglund...I'm following his lessons. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 Can you list the rest of the code? If what you want isn't being selected automatically, then the $sel_page['position'] variable is not matching $count at the right point. Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted October 2, 2008 Author Share Posted October 2, 2008 Yes let me gather it up. but I don't think I was clear enough - sorry about that. The code I posted does work. It works whenever I EDIT the page. It properly displays the selected position. What I am trying to do is re-structure that code that I already have and make it so that when I'm ADDING a page, it automatically chooses the last available position. So, if I have 3 pages, I want that script to display and have already selected "4" in the drop down menu. Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted October 2, 2008 Author Share Posted October 2, 2008 functions.php function get_all_pages(){ global $conn; $query = "SELECT * FROM pages ORDER BY position ASC"; $page_set = mysql_query($query, $conn); confirm_query($page_set); return $page_set; } add_page.php - Form $page_set = get_all_pages(); $page_count = mysql_num_rows($page_set); for($count=1; $count <= $page_count+1; $count++){ echo "<option value=\"{$count}\">{$count}</option>"; } Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 2, 2008 Share Posted October 2, 2008 You're still not showing where $sel_page is set. I suggest you check to see if that is set or not. If it is not, default to $page_count+1 rather than $sel_page['position']. But, I don't know your code for setting that, so I can't help you with that part without seeing it. Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted October 2, 2008 Author Share Posted October 2, 2008 OK... tinkered around with the code a little bit and this code here works. I appreciate you help F1Fan, and I apologize for not being clear. My original code in my first post had nothing to do with the result, and therefore $sel_page makes no difference for($count=1; $count <= $page_count+1; $count++){ echo "<option value=\"{$count}\" "; if ($count == $page_count+1){ echo " selected=\"selected\""; } echo ">{$count}</option>"; } 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.