Mysqli paganation help

hello i need help with this code as i cant get it to work this is the code

this is the line thats given me problems in it

$pages = ceil(mysqli_result($pages_query,0)/$per_page);

<?php include('styles/top.php'); ?>
 <div id="full">

    <div id="view_box">
        <ul>
            <li><img src="images/1.png" /></li>
            <!-- <li><img src="pics/2.jpg" /></li> -->
            <!-- <li><img src="pics/3.jpg" /></li> -->
        </ul>
    </div>
    
    <div id="button">
        <ul>
            <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
            <!-- <li><button class="button" scroll_value="600">*</button></li> -->
            <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
        </ul>
    </div>
    <hr /><br />
    
   <?php
    
    if (($user_level !=1) && ($user_level !=2)){
        echo "No Access Allowed";
    } else {
    
    $per_page = 8;
    $pages_query = mysqli_query($link,"SELECT COUNT(`id`) FROM(`orders`) WHERE OrderBy = '$username'");
    $pages = ceil(mysqli_result($pages_query,0)/$per_page);
    
    if (!isset($_GET['page']))
    {
        echo "<meta http-equiv='refresh' content='0;url=view.php?page=1'>";
    }
    else
    {
        $page = $_GET['page'];
    }
    
    $start = (($page - 1)*$per_page);
    
    
    ?>
    
    <h3>View Orders</h3>
    
    		<table border=1">
			<tr>
				<th>FULL NAME </th>
                <th>DATE GOT </th>
				<th>PAY DATE</th>
				<th>MONEY DUE</th>
                <th>UPDATE ORDER STATUS</th>
			</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <?php
            
            error_reporting(0);
            $result = mysqli_query($link,"SELECT * FROM orders WHERE OrderBy = '$username' AND Status = 'Layon' LIMIT $start,$per_page");
        
            
            $totmoney = 0;
            $strmoney = "";
            
            while ($myrow = mysqli_fetch_array($result))
            {
                $strmoney = $myrow["Money"];
                $strmoney = str_replace(',','',$strmoney);
                
                if(is_numeric($strmoney))
                {
                    settype($strmoney, "float");
                    $strmoney = number_format($strmoney, 2);
                }
                echo "<TR><TD><a href=\"_view.php?id=".$myrow['id']."\">".$myrow["FullName"]."</A>&nbsp;</TD><TD>".$myrow["GotDate"]."&nbsp;</TD><TD>".$myrow["PayDate"]."&nbsp;</TD><TD>&pound;".$strmoney."&nbsp;</TD>";
				echo "<TD><center><a href=\"update_order.php?id=".$myrow['id']."\"><img class=\"displayed\" alt=\"View\" src=\"images/edit.png\" width=\"175\" height=\"25\" /></a></center></TD>";
                echo "</TR>";
                
                $totmoney = $totmoney + (int)$myrow["Money"];
            }
            
            
            ?>
            
            
               <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td>TOTAL DUE</td>
					<td>&pound;<?=number_format($totmoney, 2);?>&nbsp;</td>
                    <td></td>
                    <td></td>
				</tr>
          			
		</table>
        
        
        <br />  
   
   <?php
   echo "<center>";
   for ($number=1;$number<=$pages;$number++)
    {
        echo '<a href="?page='.$number.'">'.$number.'</a> | ';
    }
        echo "<br/><br/><font color='white'>Current Page: $page</font>";
   echo "</center></br>";
   
   ?>
           
    <?php
    }
    ?>
    
    
   
</div>
<?php include('styles/bottom.php'); ?>

What error do you get on that line?

It is an odd line for pagination. Normally, you would just set up a pointer that points at the first page of items.
And, then run the query using LIMIT to start at that point. Something loosely like this:

$current_item = 0;
$per_page = 8;
$query = “SELECT * FROM Orders WHERE OrderBy = ‘$username’ LIMIT $per_page, $current_item”;

Using this you just select the 8 items you want. Then, you would have a NEXT button that would link
to $current_item + $per_page. Checking of course for the end of the database count. And, a button for
PREVIOUS that links to $current_item - $per_page… Something like that.

