Agtronic Posted February 25, 2008 Share Posted February 25, 2008 Hi boys and girls! I'm working on member lookup page for a site, and as I added more and more to the script, things were going great. Then I decided to add pagination, and it still worked great. But THEN, I realized it bothered me that the dropdown menus didn't "remember" the sorting choices once a user started clicking into the next pages, as there were no longer the $_POST vars ... What I tried to do is register the sorting choices into session variables, but then after messing with it for too long, I've realized it's too much for my brain, and I just get confused in the logic. It KIND of works, but then out of nowhere, I notice that sometimes the query string ends up with (WHERE Sex='M') and (WHERE Sex='F') in the same query string. I know why it's happening, but I can no longer think straight, and I can't find a solution. I would really appreciate it if someone could point me in the right direction. ANY help would be greatly appreciated. Thanks for your time! if ($_GET['action'] == "list") { if (isset($_GET['page'])) { $page = $_GET['page'] ; } else { $page = 1 ; } if (!$_SESSION['sort_1']) $_SESSION['sort_1'] = 10 ; // set default dropdown values if (!$_SESSION['sort_2']) $_SESSION['sort_2'] = 1 ; if (!$_SESSION['sort_3']) $_SESSION['sort_3'] = 3 ; db($db) ; // all of this is for pagination $query = "SELECT count(*) FROM profiles" ; // $result = mysql_query($query) ; // $query_data = mysql_fetch_row($result) ; // $numrows = $query_data[0] ; // // if (!$_SESSION['sort_1']) // $rows_per_page = 15 ; // else // $rows_per_page = $_SESSION['sort_1'] ; // // $lastpage = ceil($numrows/$rows_per_page) ; // $page = (int)$page ; if ($page < 1) { $page = 1 ; } elseif ($page > $lastpage) { $page = $lastpage ; } $limit = ' LIMIT ' .($page - 1) * $rows_per_page .',' .$rows_per_page; $query = "SELECT UserName, BirthDate, Sex, JoinDate, Sexuality FROM profiles" ; if ($_POST['sort_1']) $_SESSION['sort_1'] = $_POST['sort_1'] ; if ($_POST['sort_2'] == "2" or $_SESSION['sort_2'] == "2") { $_SESSION['sort_2'] = "2" ; $query .= " WHERE Sex='M'" ; } if ($_POST['sort_2'] == "3" or $_SESSION['sort_2'] == "3") { $_SESSION['sort_2'] = "3" ; $query .= " WHERE Sex='F'" ; } if ($_POST['sort_3'] == "1" or $_SESSION['sort_3'] == "1") { $_SESSION['sort_3'] = "1" ; $query .= " ORDER BY BirthDate DESC" ; } if ($_POST['sort_3'] == "2" or $_SESSION['sort_3'] == "2") { $_SESSION['sort_3'] = "2" ; $query .= " ORDER BY Sex DESC" ; } $query .= $limit ; // add in limit clause $result = mysql_query($query) ; $num_results = mysql_num_rows($result); ?> <form name="search" action="members.php?action=list" method="post"> <table width="550" cellpadding="5" cellspacing="5"> <tr> <td colspan="3" valign="middle" class="searchheaders">Members per page : <select name="sort_1" class="searchboxes"><option<? if ($_POST['sort_1'] == "10" or $_SESSION['sort_1'] == "10") echo " selected=\"selected\"" ; ?>>10</option><option<? if ($_POST['sort_1'] == "25" or $_SESSION['sort_1'] == "25") echo " selected=\"selected\"" ; ?>>25</option><option<? if ($_POST['sort_1'] == "50" or !$_POST['sort_1'] or !$_SESSION['sort_1']) echo " selected=\"selected\"" ; ?>>50</option><option<? if ($_POST['sort_1'] == "100" or $_SESSION['sort_1'] == "100") echo " selected=\"selected\"" ; ?>>100</option> </select> Show : <select name="sort_2" class="searchboxes"><option value="1"<? if ($_POST['sort_2'] == "1" or !$_POST['sort_2'] or !$_SESSION['sort_2']) echo " selected=\"selected\"" ; ?>>Both</option><option value="2"<? if ($_POST['sort_2'] == "2" or $_SESSION['sort_2'] == "2") echo " selected=\"selected\"" ; ?>>Only Men</option><option value="3"<? if ($_POST['sort_2'] == "3" or $_SESSION['sort_2'] == "3") echo " selected=\"selected\"" ; ?>>Only Women</option></select> Sort By : <select name="sort_3" class="searchboxes"><option value="1"<? if ($_POST['sort_3'] == "1" or $_SESSION['sort_3'] == "1") echo " selected=\"selected\"" ; ?>>Age</option><option value="2"<? if ($_POST['sort_3'] == "2" or $_SESSION['sort_3'] == "2") echo " selected=\"selected\"" ; ?>>Sex</option><option value="3"<? if ($_POST['sort_3'] == "3" or !$_POST['sort_3'] or !$_SESSION['sort_3']) echo " selected=\"selected\"" ; ?>>Join Date</option></select> <input type="submit" value="Refresh" class="buttons2" /></td> </tr> Quote Link to comment Share on other sites More sharing options...
Agtronic Posted February 25, 2008 Author Share Posted February 25, 2008 Sorry guys, I have never used the code tags before. I added the php tag ... <html> <body> <?php if ($_GET['action'] == "list") { if (isset($_GET['page'])) { $page = $_GET['page'] ; } else { $page = 1 ; } if (!$_SESSION['sort_1']) $_SESSION['sort_1'] = 10 ; // set default dropdown values if (!$_SESSION['sort_2']) $_SESSION['sort_2'] = 1 ; if (!$_SESSION['sort_3']) $_SESSION['sort_3'] = 3 ; db($db) ; // all of this is for pagination $query = "SELECT count(*) FROM profiles" ; // $result = mysql_query($query) ; // $query_data = mysql_fetch_row($result) ; // $numrows = $query_data[0] ; // // if (!$_SESSION['sort_1']) // $rows_per_page = 15 ; // else // $rows_per_page = $_SESSION['sort_1'] ; // // $lastpage = ceil($numrows/$rows_per_page) ; // $page = (int)$page ; if ($page < 1) { $page = 1 ; } elseif ($page > $lastpage) { $page = $lastpage ; } $limit = ' LIMIT ' .($page - 1) * $rows_per_page .',' .$rows_per_page; $query = "SELECT UserName, BirthDate, Sex, JoinDate, Sexuality FROM profiles" ; if ($_POST['sort_1']) $_SESSION['sort_1'] = $_POST['sort_1'] ; if ($_POST['sort_2'] == "2" or $_SESSION['sort_2'] == "2") { $_SESSION['sort_2'] = "2" ; $query .= " WHERE Sex='M'" ; } if ($_POST['sort_2'] == "3" or $_SESSION['sort_2'] == "3") { $_SESSION['sort_2'] = "3" ; $query .= " WHERE Sex='F'" ; } if ($_POST['sort_3'] == "1" or $_SESSION['sort_3'] == "1") { $_SESSION['sort_3'] = "1" ; $query .= " ORDER BY BirthDate DESC" ; } if ($_POST['sort_3'] == "2" or $_SESSION['sort_3'] == "2") { $_SESSION['sort_3'] = "2" ; $query .= " ORDER BY Sex DESC" ; } $query .= $limit ; // add in limit clause $result = mysql_query($query) ; $num_results = mysql_num_rows($result); ?> <form name="search" action="members.php?action=list" method="post"> <table width="550" cellpadding="5" cellspacing="5"> <tr> <td colspan="3" valign="middle" class="searchheaders">Members per page : <select name="sort_1" class="searchboxes"><option<? if ($_POST['sort_1'] == "10" or $_SESSION['sort_1'] == "10") echo " selected=\"selected\"" ; ?>>10</option><option<? if ($_POST['sort_1'] == "25" or $_SESSION['sort_1'] == "25") echo " selected=\"selected\"" ; ?>>25</option><option<? if ($_POST['sort_1'] == "50" or !$_POST['sort_1'] or !$_SESSION['sort_1']) echo " selected=\"selected\"" ; ?>>50</option><option<? if ($_POST['sort_1'] == "100" or $_SESSION['sort_1'] == "100") echo " selected=\"selected\"" ; ?>>100</option> </select> Show : <select name="sort_2" class="searchboxes"><option value="1"<? if ($_POST['sort_2'] == "1" or !$_POST['sort_2'] or !$_SESSION['sort_2']) echo " selected=\"selected\"" ; ?>>Both</option><option value="2"<? if ($_POST['sort_2'] == "2" or $_SESSION['sort_2'] == "2") echo " selected=\"selected\"" ; ?>>Only Men</option><option value="3"<? if ($_POST['sort_2'] == "3" or $_SESSION['sort_2'] == "3") echo " selected=\"selected\"" ; ?>>Only Women</option></select> Sort By : <select name="sort_3" class="searchboxes"><option value="1"<? if ($_POST['sort_3'] == "1" or $_SESSION['sort_3'] == "1") echo " selected=\"selected\"" ; ?>>Age</option><option value="2"<? if ($_POST['sort_3'] == "2" or $_SESSION['sort_3'] == "2") echo " selected=\"selected\"" ; ?>>Sex</option><option value="3"<? if ($_POST['sort_3'] == "3" or !$_POST['sort_3'] or !$_SESSION['sort_3']) echo " selected=\"selected\"" ; ?>>Join Date</option></select> <input type="submit" value="Refresh" class="buttons2" /></td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 25, 2008 Share Posted February 25, 2008 It will help if you name your form elements better than "sort 3" Before you check if the sort_2 is posted or in the session, check only if it is posted. That means it's changed, and if it was 1 and is now 2, you want to ignore the session variable, that is why you get M and F - if it WAS 2, and is NOW 3, both if statements are true. Quote Link to comment Share on other sites More sharing options...
Agtronic Posted February 25, 2008 Author Share Posted February 25, 2008 Ah ha! There's one! I'll fix that, and see where I can go from there. Thanks so much! Quote Link to comment Share on other sites More sharing options...
Agtronic Posted February 25, 2008 Author Share Posted February 25, 2008 I'm just so lost now. I have no idea what I'm doing. I've tried adding the $_POST check, but the logic is just confusing me. There is too much going on, and too many variables, and I have to little short-term memory. I understand what is happening, and I understand why it's happening, what I can't get, is how to structure the IF statements, so that they don't interfere with each other. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 25, 2008 Share Posted February 25, 2008 okay - there are a lot of things you can fix here but here is short example <?php if(isset($_POST['sort_2'])){ if ($_POST['sort_2'] == "2"){ $gender = 'M'; }else if($_POST['sort_2'] == 3){ $gender = 'F'; } }else{ $gender = $_SESSION['gender']; } $query .= " WHERE Sex='$gender'"; $_SESSION['gender'] = $gender; ?> The less often you have to type the same thing over and over the better Also, why do you put your semi-colons waaay out there? That is different... Quote Link to comment Share on other sites More sharing options...
Agtronic Posted February 25, 2008 Author Share Posted February 25, 2008 The less often you have to type the same thing over and over the better Also, why do you put your semi-colons waaay out there? That is different... Thanks alot for the code. That helps clear things up. I totally realize that I tend to write a lot of repetitive code, and it's something I've always noticed about everyone else's code. I just don't think like a programmer. As for the semi-colons, I am very visual and also OCD, so I tend to line things up all the time. Thanks for your help, it is much appreciated. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 26, 2008 Share Posted February 26, 2008 Maybe you should take some actual classes, to help you learn more about programming in general? Good luck! 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.