mike12255 Posted February 17, 2009 Share Posted February 17, 2009 So i had the following lines: $page = $_GET['page'] * 10; $tolimit = 15; print "<table class='maintable'>"; print "<tr class='headline' color = 'white'><td width=20%>Author</td><td width=80%>Post</td></tr>"; $gettopic="SELECT * from forumtutorial_posts where postid='$id' AND area ='math' LIMIT $page,$tolimit" My whole idea of using the limit feature was to limit the amount of posts displayed on a page. My friend told me that anything multiplied by something in php is an int so he said: Apples * Oranges = 0 Which means that a string couldnt be entered protecting me from an sql injection. However I tried that with the url http://schoolworkanswers.com/message.php?id=22&catagory=math&page=1 however no posts showed up and im not sure why this happenings. Im guesing because i do Page 1 * 10 so its trying to start at the tenth post but i dont have a tenth post. Anyone have an opinion on how to change this to work better and still do what I want? Quote Link to comment Share on other sites More sharing options...
trq Posted February 17, 2009 Share Posted February 17, 2009 Which means that a string couldnt be entered protecting me from an sql injection. You should simply use mysql_real_escape_string on any data coming from the client that your going to use within your queries. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 17, 2009 Author Share Posted February 17, 2009 I know i was talking to him about that and he said it wouldnt work for some reason, and regardless my problem still wont be fixed using that Quote Link to comment Share on other sites More sharing options...
Cal Posted February 17, 2009 Share Posted February 17, 2009 $page = ($_GET['page'] * 10)-10; $tolimit = $_GET['page'] * 10; if $_GET['page'] is 1, the records start from zero and go to the $tolimit which is ten more than $page. That might work? Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 17, 2009 Author Share Posted February 17, 2009 I think your math is right cal, let me try that and get back to you Quote Link to comment Share on other sites More sharing options...
trq Posted February 17, 2009 Share Posted February 17, 2009 I know i was talking to him about that and he said it wouldnt work for some reason Sounds like your freind doesn't know what his talking about. , and regardless my problem still wont be fixed using that Well, you still need to check to see if your query finds any results within your limit. If it doesn't, you can at least display a nice error page. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 18, 2009 Share Posted February 18, 2009 $page = ($_GET['page'] * 10)-10; $tolimit = $_GET['page'] * 10; if $_GET['page'] is 1, the records start from zero and go to the $tolimit which is ten more than $page. That might work? You don't want to multiply the $tolimit variable. The two varialbes for limit are the start index and the count. Using that logic page 1 would return the first 10 records, page two would return the next 20 records, page three would return the next 30 records, etc. Leave the $tolimit at 10 (or whatever count you want displayed on a page. In fact, it would be better to use a variable to determine the number of records per page so you can change it easily with one value: $records_per_page = 10; $page = (int) $_GET['page']; //Convert to an int //Determine total pages available $query = "SELECT COUNT(*) FROM table"; $result = mysql_query($query); $record = mysql_fetch_row($result); $total_pages = ceil($record[0] / $records_per_page); if ($page < 1 || $page > $total_pages) { $page = 1 } $limit_offset = ($page * $records_per_page) - $records_per_page; $query = "SELECT * FROM table ORDER BY somefield LIMIT $offset, $records_per_page"; Quote Link to comment Share on other sites More sharing options...
mike12255 Posted February 18, 2009 Author Share Posted February 18, 2009 I seemed to get it working, i was playing with the wrong line, but had the right code. 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.