Since this way you would only have the 8 items in the query, you can use a simple WHILE loop to display
all of the data as needed.

Dividing and rounding up is harder to process than a simple addition. Hope that makes sense and helps.

i seem to hav it showing the records now but the page numbers at the bottom dont show up?.

<?php include('styles/top.php'); ?>
 <div id="full">

    <div id="view_box">
        <ul>
            <li><img src="images/1.png" /></li>
            <!-- <li><img src="pics/2.jpg" /></li> -->
            <!-- <li><img src="pics/3.jpg" /></li> -->
        </ul>
    </div>
    
    <div id="button">
        <ul>
            <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
            <!-- <li><button class="button" scroll_value="600">*</button></li> -->
            <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
        </ul>
    </div>
    <hr /><br />
    
   <?php
    
    if (($user_level !=1) && ($user_level !=2)){
        echo "No Access Allowed";
    } else {
    
    $per_page = 8;
    $pages_query = mysqli_query($link,"SELECT COUNT(`id`) FROM(`orders`) WHERE OrderBy = '$username'");
 
 
    if (!isset($_GET['page']))
    {
        echo "<meta http-equiv='refresh' content='0;url=view.php?page=1'>";
    }
    else
    {
        $page = $_GET['page'];
    }
    
    $start = (($page - 1)*$per_page);
    
    
    ?>
    
    <h3>View Orders</h3>
    
    		<table border=1">
			<tr>
				<th>FULL NAME </th>
                <th>DATE GOT </th>
				<th>PAY DATE</th>
				<th>MONEY DUE</th>
                <th>UPDATE ORDER STATUS</th>
			</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <?php
            
            error_reporting(0);
            $result = mysqli_query($link,"SELECT * FROM orders WHERE OrderBy = '$username' AND Status = 'Layon' LIMIT $start,$per_page");
        
            
            $totmoney = 0;
            $strmoney = "";
            
            while ($myrow = mysqli_fetch_array($result))
            {
                $strmoney = $myrow["Money"];
                $strmoney = str_replace(',','',$strmoney);
                
                if(is_numeric($strmoney))
                {
                    settype($strmoney, "float");
                    $strmoney = number_format($strmoney, 2);
                }
                echo "<TR><TD><a href=\"_view.php?id=".$myrow['id']."\">".$myrow["FullName"]."</A>&nbsp;</TD><TD>".$myrow["GotDate"]."&nbsp;</TD><TD>".$myrow["PayDate"]."&nbsp;</TD><TD>&pound;".$strmoney."&nbsp;</TD>";
				echo "<TD><center><a href=\"update_order.php?id=".$myrow['id']."\"><img class=\"displayed\" alt=\"View\" src=\"images/edit.png\" width=\"175\" height=\"25\" /></a></center></TD>";
                echo "</TR>";
                
                $totmoney = $totmoney + (int)$myrow["Money"];
            }
            
            
            ?>
            
            
               <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td>TOTAL DUE</td>
					<td>&pound;<?=number_format($totmoney, 2);?>&nbsp;</td>
                    <td></td>
                    <td></td>
				</tr>
          			
		</table>
        
        
        <br />  
   
   <?php
   echo "<center>";
   for ($number=1;$number<=$pages;$number++)
    {
        echo '<a href="?page='.$number.'">'.$number.'</a> | ';
    }
        echo "<br/><br/><font color='white'>Current Page: $page</font>";
   echo "</center></br>";
   
   ?>
           
    <?php
    }
    ?>
    
    
   
</div>
<?php include('styles/bottom.php'); ?>

I don’t see where you set up $pages ! Just after the query, you should do something like:
$pages = mysqli_num_rows($pages_query) / $per_page;

i tried adding that but still nothings changed?

sorry i ment i tried what u said but still isnt showing the pages

