Stan007 Posted January 11, 2020 Share Posted January 11, 2020 Guys thanks for helping me solve the problem i had but now i have another problem and i am lost. i have included the code below for you to have overview. On the home page the url shows how it should be: http://localhost/board/italian.php?country=Italian BUT When you mouse over the NEXT link to go to next page you get this : http://localhost/board/?page=1&country=Italian and then an error 404 when you click on the link page = 1 no matter if you are mousing over link for page 5 or 2 This is the code: <table style="width:100%; margin-left:auto; margin-right:auto"> <?php $items_per_page = 5;//how many records/rows to display per page $page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $cleaned_current_page_country = htmlspecialchars($_GET["country"]); $page_number = (isset($_GET['page']))? filter_var($_GET['page']) : 1; //$page_number = filter_var($_GET['page']); //$page_number = $_GET['page']; $selected_restaurant_type = $_GET['country']; // find number of rows in the db $query = "SELECT * FROM menu_update WHERE special_of_theday = 1 AND restaurant_type = '".$selected_restaurant_type."' "; $query_result = mysqli_query($connection, $query); $row_count = mysqli_fetch_array($query_result); $total_rows = mysqli_num_rows($query_result); $expected_number_of_pages = ceil($total_rows/$items_per_page); //expected number of pages $page_position = (($page_number - 1) * $items_per_page); //get starting position to fetch record //fetch a group of records using sql LIMIT clause $query = "SELECT * FROM menu_update WHERE special_of_theDay = 1 AND restaurant_type = '".$selected_restaurant_type."' LIMIT " .$page_position. ',' .$items_per_page; $query_result = mysqli_query($connection, $query); ?> <table width="100%" border=0> <tr bgcolor = "#cccccc"> <td>Meal</td> <td>Meal name</td> <td>Meal Price</td> <td>Order Now</td> <td>Full Menu</td> <td>Our Gallery</td> <td>Find Us</td> <td>Our Chef</td> <td>Our Manager</td> </tr> <?php while($row_count = mysqli_fetch_array($query_result)){ echo "<tr>"; echo" <td style ='width:200px; text-align:center; height:120px; background:#eaedf2'><a href='meal_fullPic.php'><img src= ".$row_count['img']." /></a></td> <td style ='width:119px; text-align:center; height:120px; background:#eaedf2'> ".$row_count['meal_name']."</td> <td style ='width:100px; text-align:center; height:120px; background:#eaedf2'>".$row_count['price']."</td> <td style ='width:100px; text-align:center; height:120px; background:#eaedf2'><a href='order_processor.php'><img src= ".$row_count['order_now']." /></a></td> <td style ='width:100px; text-align:center; height:120px; background:#eaedf2'><a href='full_menu.php'><img src= ".$row_count['full_menu_img']." /></a></td> <td style ='width:170px; text-align:center; height:120px; background:#eaedf2'><a href='restaurant_gallery.php'><img src= ".$row_count['restaurant_pic']." /></a></td> <td style ='width:100px; text-align:center; height:120px; background:#eaedf2'><a href='restaurant_location.php'><img src= ".$row_count['location_pic']." /></a></td> <td style ='width:119px; height:120px; background:#eaedf2'> ".$row_count['chef_name']."</td> <td style ='width:119px; height:120px; background:#eaedf2'> ".$row_count['admin']."</td> "; echo "</tr>"; } mysqli_close($connection); ?> </table> <div class="board_pagination"> <?php //display the links to the pages for($current_page = 1; $current_page <= $expected_number_of_pages; $current_page++){ if ($page_number != $expected_number_of_pages){ $data = array( 'page' => $page_number, 'country' => $cleaned_current_page_country, ); $the_query = http_build_query($data); $page_url .= '?' . $the_query; echo '<li><a href=" '.$the_query.' ">'.$current_page.'</a></li> '; } } ?> </div> </table> Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/ Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 3 minutes ago, Stan007 said: When you mouse over the NEXT link to go to next page you get this : http://localhost/board/?page=1&country=Italian and then an error 404 when you click on the link does it work if you put the "Italian.php" back in the middle of that url? Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573353 Share on other sites More sharing options...
Stan007 Posted January 11, 2020 Author Share Posted January 11, 2020 Hello Barand, let me check that now Thanks so much for replying Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573354 Share on other sites More sharing options...
Stan007 Posted January 11, 2020 Author Share Posted January 11, 2020 Hello Barand, yes it does work when i put italian and page=2 in the url and takes me rightly to page 2 this is the url that works but i had to put back italian.php in the url manually: http://localhost/board/italian.php?page=2&country=Italian Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573355 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 What does $page_url contain? Try var_dump($page_url) on line 7 after you set its value; Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573356 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 I have just noticed you don't use the $page_url in those pagination links. It looks like you intended to but then just used the query string $the_query = http_build_query($data); $page_url .= '?' . $the_query; echo '<li><a href=" '.$the_query.' ">'.$current_page.'</a></li> '; As you go to the same page that should be sufficient but I think you will need the preceding "?" Try $the_query = http_build_query($data); $the_query = '?' . $the_query; echo "<li><a href=\"$the_query\">$current_page</a></li>"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573358 Share on other sites More sharing options...
Stan007 Posted January 11, 2020 Author Share Posted January 11, 2020 Hi Barand, Thanks so much, the italian.php is now automatically added to the url like this: http://localhost/menuboard/italian.php?page=1&country=Italian But even when you click on link for page 2, the page always = 1 So you dont actually go to any subsequent page corresponding to the link you clicked. You remain at the index page. Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573362 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 (edited) In your pagination loop you set all the links to the same page number! Should be $data = array( 'page' => $current_page, <-- CHANGED 'country' => $cleaned_current_page_country ); Edited January 11, 2020 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573363 Share on other sites More sharing options...
Stan007 Posted January 11, 2020 Author Share Posted January 11, 2020 Hello Barand, The problem is solved, this code that you suggested fixed it: 'page' => $current_page, <-- CHANGED You really helped me thanks a lot, you have a new fan. Will be making donations pretty soon. Thanks again Stanley Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573365 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 Your misleading choice of variables names no doubt contributed to the problem. There are other things could be improved too Use prepared queries instead of putting variables directly into the sql query strings. (I would also recommend PDO in preference to mysqli) use css classes instead of repetitive inline style definitions you are using deprecated html markup code (eg <tr bgcolor = "#cccccc">) you use a lot of unnecessary concatenation when building strings - a common source of errors. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573366 Share on other sites More sharing options...
Stan007 Posted January 11, 2020 Author Share Posted January 11, 2020 Hello Barand, One last request please, i just noticed that when i click link and arrive at second page, i dont have any back link. Can you please help me with the pagination section of the code. Thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573367 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 It would appear from the code that you would always have links 1 2 3 4 ... N except when you are on the last page. You show the links on this condition... if ($page_number != $expected_number_of_pages) Do you, perchance, only have two pages at present? And do you mean $page_number or do you mean $current_page (confusing names) Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573368 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 The more information a query retrieves the slower it will run. In the code below all you want is a single count yet you retrieve every column in every record. // find number of rows in the db $query = "SELECT * FROM menu_update WHERE special_of_theday = 1 AND restaurant_type = '".$selected_restaurant_type."' "; $query_result = mysqli_query($connection, $query); $row_count = mysqli_fetch_array($query_result); $total_rows = mysqli_num_rows($query_result); Far more efficient to $query = "SELECT COUNT(*) as total FROM menu_update WHERE special_of_theday = 1 AND restaurant_type = '$selected_restaurant_type' "; $query_result = mysqli_query($connection, $query); $row = mysqli_fetch_array($query_result); $total_rows = $row['total']; Quote Link to comment https://forums.phpfreaks.com/topic/309824-problem-fixed-but-caused-new-problem/#findComment-1573370 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.