scooptraynor Posted September 22, 2008 Share Posted September 22, 2008 So I used to play around with PHP and ASP about 8 years ago and recently I've been handed the task of building up a website (from content to design, fun times). Anyway, Basically I have a long bibliography that I have put into MySQL and am working out the basics of making a PHP based page for that content. Basic keyword searches on titles, authors, dates, publishers stuff like that. Everything has been going fine (I'm sure the code is pretty damn basic, but it does the job for now) until i hit a brick wall. I need to split up the database entries with page breaks, which is fine if it's just a simple "SELECT * FROM table WHERe xxx limit $x, $y" I've got that sorted, can set different limits etc. However, when it comes to the searching based part of the page, the limits mess up. Basics about the page: I have one overlying if...else statement which acts on whether or not something has been searched for, if something has, it goes off and does a bunch of narrowing down to work out what SQL query to send to the db, if not, it just returns all the records from the db. I'm pretty sure the problem is somewhere in the start of the code. What I've done is when you have the "Previous Page, 1, 2, 3, 4, Next Page" bit at the bottom, each of those links is a submit button for a form which sends itself back on itself with an updated start number for the limit. When i input a search term I get the results up, along with the correct number of pages at the bottom, so at least I know that the SQL query is right and that I'm pulling everything I need to. So I'm on 'page 1' of the results and I click 'page2', this loads up fine, with the correct results. But when I click on page3, page4, next page, or previous page it will, without fail take me back to page one of the results. After playing about for a bit I've realised that no matter what page I click on after loading the search the page will only toggle between page 1 and page 2... When I look at the HTML that's been generated however, all the forms look like they're doing their job. For example, one form would be: <form name="page<? echo $l; ?>" method="post" action="bibliography.php"> <input type="hidden" name="start" value="<? echo $i; ?>" /> //start number for limit SQL query <input type="hidden" name="search" value="<? echo $search; ?>" /> <input type="hidden" name="field" value="<? echo $field; ?>" /> <input type="hidden" name="date" value="<? echo $_POST["date"]; ?>" /> <input type="hidden" name="free" value="<? echo $_POST["free"]; ?>" /> <input type="hidden" name="on" value="yes" /> <input type="hidden" name="supporttype" /> <a href="javascript:getsupport()"><? echo $l;?>[/url] </form> When I look at one of these in the HTML it comes back <form name="page3" method="post" action="bibliography.php"> <input type="hidden" name="start" value="4" /> //with a limit of 2 results a page, page 1 was 0, page 2 was 2, and 3 is 4. <input type="hidden" name="search" value="A" /> <input type="hidden" name="field" value="author" /> <input type="hidden" name="date" value="" /> <input type="hidden" name="free" value="" /> <input type="hidden" name="on" value="yes" /> <input type="hidden" name="next" value="yes" /> <input type="hidden" name="supporttype" /> <a href="javascript:getsupport()">3[/url] </form> OK, so the data has now been passed back to same page, so at the beginning I have my first check to see if form data has been passed through or not. if ($_POST["on"]=="yes") { $on = $_POST["on"]; $search = $_POST["search"]; $field = $_POST["field"]; $date = $_POST["date"]; $free = $_POST["free"]; $field2 = $field."2"; $field3 = $field."3"; $sn = $_POST["start"]; $limit = 2; $this2 = $sn+$limit; $back = $sn-$limit; $next = $sn+$limit; } end; So in my head, when my form has an area called "start" with a value of 4, $sn=4, $this2=6, $back=2, $next=6... So an example of an SQL query would be $query = "SELECT * FROM bib WHERE ($a OR $c OR $d) ORDER BY author"; $query2 = $query." limit $sn, $limit"; $result2 = mysql_query($query); $nume = mysql_num_rows($result2); $result = mysql_query($query2); $nume is being used later on to work out how many records there are in total so I know when to stop the page links.... You can check it out and try searching for leters or something (only 10 entries in the DB right now) just to see what I mean http://www.paultraynor.byethost9.com/Media%20Studies/bibliography.php but any help would be really appreciated, been tearing my hair out over this one for the last few hours and just can't for the life of me understand what's going wrong Link to comment https://forums.phpfreaks.com/topic/125321-beginner-help-with-searching-sql-and-page-breaks/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.