<?php include('styles/top.php'); ?>
 <div id="full">

    <div id="view_box">
        <ul>
            <li><img src="images/1.png" /></li>
            <!-- <li><img src="pics/2.jpg" /></li> -->
            <!-- <li><img src="pics/3.jpg" /></li> -->
        </ul>
    </div>
    
    <div id="button">
        <ul>
            <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
            <!-- <li><button class="button" scroll_value="600">*</button></li> -->
            <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
        </ul>
    </div>
    <hr /><br />
    
   <?php
    
    if (($user_level !=1) && ($user_level !=2)){
        echo "No Access Allowed";
    } else {
    
    $per_page = 10;
    $pages_query = mysqli_query($link,"SELECT COUNT(`id`) FROM(`orders`) WHERE OrderBy = '$username'");
    $pages = mysqli_num_rows($pages_query) / $per_page;
 
    if (!isset($_GET['page']))
    {
        echo "<meta http-equiv='refresh' content='0;url=view.php?page=1'>";
    }
    else
    {
        $page = $_GET['page'];
    }
    
    $start = (($page - 1)*$per_page);
    
    
    ?>
    
    <h3>View Orders</h3>
    
    		<table border=1">
			<tr>
				<th>FULL NAME </th>
                <th>DATE GOT </th>
				<th>PAY DATE</th>
				<th>MONEY DUE</th>
                <th>UPDATE ORDER STATUS</th>
			</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <?php
            
            error_reporting(0);
            $result = mysqli_query($link,"SELECT * FROM orders WHERE OrderBy = '$username' AND Status = 'Layon' LIMIT $start,$per_page");
        
            
            $totmoney = 0;
            $strmoney = "";
            
            while ($myrow = mysqli_fetch_array($result))
            {
                $strmoney = $myrow["Money"];
                $strmoney = str_replace(',','',$strmoney);
                
                if(is_numeric($strmoney))
                {
                    settype($strmoney, "float");
                    $strmoney = number_format($strmoney, 2);
                }
                echo "<TR><TD><a href=\"_view.php?id=".$myrow['id']."\">".$myrow["FullName"]."</A>&nbsp;</TD><TD>".$myrow["GotDate"]."&nbsp;</TD><TD>".$myrow["PayDate"]."&nbsp;</TD><TD>&pound;".$strmoney."&nbsp;</TD>";
				echo "<TD><center><a href=\"update_order.php?id=".$myrow['id']."\"><img class=\"displayed\" alt=\"View\" src=\"images/edit.png\" width=\"175\" height=\"25\" /></a></center></TD>";
                echo "</TR>";
                
                $totmoney = $totmoney + (int)$myrow["Money"];
            }
            
            
            ?>
            
            
               <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td>TOTAL DUE</td>
					<td>&pound;<?=number_format($totmoney, 2);?>&nbsp;</td>
                    <td></td>
                    <td></td>
				</tr>
          			
		</table>
        
        
        <br />  
   
   <?php
   echo "<center>";
   for ($number=1;$number<=$pages;$number++)
    {
        echo '<a href="?page='.$number.'">'.$number.'</a> | ';
    }
        echo "<br/><br/><font color='white'>Current Page: $page</font>";
   echo "</center></br>";
   
   ?>
           
    <?php
    }
    ?>
    
    
   
</div>
<?php include('styles/bottom.php'); ?>

What value are you getting for $pages ?

there is nothing coming up apart from the records?.

i have 7 records in my database and only displaying 5 per page but its not showing number page 2?

this is the code for just pagination

$per_page = 8;
$pages_query = mysqli_query($link,“SELECT COUNT(id) FROM(orders) WHERE OrderBy = ‘$username’”);
$page = mysqli_num_rows($pages_query) / $per_page;

    if (!isset($_GET['page']))
    {
        echo "<meta http-equiv='refresh' content='0;url=view.php?page=1'>";
    }
    else
    {
        $page = $_GET['page'];
    }
    
    $start = (($page - 1)*$per_page);

$result = mysqli_query($link,“SELECT * FROM orders WHERE OrderBy = ‘$username’ AND Status = ‘Layon’ LIMIT $start,$per_page”);

echo “”;
for ($number=1;$number<=$page;$number++)
{
echo ‘’.$number.’ | ';
}
echo “

Current Page: $page”;
echo “”;

You have it set to 10 per page, therefore, no page #2 exists. (Try setting the per-page to 3 for testing)

