Jump to content

Problem fixed but caused new problem


Stan007

Recommended Posts

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>
 

Link to comment
Share on other sites

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>";

 

  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.
  • Like 1
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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'];

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.