tchick Posted April 13, 2011 Share Posted April 13, 2011 Hi All, I have been trying to get this script to work for a while now and have realised i need some expert help here, what i am trying to achieve is select a letter from alphabet then select gender and only show results for this search but it is not quite working correct. If i search using alphabet fine all works well, but when i throw gender into the equasion it doesn't work correctly, it starts ok until i select another page number and it doesn't work then? Appreciate your help with this one. Thanks Tony <?php include("common.php"); $alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); foreach ($alphabet as $letter) { echo "<a href=\"?letter=" . $letter . "\">" . $letter . "</a> ¦ "; } echo "<a href=\"?\">Show All</a></p><br /> <form method=\"post\" action=\"?\"> Select Gender :<br> Male:<input type=\"radio\" value=\"male\" name=\"gender\"> <br> Female:<input type=\"radio\" value=\"female\" name=\"gender\"> <br><input type=\"submit\" name=\"submit\" value=\"Search\" class=\"submit\" /> </form>"; mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); // If current page number, use it // if not, set one! if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } if(!isset($_GET['letter'])){ $letter = 'a'; } else { $letter = $_GET['letter']; } if(!isset($_GET['gender'])){ $gender = "%"; } else { $gender = $_GET['gender']; } // Define the number of results per page $max_results = 4; // Figure out the limit for the query based // on the current page number. $from = (($page * $max_results) - $max_results); if(empty($_POST)) { $letter = $_GET['letter']; $letter .= "%"; //$sdesc = "*"; $query = "SELECT * FROM contacts WHERE fname LIKE '".$letter."%' ORDER BY fname DESC LIMIT $from, $max_results"; } else { $sex = $_POST['gender']; $query = "SELECT * FROM contacts WHERE gender = '$gender' AND fname LIKE '".$letter."%' ORDER BY fname DESC LIMIT $from, $max_results"; } // Perform mysql query on only the current page number's results $result = mysql_query($query) or die(mysql_error()); //TO PRINT OUT THE DATA echo "<table border='1'>"; echo "<tr> <th>ID</th><th>Photo</th><th>Name</th><th>Gender</th></tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo '<a href="members_gallary.php?id='.$row['id']. '"><img src="rip_pages.php?id=' .$row['id'].'" height="35" width="45"></a>'; echo "</td><td>"; echo $row['fname']; echo "</td><td>"; echo $row['gender']; echo "</td></tr>"; } echo "</table>"; //STOP PRINTING OUT THE DATA // Figure out the total number of results in DB: if(empty($_POST)) { $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM contacts ORDER BY fname DESC"),0); } else { $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM contacts WHERE gender = '$sex' ORDER BY fname DESC"),0); } echo "total pages = " . $total_results ; // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks echo "<p class=\"center\">Pages: "; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">«</a> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i &letter=$letter\">$i</a> "; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">»</a>"; } echo "</p>"; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/ Share on other sites More sharing options...
drisate Posted April 13, 2011 Share Posted April 13, 2011 First of all, you can loop the alphabet with out creating an array like that ... <?php for ($i=ord("A");$i<ord("Z");$i++){ echo "<a href=\"?letter=" . chr($i) . "\">" . chr($i) . "</a> ¦ "; } ?> I made a script that looks like it a week ago It goes like this: <?php $table = "contacts"; $mypage = "index.php?letter=$_GET[letter]&gender=$_GET[gender]"; $nb_per_page = 25; if ($_GET[letter]){$where = "fname LIKE '$letter%'";} if ($_GET == "") { $num_page = 1; } else { $num_page = $_GET['page']; } $offset = ($num_page - 1) * $nb_per_page; $total_results = @current(@mysql_fetch_assoc(@mysql_query("SELECT count(id) FROM $table $where"))); $re_page = $num_page * $nb_per_page; $ree_page = $re_page - $nb_per_page; $i = 0; $maxPage = ceil($total_results / $nb_per_page); $nav = ''; for ($page = 1; $page <= $maxPage; $page++) { if ($page == $num_page) { $nav .= " $page "; } else { $nav .= " <a href=\"$mypage&page=$page\">$page</a> "; } } if ($num_page > 1) { $page = $num_page - 1; $prev = " <a href=\"$mypage&page=$page\">[back]</a> "; $first = " <a href=\"$mypage&page=1\">[First page]</a> "; } else { $prev = ' '; $first = ' '; } if ($num_page < $maxPage) { $page = $num_page + 1; $next = " <a href=\"$mypage&page=$page\">[Next]</a> "; $last = " <a href=\"$mypage&page=$maxPage\">[Last page]</a> "; } else { $next = ' '; $last = ' '; } if ($re_page >= "$total_results") { $re_page = "$total_results"; } echo '<center>'; for ($i=ord("A");$i<ord("Z");$i++){ echo "<a href=\"?letter=" . chr($i) . "\">" . chr($i) . "</a> ¦ "; } echo '</center><br><br>'; print ('<table border="0" cellpadding="3" cellspacing="3" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> <tr> <td width="100%" align="center"><b>F Name</b></td> </tr>'); $select = mysql_query("SELECT * FROM $table $where order by fname asc limit $offset, $nb_per_page") or die(mysql_error()); while ($list = mysql_fetch_array($select)) { $i++; print ('<tr> <td width="100%" style="border-top-style: solid; border-top-width: 1; border-bottom-style: none; border-bottom-width: medium" bgcolor="#E8E8E8"> <b>' . $list[fname] . '</b></td> </tr>'); } // Empty if ($i == "0") { print (' <tr> <td width="100%" colspan="5" align="center" style="border-top-style: solid; border-top-width: 1; border-bottom-style: solid; border-bottom-width: 1" bgcolor="#E8E8E8">No result</td> </tr>'); } echo '</table>'; // Navigation print ("<br>($ree_page to $re_page de $total_results) Page:<b>$first $prev $nav $next $last"); ?> I did not adapte all my code to yours but it's a good start ... All you need is to finish the gender integration and probably a few other things of your code. Upload it and see what left that needs to be done :-) Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1201211 Share on other sites More sharing options...
tchick Posted April 14, 2011 Author Share Posted April 14, 2011 Thanks for the reply dristate, I have scrapped the array and used your idea of the loop for the alphabet which is much better. I tried messing round with your code but to no avail !. I have this code so far which is basic as i am trying to build from scratch. The problem is i can select a letter and it will show all names begining with that letter from my database and when i select a gender it will show all names of that gender but what i can't achieve is select gender and letter to show only these selections from the database ? When i select a letter i get this error message. Notice: Undefined variable: gender in C:\wamp\www\public_html\alpha.php on line 33 when i select gender i get this message. Notice: Undefined variable: letter in C:\wamp\www\public_html\alpha.php on line 33 How can i get them to work together ? Regards Tony <?php session_start(); include("common.php"); // create Alphabet row for ($i=ord("A"); $i<ord("Z");$i++) { echo "<a href=\"alpha.php?letter=" . chr($i) . "\">" . chr($i) . "</a> ¦ "; } echo "<br>"; ?> <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" > Select Gender :<br> Male:<input type="radio" value="male" name="gender"> <br> Female:<input type="radio" value="female" name="gender"> <br><input type="submit" name="submit" value="Search" class="submit"><br> <?php if(isset($_POST['submit'])) { $gender = $_POST['gender']; $_SESSION['gender'] = $gender; echo "Selected Gender = " . $gender . "<br>"; } if (isset($_GET['letter'])) { $letter = $_GET['letter']; $_SESSION['letter'] = $letter; echo "Selected Letter = " . $letter . "<br>"; } // Connect to database $conn = mysql_connect(host,username,password) or die(mysql_error()); $db = mysql_select_db(db) or die(mysql_error()); $sql = "SELECT * FROM contacts WHERE fname LIKE '".$letter."%' AND gender = '".$gender."' ORDER BY fname"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); // While there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // Echo data echo $list['id'] . " : " . $list['gender'] . " : " . $list['fname'] . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1201744 Share on other sites More sharing options...
nethnet Posted April 14, 2011 Share Posted April 14, 2011 Going back to your first script posted, I think it would work properly if you fixed these lines: $sex = $_POST['gender']; $query = "SELECT * FROM contacts WHERE gender = '$gender' AND fname LIKE '".$letter."%' ORDER BY fname DESC LIMIT $from, $max_results"; In here you set the gender from the form equal to the $sex variable, but then use the $gender variable to pull it from the database. You then use the $sex variable later in another query to the database. For ease of simplification, I would replace all three instances of $sex in your code with $gender. I didn't test this, and there may be other issues with the original code, but I spotted this and it seemed to be a likely cause of the issue you originally had. Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1201748 Share on other sites More sharing options...
tchick Posted April 14, 2011 Author Share Posted April 14, 2011 Hi nethnet, Thanks for the reply, I have gone back to my original script as suggested and altered the code as you pointed out. Now this works much better, i am able to select a letter then page numbers for that letter all ok, i can even select a letter and a gender once and it works fine but as soon as i select a page number it loses the letter and goes wrong, any ideas on this ?. I have included my updated script so far. Regards Tony <?php include("common.php"); // create Alphabet row for ($i=ord("A"); $i<ord("Z");$i++) { echo "<a href=\"alpha2.php?letter=" . chr($i) . "\">" . chr($i) . "</a> ¦ "; } echo "<br>"; echo "<a href=\"?\">Show All</a></p><br /> <form method=\"post\" action=\"?\"> Select Gender :<br> Male:<input type=\"radio\" value=\"male\" name=\"gender\"><br> Female:<input type=\"radio\" value=\"female\" name=\"gender\"><br> <input type=\"submit\" name=\"submit\" value=\"Search\" class=\"submit\" /> </form>"; mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); // If current page number, use it // if not, set one! if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } if(!isset($_GET['letter'])){ $letter = '%'; } else { $letter = $_GET['letter']; } if(!isset($_POST['gender'])){ $gender = "%male"; } else { $gender = $_POST['gender']; } // Define the number of results per page $max_results = 4; // Figure out the limit for the query based // on the current page number. $from = (($page * $max_results) - $max_results); if(empty($_POST)) { $letter = $_GET['letter']; $letter .= "%"; //$sdesc = "*"; $query = "SELECT * FROM contacts WHERE fname LIKE '".$letter."%' ORDER BY fname DESC LIMIT $from, $max_results"; } else { $gender = $_POST['gender']; $query = "SELECT * FROM contacts WHERE gender LIKE '$gender' AND fname LIKE '".$letter."%' ORDER BY fname DESC LIMIT $from, $max_results"; } // Perform mysql query on only the current page number's results $result = mysql_query($query) or die(mysql_error()); //TO PRINT OUT THE DATA echo "<table border='1'>"; echo "<tr> <th>ID</th><th>Photo</th><th>Name</th><th>Gender</th></tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo '<a href="members_gallary.php?id='.$row['id']. '"><img src="rip_pages.php?id=' .$row['id'].'" height="35" width="45"></a>'; echo "</td><td>"; echo $row['fname']; echo "</td><td>"; echo $row['gender']; echo "</td></tr>"; } echo "</table>"; //STOP PRINTING OUT THE DATA // Figure out the total number of results in DB: if(empty($_POST)) { $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM contacts ORDER BY fname DESC"),0); }else { $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM contacts WHERE gender = '$gender' ORDER BY fname DESC"),0); } echo "total pages = " . $total_results ; // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks echo "<p class=\"center\">Pages: "; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">«</a> "; } for($i = 1; $i <= $total_pages; $i++) { if(($page) == $i){ echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i &letter=$letter\">$i</a> "; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">»</a>"; } echo "</p>"; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1201762 Share on other sites More sharing options...
nethnet Posted April 14, 2011 Share Posted April 14, 2011 You're not passing the letter and gender in your pagination links. After reading through your code, I've noticed some inconsistencies that look like you've added things and taken things away and it has kind of made the code a bit sloppy. I've cleaned the code up a bit and fixed the issue with the page links. Try this: <?php include("common.php"); for ($i = ord("A"); $i < ord("Z"); $i++) echo "<a href=\"alpha2.php?letter=" . chr($i) . "\">" . chr($i) . "</a> ¦ "; echo "<br>"; echo "<a href=\"?\">Show All</a></p><br /> <form method=\"post\" action=\"?\"> Select Gender :<br> Male:<input type=\"radio\" value=\"male\" name=\"gender\"><br> Female:<input type=\"radio\" value=\"female\" name=\"gender\"><br> <input type=\"hidden\" name=\"letter\" value=\"{$_GET['letter']}\" /> <input type=\"submit\" name=\"submit\" value=\"Search\" class=\"submit\" /> </form>"; mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); $page = (empty($_GET['page'])) ? 1 : $_GET['page']; $letter = (empty($_GET['letter'])) ? "%" : $_GET['letter']; $gender = (empty($_GET['gender'])) ? "%male" : $_GET['gender']; $max_results = 4; $from = (($page * $max_results) - $max_results); if(empty($_POST)) { $query = "SELECT * FROM `contacts` WHERE `fname` LIKE '".$letter."%' ORDER BY `fname` DESC LIMIT $from, $max_results"; } else { $gender = $_POST['gender']; $letter = $_POST['letter']; $query = "SELECT * FROM `contacts` WHERE `gender` LIKE '$gender' AND `fname` LIKE '$letter%' ORDER BY `fname` DESC LIMIT $from, $max_results"; } $result = mysql_query($query) or die(mysql_error()); $rows = mysql_num_rows($result); echo "<table border='1'>"; echo "<tr> <th>ID</th><th>Photo</th><th>Name</th><th>Gender</th></tr>"; if ($rows > 0) { while($row = mysql_fetch_array($result)) { echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo '<a href="members_gallary.php?id='.$row['id']. '"><img src="rip_pages.php?id=' .$row['id'].'" height="35" width="45"></a>'; echo "</td><td>"; echo $row['fname']; echo "</td><td>"; echo $row['gender']; echo "</td></tr>"; } } else { echo "<tr><td colspan=\"4\">No results found!</td></tr>"; } echo "</table>"; echo "Total results = " . $rows; $total_pages = ceil($rows / $max_results); $letterstr = ($letter == "%") ? "" : "letter=" . $letter; $genderstr = ($gender == "%male") ? "" : "gender=" . $gender; echo "<p class=\"center\">Pages: "; if ($page > 1) { $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&$letterstr&$genderstr\">«</a> "; } for($i = 1; $i <= $total_pages; $i++) { if($page == $i) { echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&$letterstr&$genderstr\">$i</a> "; } } if($page < $rows){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&$letterstr&$genderstr\">»</a>"; } echo "</p>"; mysql_close(); ?> I also added a hidden field to your form to help the transition of data from POST (on the first page) to GET (on subsequent pages). Also, I didn't test this, so there may be a silly parse error somewhere. Just run it with error reporting turned on in case. Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1201780 Share on other sites More sharing options...
tchick Posted April 15, 2011 Author Share Posted April 15, 2011 Hi nethnet, Thanks for the alterations to the code your changes certainly made the difference, almost got it working as i want with a few more changes. I can now select letter and gender and works fine, just a small problem with the pagination now. Seems if no more than one page worth of results it still shows forward and backward pages so i need to try to eliminate that bit. Here is the code to date. Regards Tony <?php session_start(); include("common.php"); for ($i = ord("A"); $i < ord("Z"); $i++) echo "<a href=\"alpha3.php?letter=" . chr($i) . "\">" . chr($i) . "</a> ¦ "; echo "<br>"; if (!isset($_POST['submit'])) { echo "<a href=\"?\">Show All</a></p><br /> <form method=\"post\" action=\"?\"> Select Gender :<br> Male:<input type=\"radio\" value=\"male\" name=\"gender\"><br> Female:<input type=\"radio\" value=\"female\" name=\"gender\"><br> <input type=\"hidden\" name=\"letter\" value=\"{$_GET['letter']}\" /> <input type=\"submit\" name=\"submit\" value=\"Search\" class=\"submit\" /> </form>"; } else { Echo "form has been submited"."<br>"; } mysql_connect(host,username,password) or die(mysql_error()); mysql_select_db(db) or die(mysql_error()); $page = (empty($_GET['page'])) ? 1 : $_GET['page']; $letter = (empty($_GET['letter'])) ? "%" : $_GET['letter']; $gender = (empty($_GET['gender'])) ? "%male" : $_GET['gender']; $max_results = 4; $from = (($page * $max_results) - $max_results); if(empty($_POST)) { $query = "SELECT * FROM `contacts` WHERE `fname` LIKE '".$letter."%' ORDER BY `fname` DESC LIMIT $from, $max_results"; } else { $gender = $_POST['gender']; $letter = $_POST['letter']; $letter .= "%"; echo "Letter = " . $letter . " Gender = " . $gender ; $query = "SELECT * FROM `contacts` WHERE `gender` = '$gender' AND `fname` LIKE '$letter' ORDER BY `fname` DESC LIMIT $from, $max_results"; } $result = mysql_query($query) or die(mysql_error()); $rows = mysql_num_rows($result); echo "<table border='1'>"; echo "<tr> <th>ID</th><th>Photo</th><th>Name</th><th>Gender</th></tr>"; if ($rows > 0) { while($row = mysql_fetch_array($result)) { echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo '<a href="members_gallary.php?id='.$row['id']. '"><img src="rip_pages.php?id=' .$row['id'].'" height="35" width="45"></a>'; echo "</td><td>"; echo $row['fname']; echo "</td><td>"; echo $row['gender']; echo "</td></tr>"; } } else { echo "<tr><td colspan=\"4\">No results found!</td></tr>"; } echo "</table>"; echo "Total results = " . $rows; $total_pages = ceil($rows / $max_results); $letterstr = ($letter == "%") ? "" : "letter=" . $letter;$genderstr = ($gender == "%male") ? "" : "gender=" . $gender; echo "<p class=\"center\">Pages: "; if ($page > 1) { $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev&$letterstr&$genderstr\">«</a> "; } for($i = 1; $i <= $total_pages; $i++) { if($page == $i) { echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i&$letterstr&$genderstr\">$i</a> "; } } if($page < $rows){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next&$letterstr&$genderstr\">»</a>"; } echo "</p>"; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1202078 Share on other sites More sharing options...
nethnet Posted April 15, 2011 Share Posted April 15, 2011 For the next link, change the if statement to this: if ($page < $total_pages) { The previous link shows on all pages of results, even page 1? Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1202146 Share on other sites More sharing options...
tchick Posted April 15, 2011 Author Share Posted April 15, 2011 That got the page links sorted, thanks nethnet. I made both these the same for($i = 1; $i <= $rows; $i++) { if($page < $rows){ this gives me [Prev] 1 2 3 4 [Next] which works fine. The problems i have left now are lets say i select 'B' and male it works correct and shows me the 3 male records i have, but the page numbers are still [Prev] 1 2 3 4 [Next] when i only want to show page links if there are more than one page?. The other problem i have still is if i select the link 'show all' i get the Notice: Undefined index: letter in C:\wamp\www\public_html\alpha3.php on line 11 error? Thanks for your help so far with this one. Regards Tony Quote Link to comment https://forums.phpfreaks.com/topic/233620-pagination-with-alphabet-gender-search/#findComment-1202164 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.