Also, this section is odd, logic wise!

    if (!isset($_GET['page']))
    {
        echo "<meta http-equiv='refresh' content='0;url=view.php?page=1'>";
    }
    else
    {
        $page = $_GET['page'];
    }
    
    $start = (($page - 1)*$per_page);

You should not use META-REFRESH as it seldom works correctly this way. You would need to put that into a very specific location in the header of the page. You should just set page equal to 1 and then check if it was passed and if so, assign it to the passed version. Then, continue on. You don’t really want to refresh the page again as it is a waste of time. Just use the data. Does this make sense to you?

i set it to 3 and still nothing happened its still only showing 1

Does it show the first five records?

Does it show any part of the page numbers?

Also, in the database what is the field “Money” defined as? Decimal? INT? FLOAT?
(You process it two different ways in your code, just curious why.)

yeah it shows the 5 records

it shows page 1 number you can see the screen here http://mbdues.co.uk/mysqli/img.JPG

Money is just set as a varchar
im just learning

LOL Okay, I see now… So, you display them in the for loop until the number is equal to or less than the current page number. You need to change it to loop to the TOTAL pages… Sorry I didn’t catch that sooner!

What do i need to change?.

Before you calculated the total pages using the ceiling() function. That would give you 2 pages in this case.
You named the value $pages not $page. I never code names like that. I use $current_page and $total_pages so there is no confusion.

So, you need to change the $page to $pages in the for() clause… I used mysqli_num_rows, but, left off the ceiling function…

For the query that gets the total number of matching records, using SELECT COUNT(id) …, you must fetch that count value. The original code this came from, using the old mysql_ functions, used mysql_result(). There is no equivalent mysqli function (you should have been getting a fatal undefined function error.) You must use one of the mysqli fetch statements and reference the count value in the row of fetched data. If you add an alias name in the sql query for the count term, you can use the mysqli_fetch_assoc() statement and reference the count by the alias name.

The calculation for the total number of pages does need the ceil() function so that fractional pages are rounded up.

You should also use the calculated total number of pages to test/limit the requested page number. If someone requests a page that’s greater than the total number of pages (or is less than 1), there’s no point in wasting time running the main SELECT … query since it won’t match any data.

Next, the WHERE … term you are using must be the same in both of the sql queries. The first query needs the AND Status = ‘Layon’ term.

Lastly, you should not put external/unknown values directly into an sql query statement. If the username could contain characters that allow sql injection, someone can craft a username that will retrieve the content of any of your database tables. You must always protect against sql injection. The surest way of doing this is to use prepared queries. In addition to this, your orders table should hold the user id, not the username. This will reduce the storage requirements and speed up the queries.

I tried to change everything to pages now its not showing anything

