ivatanako Posted November 9, 2007 Share Posted November 9, 2007 I needed to paginate my article section at my site so that i wont have 20 paragraphs page, now when I tried this on LOCALHOST i encountered no problem at all. But when I uploaded it on my server, the pagination doesn't work. I think the problem here is about the global variables, but im not sure and I dont know how to fix it. Here's my code: <?php $pagenum = $_GET['pagenum']; $pagetitle = 'article Sections'; $active = '2'; include('../includes/header.php'); $dbcnt = mysql_connect("localhost", "xxxxxx", "xxxxx"); mysql_select_db("articles"); echo '<!-- content-wrap starts here --> <div id="content-wrap"> '; include('../includes/sidebar.php'); echo '<div id="main">'; //This checks to see if there is a page number. If not, it will set it to page 1 if (!(isset($pagenum))) { $pagenum = 1; } //Here we count the number of results //Edit $data to be your query $data = mysql_query("SELECT * FROM dlink"); $rows = mysql_num_rows($data); //This is the number of results displayed per page $page_rows = 4; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM dlink $max") or die(mysql_error()); // Query the database while ($row = mysql_fetch_assoc($data_p)) { echo '<div class="box">'; echo '<h1>' .$row['downName'].'</h1>'; echo '<p>Added on: <cite>'.$row['downTime'].'<cite></p>'; echo '<img src='.$row['downImg'].' width=140 height=110 class="dimg" /><p style="text-indent:1px;">'.$row['downDes'].'</p>'; echo '<br clear=none/><p class=comments align-right>Download link:<a href='.$row['downLink'].' target="_self">Download link</a></p>'; echo '</div><div class="boxBottom"><img src="http://ivatanako.000webhost.info/images/block-bottom-bg.jpg" alt=""></div>'; } echo '<div class="box"><p class=comments align-right>'; // This shows the user what page they are on, and the total number of pages echo " Page $pagenum of $last "; // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. if ($pagenum == 1) { } else { echo " <a href='index.php?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='index.php?pagenum=$previous'> <-Previous</a> "; } //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a href='index.php?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='index.php?pagenum=$last'>Last ->></a> "; } echo '</div><div class="boxBottom"><img src="http://xxx.com/images/block-bottom-bg.jpg" alt=""></div>'; // end all div echo '</div></div>'; include('../includes/footer.php'); ?> Quote Link to comment Share on other sites More sharing options...
atlanta Posted November 9, 2007 Share Posted November 9, 2007 Do you have a test link that we could check out to see what error you getting!? Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 9, 2007 Author Share Posted November 9, 2007 Here's a link http://ivatanako.000webhost.info/downloads/index.php well it should paginate to 2 pages since it already has 5 entries, 4 per pages. But it is not working, do you have any idea how to fix it? Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 9, 2007 Share Posted November 9, 2007 can you print your query to where you went wrong? print your query for page 1 and 2 Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 9, 2007 Author Share Posted November 9, 2007 I dont know if I did went wrong because this is working on testing via LOCALHOST. But when it was uploaded to the webhost, it doesn't seem to work. $pagenum = $_GET['pagenum']; Now, if I remove the code above on localhost, i will encounter the same problem, so that's why I have a feeling that this could have something to do with GLOBAL_VARIABLES, but I dont know how to fix global_variables. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 10, 2007 Share Posted November 10, 2007 sorry if i dont read your code but. print this line SELECT * FROM dlink $max(simply echo this line ) on page 1 and to to see if your paging (sql )really works Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 I get this: SELECT * FROM dlink limit 0,4 using echo ('SELECT * FROM dlink'.$max); Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 10, 2007 Share Posted November 10, 2007 i mean the query for page 1 and 2 because im confused with this line if (!(isset($pagenum))) { $pagenum = 1; } should be if (!isset($pagenum)) { $pagenum = 1; } Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 What the line does is it checks if the "?pagenum" is present in the URL and if its not set then it'll set it as ?pagenum=1 for the first page. I tried changing if (!(isset($pagenum))) { $pagenum = 1; } to if (!isset($pagenum)) { $pagenum = 1; } But still there where no changes. This is where the query happens: $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM dlink $max") or die(mysql_error()); // Query the database while ($row = mysql_fetch_assoc($data_p)) { echo '<div class="box">'; echo '<h1>' .$row['downName'].'</h1>'; echo '<p>Added on: <cite>'.$row['downTime'].'<cite></p>'; echo '<img src='.$row['downImg'].' width=140 height=110 class="dimg" /><p style="text-indent:1px;">'.$row['downDes'].'</p>'; echo '<br clear=none/><p class=comments align-right>Download link:<a href='.$row['downLink'].' target="_self">Download link</a></p>'; echo '</div><div class="boxBottom"><img src="http://ivatanako.000webhost.info/images/block-bottom-bg.jpg" alt=""></div>'; } echo '<div class="box"><p class=comments align-right>'; Im sorry if I dont get your instructions and Im sorry about my bad english. Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 10, 2007 Share Posted November 10, 2007 Ironically enough, nothing's wrong with the query. I want to point out that this brilliant user as the or die(mysql_error()) on the stuff, so it's fine there. I believe he's right with the variables however, and I want to try changing the code so you echo the LIMIT into the query instead of a string for it. $start_from is a calculation to determine what row to start counting from to a maximum of $perpage rows (so if you're on page1, it starts at row 0 to 10 rows [assuming 10 rows perpage.. etc]) $start_from = ($pagenum-1)*$page_rows; $perpage = $page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM dlink LIMIT $start_from,$perpage") or die(mysql_error()); // Query the database while ($row = mysql_fetch_array($data_p)) { echo '<div class="box">'; echo '<h1>' .$row['downName'].'</h1>'; echo '<p>Added on: <cite>'.$row['downTime'].'<cite></p>'; echo '<img src='.$row['downImg'].' width=140 height=110 class="dimg" /><p style="text-indent:1px;">'.$row['downDes'].'</p>'; echo '<br clear=none/><p class=comments align-right>Download link:<a href='.$row['downLink'].' target="_self">Download link</a></p>'; echo '</div><div class="boxBottom"><img src="http://ivatanako.000webhost.info/images/block-bottom-bg.jpg" alt=""></div>'; } echo '<div class="box"><p class=comments align-right>'; Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 Thank you for the help. But it is still not working. i like to add that, the last time I encountered this problem on testing at localhost is that when I forgot to add this $pagenum = $_GET['pagenum']; So assuming that Global_variables in the php.ini file is off, how do I change my variables so that I could get the value of pagenum in the url? Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 10, 2007 Share Posted November 10, 2007 Oh, now I get your problem.. Just add $pagenum = $_GET['pagenum'] for your code (global variables = VERY VERY VERY UBER unsecure). That's the only thing you need to add o_o Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 Thank you. But I already have that added below <?php, or if im doing it wrong, where should I add the $pagenum = $_GET['pagenum']; Thank you Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 10, 2007 Share Posted November 10, 2007 At the top if you can, you need to also have the isset($_GET['pagenum']) and is_numeric($_GET['pagenum']) checks to make sure it exists, and that it's numeric. Other than that, I need to see what error the code outputs, as the query is built correctly. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 10, 2007 Share Posted November 10, 2007 i dont know if you have change this line if (!(isset($pagenum))) <----if (!(boolean)) { $pagenum = 1; } if (!(isset($pagenum))) <----if (!(boolean)) that line looks this way if (!(true)) or if(!(false)) i dont think your gonna get the right result out of that condition Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 I have added !(isset($_GET['pagenum']))&& is_numeric($_GET['pagenum']) , no good. There's no errors, here's the link http://ivatanako.000webhost.info/downloads/ Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 10, 2007 Share Posted November 10, 2007 I get this: SELECT * FROM dlink limit 0,4 using echo ('SELECT * FROM dlink'.$max); please do that on page 1 and 2 and print here the result so that we know where to start looking (you only print the query for page one wheres page 2?) Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 ill try explain the code, this code is placed inside index.php, when the page load, it'll set the index.php into index.php?pagenum=1 then fetch 4 data from the database $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM dlink $max") or die(mysql_error()); And automatically paginates the page, so if I have 5 data from the database, it will only display 4 data in the index.php?pagenum=1 then on index.php?pagenum=2 will display the last data. This code creates the NEXT link if ($pagenum == 1) { } else { echo " <a href='index.php?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='index.php?pagenum=$previous'> <-Previous</a> "; } //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a href='index.php?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='index.php?pagenum=$last'>Last ->></a> "; } Sorry, I really dont get want you mean about page 2. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 10, 2007 Share Posted November 10, 2007 paging is generated by sql statement by setting its limit so your first query gives this SELECT * FROM dlink limit 0,4 which means in your page one it will display records form 0-4 and on your page 2 if you print your query it should print this way SELECT * FROM dlink limit 4,8 which means record 4-8 what im suspecting is that your getting SELECT * FROM dlink limit 0,4 in page 1 in 2 so post you query in your index.php?pagenum=1 and index.php?pagenum=2 sorry i cant test that code so im asking those info Quote Link to comment Share on other sites More sharing options...
ivatanako Posted November 10, 2007 Author Share Posted November 10, 2007 I have no idea how to do this. 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.