uwictech Posted October 13, 2009 Share Posted October 13, 2009 Hi all, Sorry if this is simple, i'm very new to php, well, any programming language actually. My code runs a query and comes back with the results. Very rarely there may be more than one result. I have added the code that I think should enable me to go to the next and previous records but it is not working. I have tried lots of different bits of code so now its all over the shop! Can you help please? Jamie <?php //run ext query require("conf.php"); { $udf_form = $_POST['udf']; $extract = mysql_query ("SELECT * FROM cy_equ WHERE udf = '$udf_form' LIMIT 1 "); $numrows = mysql_num_rows ($extract); while ($row = mysql_fetch_assoc($extract)) { $id = $row['id']; $equ = $row['equ']; $ext = $row['ext']; $udf = $row['udf']; $udf2 = $row['udf2']; $udf3 = $row['udf3']; $next = $udf + 1; $prev = $udf - 1; } } $next = $udf_form + 1; $prev = $udf_form - 1; ?> <div class="boxextqry"> <h3> UDF Query </h3> <form name="form1" method="post" action="../index1.php?page=udfsearch"> <p> Input UDF: <input type="number" name="udf" size="1" maxlength="4"/></p><br> <p> ID = <font color="red"><?php echo $id; ?> </font> </p> <p> EXT = <font color="red"><?php echo $ext; ?> </font> </p> <p> EQU = <font color="red"><?php echo $equ; ?> </font></p> <p> UDF = <font color="red"><?php echo $udf; ?> </font></p> <p> UDF2 = <font color="red"><?php echo $udf2; ?> </font></p> <p> UDF3 = <font color="red"><?php echo $udf3; ?> </font></p> <p><input type="submit" name="submit" value="GET DATA"/></p> <p><input type="submit" name="udf[<?php echo $next?>]" value="NEXT"/></p> <p><input type="submit" name="udf[<?php echo $prev?>]" value="PREV"/></p> </form> </div> Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/ Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 google pagination. you'll find plenty of tutorials that will help. Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936200 Share on other sites More sharing options...
uwictech Posted October 14, 2009 Author Share Posted October 14, 2009 Thanks for that GK. I did some searching and changed my code. I can now cycle through every record but, I can no longer search for a record. When I press the "GET DATA" button nothing happens. I would like to press the "GET DATA" button and then cycle through the records that come back. I will try and fix it myself, but any help would be appreciated. <?php //run ext query require("conf.php"); { $udf_form = $_POST['udf']; $extract = mysql_query ("SELECT * FROM cy_equ WHERE udf = '$udf_form' LIMIT 1 "); $numrows = mysql_num_rows ($extract); while ($row = mysql_fetch_assoc($extract)) { $id = $row['id']; $equ = $row['equ']; $ext = $row['ext']; $udf = $row['udf']; $udf2 = $row['udf2']; $udf3 = $row['udf3']; } } if(isset($_POST['next'])) { $udf_form = $_POST['udf'] + 1; } elseif(isset($_POST['prev'])) { $udf_form = $_POST['udf'] - 1; } else { // something else } ?> <div class="boxextqry"> <h3> UDF Query </h3> <form name="form1" method="post" action="../index1.php?page=udfsearch"> <p> Input UDF: <input type="number" name="udf" size="1" maxlength="4"/></p><br> <p> ID = <font color="red"><?php echo $id; ?> </font> </p> <p> EXT = <font color="red"><?php echo $ext; ?> </font> </p> <p> EQU = <font color="red"><?php echo $equ; ?> </font></p> <p> UDF = <font color="red"><?php echo $udf; ?> </font></p> <p> UDF2 = <font color="red"><?php echo $udf2; ?> </font></p> <p> UDF3 = <font color="red"><?php echo $udf3; ?> </font></p> <p><input type="submit" name="submit" value="GET DATA"/></p> <input type="hidden" name="udf" value= "<?php echo $udf_form; ?>" /> <p><input type="submit" name="next" value="NEXT"/></p> <p><input type="submit" name="prev" value="PREV"/></p> </form> </div> Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936650 Share on other sites More sharing options...
PravinS Posted October 14, 2009 Share Posted October 14, 2009 Hi Please use below given function function pagingPN($sql, $page, $limit, $getvars, $class) { if ($page == "") $page = 1; if ($limit == 0) $limit = $this->limit; $tsql = $sql; $result = mysql_query($tsql) or die("Error: ".mysql_errno().":- ".mysql_error()); $total = mysql_num_rows($result); $totnumpages = ceil($total/$limit); if ($offset < 0) $offset = 0; else $offset = ($page - 1) * $limit; $sql = $sql. " limit $offset, $limit"; if ($sql!="") { $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error()); if ($result) { while($row = mysql_fetch_array($result)) $data[] = $row; } $res = $data; $serial_no = ($page - 1) * $limit; if ($total > 0) { $link .= "<font face='verdana' size='1'>Page: <strong>".$page."</strong> of <strong>".$totnumpages."</strong> Goto: </font>"; if ($page > 1) { $link .= "<a href=".$_SERVER['PHP_SELF']."?page=1$getvars class='".$class."' title='Jump to First Page'><<</a> | "; $prev = $page - 1; $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$prev."$getvars class='".$class."' title='Goto Previous Page'>Previous</a><span class='".$class."'> | </span>"; } else { $link .= "<span class='".$class."' title='Jump to First Page'><<</span> | <span class='".$class."' title='Goto Previous Page'>Previous | </span>"; } if ($page < $totnumpages) { $next = $page + 1; $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$next."$getvars class='".$class."' title='Goto Next Page'>Next</a> | "; $link .= "<a href=".$_SERVER['PHP_SELF']."?page=".$totnumpages."$getvars class='".$class."' title='Jump to Last Page'>>></a>"; } else { $link .= "<span class='".$class."' title='Goto Next Page'>Next</span> | <span class='".$class."' title='Jump to Last Page'>>></span>"; } } $retarr["sql"] = $sql; $retarr["records"] = $res; $retarr["serial_no"] = $_no; $retarr["link"] = $link; return $retarr; } } $rs = pagingPN($query, trim($_REQUEST['page']), 10, "", "pagingCSS"); $rs_recordset = $rs['records']; $link = $rs['link']; Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936653 Share on other sites More sharing options...
uwictech Posted October 14, 2009 Author Share Posted October 14, 2009 Hi, Thanks for the reply. I'm sorry, I just don't know what to do with this function. I was hoping for a bit of advice on what I have to do with my code. Hope someone can help. Jamie Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936729 Share on other sites More sharing options...
uwictech Posted October 14, 2009 Author Share Posted October 14, 2009 Please help, Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936771 Share on other sites More sharing options...
mrMarcus Posted October 14, 2009 Share Posted October 14, 2009 you have a random { just below the line with: require("conf.php"); { nothing happens like how? time stops? the button does nothing? the page turns white? there's an error of some sort? Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936895 Share on other sites More sharing options...
ialsoagree Posted October 14, 2009 Share Posted October 14, 2009 You say you hit the "GET DATA" button and nothing happens. What I think you mean is that you hit the "GET DATA" button and you're taken to the page you're already at. This is a logic error in your program as this is what you've written it to do: $extract = mysql_query ("SELECT * FROM cy_equ WHERE udf = '$udf_form' LIMIT 1 "); The "LIMIT 1" tells MySQL only to ever return 1 result set. If you're looking to get more than one result set, you can't use the limit. In addition, removing the limit isn't going to be enough: <?php while ($row = mysql_fetch_assoc($extract)) { $id = $row['id']; $equ = $row['equ']; $ext = $row['ext']; $udf = $row['udf']; $udf2 = $row['udf2']; $udf3 = $row['udf3']; }?> On each iteration of this loop, all the data from one result is replaced by the next result. When the 1st result is processed (currently only 1 result can ever be returned), $id is equal to the 1st results id data, $equ is equal to the equ data etc. However, on the 2nd iteration (for the 2nd result) those same variables, $id, $equ, etc. are set to the 2nd results data (and the 1st result is lost). This will repeat until it gets to the last result. In the end, every result prior to the last one isn't saved. But even if you were saving all the data from every result, your HTML is only outputting one set of data: <div class="boxextqry"> <h3> UDF Query </h3> <form name="form1" method="post" action="../index1.php?page=udfsearch"> <p> Input UDF: <input type="number" name="udf" size="1" maxlength="4"/></p><br> <p> ID = <font color="red"><?php echo $id; ?> </font> </p> <p> EXT = <font color="red"><?php echo $ext; ?> </font> </p> <p> EQU = <font color="red"><?php echo $equ; ?> </font></p> <p> UDF = <font color="red"><?php echo $udf; ?> </font></p> <p> UDF2 = <font color="red"><?php echo $udf2; ?> </font></p> <p> UDF3 = <font color="red"><?php echo $udf3; ?> </font></p> <p><input type="submit" name="submit" value="GET DATA"/></p> <input type="hidden" name="udf" value= "<?php echo $udf_form; ?>" /> <p><input type="submit" name="next" value="NEXT"/></p> <p><input type="submit" name="prev" value="PREV"/></p> </form> </div> All your HTML happens exactly 1 time and therefor can only output 1 set of data no matter how many you collect and processes. You would have to make a loop with this HTML inside it to display each result. Truth be told, it's hard to tell exactly what you're trying to do here because it's not thoroughly planned out. Your program is doing exactly what you've told it to. There doesn't appear to be an error with the syntax, it's a logic error in the program. That is, you need to think more about what you're trying to accomplish and the steps you need to take in order to accomplish it. If you provide a more detailed idea of what you're trying to do it may be possible to explain where your code has gone wrong. Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-936916 Share on other sites More sharing options...
uwictech Posted October 19, 2009 Author Share Posted October 19, 2009 Hi, My apologies if I have not explained myself properly. Hopefully, this will tell you exactly what I am trying to convey. I would like to search the database for a specific number (UDF), there could be more than one record that comes back. I would then like to be able to cycle through those records. Is that enough info? Jamie Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-939539 Share on other sites More sharing options...
uwictech Posted October 19, 2009 Author Share Posted October 19, 2009 Do I need to provide more info? Jamie Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-939680 Share on other sites More sharing options...
cags Posted October 19, 2009 Share Posted October 19, 2009 As in your query you are using LIMIT 1, you will only ever get the first result, your query actually needs to include LIMIT $start, 1. Where $start is the 'index' you are requesting. Ignoring the LIMIT part for now your query selects all rows which have udf = '$udf_form'. Now if you LIMIT 1,1 it will fetch the first of those items, LIMIT 2, 1 will fetch the second etc, etc. Basically speaking you need to amend your limit as stated. You will then need to create pagination style links to link to the next/ previous items. Watching alex's pagination tutorial over at PHPAcademy should explain that in terms you will understand. Quote Link to comment https://forums.phpfreaks.com/topic/177555-how-to-show-next-and-prev-records/#findComment-939707 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.