<?php include('styles/top.php'); ?>
 <div id="full">

    <div id="view_box">
        <ul>
            <li><img src="images/1.png" /></li>
            <!-- <li><img src="pics/2.jpg" /></li> -->
            <!-- <li><img src="pics/3.jpg" /></li> -->
        </ul>
    </div>
    
    <div id="button">
        <ul>
            <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
            <!-- <li><button class="button" scroll_value="600">*</button></li> -->
            <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
        </ul>
    </div>
    <hr /><br />
    
   <?php
    
    if (($user_level !=1) && ($user_level !=2)){
        echo "No Access Allowed";
    } else {
    
    $per_page = 3;
    $pages_query = mysqli_query($link,"SELECT COUNT(`id`) FROM(`orders`) WHERE OrderBy = '$username'");
    $pages = mysqli_num_rows($pages_query) / $per_page;
 
    if (!isset($_GET['pages']))
    {
        echo "<meta http-equiv='refresh' content='0;url=view.php?page=1'>";
    }
    else
    {
        $pages = $_GET['pages'];
    }
    
    $start = (($pages - 1)*$per_page);
    
    
    ?>
    
    <h3>View Orders</h3>
    
    		<table border=1">
			<tr>
				<th>FULL NAME </th>
                <th>DATE GOT </th>
				<th>PAY DATE</th>
				<th>MONEY DUE</th>
                <th>UPDATE ORDER STATUS</th>
			</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <?php
            
            error_reporting(0);
            $result = mysqli_query($link,"SELECT * FROM orders WHERE OrderBy = '$username' AND Status = 'Layon' LIMIT $start,$per_page");
        
            
            $totmoney = 0;
            $strmoney = "";
            
            while ($myrow = mysqli_fetch_array($result))
            {
                $strmoney = $myrow["Money"];
                $strmoney = str_replace(',','',$strmoney);
                
                if(is_numeric($strmoney))
                {
                    settype($strmoney, "float");
                    $strmoney = number_format($strmoney, 2);
                }
                echo "<TR><TD><a href=\"_view.php?id=".$myrow['id']."\">".$myrow["FullName"]."</A>&nbsp;</TD><TD>".$myrow["GotDate"]."&nbsp;</TD><TD>".$myrow["PayDate"]."&nbsp;</TD><TD>&pound;".$strmoney."&nbsp;</TD>";
				echo "<TD><center><a href=\"update_order.php?id=".$myrow['id']."\"><img class=\"displayed\" alt=\"View\" src=\"images/edit.png\" width=\"175\" height=\"25\" /></a></center></TD>";
                echo "</TR>";
                
                $totmoney = $totmoney + (int)$myrow["Money"];
            }
            
            
            ?>
            
            
               <tr>
					<td>&nbsp;</td>
					<td></td>
					<td></td>
                    <td></td>
                    <td></td>
				</tr>
            
            <tr>
					<td>&nbsp;</td>
					<td>TOTAL DUE</td>
					<td>&pound;<?=number_format($totmoney, 2);?>&nbsp;</td>
                    <td></td>
                    <td></td>
				</tr>
          			
		</table>
        
        
        <br />  
   
   <?php
   echo "<center>";
    for ($number=1;$number<=$pages;$number++)
    {
        echo '<a href="?page='.$number.'">'.$number.'</a> | ';
    }
        echo "<br/><br/><font color='white'>Current Page: $pages</font>";
   echo "</center></br>";
   
   ?>
           
    <?php
    }
    ?>
    
    
   
</div>
<?php include('styles/bottom.php'); ?>

hi,

ive tried to change some of the code this is the new look so far

<?php include('styles/top.php'); ?>
 <div id="full">

    <div id="view_box">
        <ul>
            <li><img src="images/1.png" /></li>
            <!-- <li><img src="pics/2.jpg" /></li> -->
            <!-- <li><img src="pics/3.jpg" /></li> -->
        </ul>
    </div>
    
    <div id="button">
        <ul>
            <!-- <li><button class="button" scroll_value="0">*</button></li> -- >
            <!-- <li><button class="button" scroll_value="600">*</button></li> -->
            <!-- <li><button class="button" scroll_value="1200">*</button></li> -->
        </ul>
    </div>
    <hr />
    
    <?php
    
    if ($user_level !=1){
        echo "No Access Allowed";
    } else {
    
    ?>
    
   <?php
   
   $record_per_page = 5;
   $page = '';
   
   if(isset($_GET["page"]))
   {
       $page = $_GET["page"];
   }
   else
   {
       $page = 1;
   }
   
   $start_from = ($page-1)*$record_per_page;
   
   $query = "SELECT * FROM orders DESC LIMIT $start_from, $record_per_page";
   $result = mysqli_query($link, $query);
   
   ?>

    <table>
        <tr>
            <td>Name</td>
            <td>Phone</td>
        </tr>
        <?php
 while($row = mysqli_fetch_array($result))
 {
 ?>
 <tr>
  <td><?php echo $row["student_name"]; ?></td>
  <td><?php echo $row["student_phone"]; ?></td>
 </tr>
 <?php
 }
 ?>
        
    </table>

    <?php
    }
    ?>
    
    
   
</div>
<?php include('styles/bottom.php'); ?>

THis is the error i get

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /customers/7/d/c/mbdues.co.uk/httpd.www/mysqli/view.php on line 59

Do i need to change to a higher page version 7.3?

Sponsor our Newsletter | Privacy Policy | Terms of Service