Page 1 of 5 123 ... LastLast
Results 1 to 10 of 46

Thread: paginate search results

  1. #1
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default paginate search results

    Hi

    I am trying to paginate search results but have the following error

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 148

    It is dividing up the results as 5 per page so that bit is working but unsure of what I am missing

    below is my script

    PHP Code:
    <?php

    ini_set
    ('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);

    include(
    'connect.php');

    $per_page=5;
    if (isset(
    $_GET["page"])) {

    $page $_GET["page"];

    }

    else {

    $page=1;

    }

    // Page will start from 0 and Multiple by Per Page
    $start_from = ($page-1) * $per_page;

    $data = [];
    if (isset(
    $_GET['d1']) && isset($_GET['d2'])) {
        
    $d1 strtotime($_GET['d1']);
        
    $d2 strtotime($_GET['d2']);
        if (
    $d1 && $d2) {
            
    $start_date date('Y-m-d'$d1);
            
    $end_date date('Y-m-d'$d2);
            
    $result $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from$per_page");
            
    $result->bindParam(':a'$start_date);
            
    $result->bindParam(':b'$end_date);
            
    $exec $result->execute();
            if (
    $exec) {
                 
    $data $result->fetchAll();
            }
        }
    }

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head> 
            <title>Search Data Between Two Dates</title>
            <link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
            <link rel="stylesheet" type="text/css" href="css/tcal.css" />
            <script type="text/javascript" src="js/tcal.js"></script>
        </head>  

        <body>
            <div id="logo">
                <img src="images/logo/it-done-right.jpg" alt="" title="">
            </div>

            <?php if ($user): ?>
                <div class='home-content'>
                    <center>
                        <h2>Hello, <?= $user['username']; ?></h2>
                        <a href='../logout.php'>Log Out</a>
                        <br><br>
                        <a href='../index.php'>Home</a>
                    </center>
                </div>
                <br>
            <?php endif; ?>

            <?php include("nav-menu.php"); ?>

            <br>

            <form action="search-data.php" method="get">
                From : <input type="text" name="d1" class="tcal" value="" />
                To: <input type="text" name="d2" class="tcal" value="" />
                <input type="submit" value="Search">
            </form>                        

            <table class="view-repairs">
                <thead>
                    <tr>
                        <th>Software ID</th>
                        <th>Customer PayPal Email</th>
                        <th>Sales Date</th>
                        <th>Software Title</th>
                        <th>Quantity</th>
                        <th>Total Sale</th>
                        <th>Ebay Fees</th>
                        <th>PayPal Fees</th>
                        <th>Cost Price</th>
                        <th>Profit</th>
                        <th>Notes</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($data as $row): ?>
                    <tr>
                       <td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
                       <td><?php echo $row['customer_pp_email']; ?></td>
                       <td><?php echo date("d/m/Y"strtotime($row['sales_month'])); ?></td>
                       <td><?php echo $row['software_title']; ?></td>
                       <td><?php echo $row['quantity']; ?></td>
                       <td><?php echo $row['total_sale']; ?></td>
                       <td><?php echo $row['ebay_fees']; ?></td>
                       <td><?php echo $row['paypal_fees']; ?></td>
                       <td><?php echo '£' $row['software_cost']; ?></td>
                       <td><?php echo '£' $row['profit']; ?></td>
                       <td><?php echo substr($row['notes'], 025); ?></td>
                       <td><?php echo $row['status']; ?></td>
                       <td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            
            <?php
            
    // Count the total records
    $total_records mysqli_num_rows($data);

    //Using ceil function to divide the total records on per page
    $total_pages ceil($total_records $per_page);

    //Going to first page
    echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";

    for (
    $i=1$i<=$total_pages$i++) {

    echo 
    "<a href='search-data.php?page=".$i."'>".$i."</a>";
    };
    // Going to last page
    echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
            
            
    ?>
            
        <div class="box-right" style="height: 90px !important">
        <div class="box-middle-content">

    <?php

    include('connect.php');
    $data = [];
    if (isset(
    $_GET['d1']) && isset($_GET['d2'])) {
        
    $d1 strtotime($_GET['d1']);
        
    $d2 strtotime($_GET['d2']);
        if (
    $d1 && $d2) {
            
    $start_date date('Y-m-d'$d1);
            
    $end_date date('Y-m-d'$d2);

    $result $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");

    $result->bindParam(':a'$start_date);
            
    $result->bindParam(':b'$end_date);
            
    $exec $result->execute();
            if (
    $exec) {
                 
    $data $result->fetchAll();
            }
        }
    }

    foreach (
    $data as $row):

     echo 
    'Total Software Profit for ' $start_date date('d-m-Y'$d1) . ' AND ' $end_date date('d-m-Y'$d2) . '<br>' '£' $row['SUM(profit)'];

    endforeach;

    ?>

    </div>
    </div>
            
            <a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
            
        </body>
    </html>
    Thank you in advance

    Ian

  2. #2
    Join Date
    Jan 2016
    Posts
    23
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default

    Is this the 'search-data.php' file stated in the error message? I guess I don't see very clearly what line 148 is to know how to identify the problem.

  3. #3
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Yeah, sorry the error message I got on search-data.php is Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 155

    On that line 155 is
    Code:
    $total_records = mysqli_num_rows($data);

  4. #4
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    You appear to have included "connect.php" twice. The first is on line 7 and again on line 143. That could be the reason that you cannot connect to the database. Try removing the second instantiation and changing the first to:

    Code:
    require_once('connect.php');

  5. #5
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by styxlawyer View Post
    You appear to have included "connect.php" twice. The first is on line 7 and again on line 143. That could be the reason that you cannot connect to the database. Try removing the second instantiation and changing the first to:

    Code:
    require_once('connect.php');
    Hi

    I have updated the script now to what you said as quoted here but got the following error

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in /home/sites/it-doneright.co.uk/public_html/admin/software-sales/search-data.php on line 149

  6. #6
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    It was only a possible error. The page you have quoted returns a 404 error, so nobody can see your modified code.

  7. #7
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by styxlawyer View Post
    It was only a possible error. The page you have quoted returns a 404 error, so nobody can see your modified code.
    Oh right ok, I have added below the modified code

    Code:
    <?php
    
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);
    
    session_start();
    if ($_SESSION['user']=='') {
        header("Location:../index.php");
    } else {
        include("../config.php");
        $sql = $dbh->prepare("SELECT * FROM users WHERE id=?");
        $exec = $sql->execute(array($_SESSION['user']));
        $user = $exec ? $sql->fetch() : null;
    }
    
    require_once('connect.php');
    
    $per_page=5;
    if (isset($_GET["page"])) {
    
    $page = $_GET["page"];
    
    }
    
    else {
    
    $page=1;
    
    }
    
    // Page will start from 0 and Multiple by Per Page
    $start_from = ($page-1) * $per_page;
    
    $data = [];
    if (isset($_GET['d1']) && isset($_GET['d2'])) {
        $d1 = strtotime($_GET['d1']);
        $d2 = strtotime($_GET['d2']);
        if ($d1 && $d2) {
            $start_date = date('Y-m-d', $d1);
            $end_date = date('Y-m-d', $d2);
            $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
            $result->bindParam(':a', $start_date);
            $result->bindParam(':b', $end_date);
            $exec = $result->execute();
            if ($exec) {
                 $data = $result->fetchAll();
            }
        }
    }
    
    ?>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head> 
            <title>Search Data Between Two Dates</title>
            <link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
            <link rel="stylesheet" type="text/css" href="css/tcal.css" />
            <script type="text/javascript" src="js/tcal.js"></script>
        </head>  
    
        <body>
            <div id="logo">
                <img src="images/logo/it-done-right.jpg" alt="" title="">
            </div>
    
            <script>
                var t;
                window.onload=resetTimer;
                document.onkeypress=resetTimer;
    
                function logout()
                {
                    alert("You are now logged out.");
                    location.href='../logout.php';
                }
                function resetTimer()
                {
                    clearTimeout(t);
                    t=setTimeout(logout,1800000) //logs out in 30 minutes
                }
            </script>
    
            <?php if ($user): ?>
                <div class='home-content'>
                    <center>
                        <h2>Hello, <?= $user['username']; ?></h2>
                        <a href='../logout.php'>Log Out</a>
                        <br><br>
                        <a href='../index.php'>Home</a>
                    </center>
                </div>
                <br>
            <?php endif; ?>
    
            <?php include("nav-menu.php"); ?>
    
            <br>
    
            <form action="search-data.php" method="get">
                From : <input type="text" name="d1" class="tcal" value="" />
                To: <input type="text" name="d2" class="tcal" value="" />
                <input type="submit" value="Search">
            </form>						
    
            <table class="view-repairs">
                <thead>
                    <tr>
                        <th>Software ID</th>
                        <th>Customer PayPal Email</th>
                        <th>Sales Date</th>
                        <th>Software Title</th>
                        <th>Quantity</th>
                        <th>Total Sale</th>
                        <th>Ebay Fees</th>
                        <th>PayPal Fees</th>
                        <th>Cost Price</th>
                        <th>Profit</th>
                        <th>Notes</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($data as $row): ?>
                    <tr>
                       <td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
                       <td><?php echo $row['customer_pp_email']; ?></td>
                       <td><?php echo date("d/m/Y", strtotime($row['sales_month'])); ?></td>
                       <td><?php echo $row['software_title']; ?></td>
                       <td><?php echo $row['quantity']; ?></td>
                       <td><?php echo $row['total_sale']; ?></td>
                       <td><?php echo $row['ebay_fees']; ?></td>
                       <td><?php echo $row['paypal_fees']; ?></td>
                       <td><?php echo '&#163;' . $row['software_cost']; ?></td>
                       <td><?php echo '&#163;' . $row['profit']; ?></td>
                       <td><?php echo substr($row['notes'], 0, 25); ?></td>
                       <td><?php echo $row['status']; ?></td>
                       <td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
                    </tr>
                <?php endforeach; ?>
    
                </tbody>
            </table>
            
            <?php
            // Count the total records
    $total_records = mysqli_num_rows($data);
    
    //Using ceil function to divide the total records on per page
    $total_pages = ceil($total_records / $per_page);
    
    //Going to first page
    echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
    
    for ($i=1; $i<=$total_pages; $i++) {
    
    echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
    };
    // Going to last page
    echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
            
            ?>
            
        <div class="box-right" style="height: 90px !important">
    	<div class="box-middle-content">
    
    <?php
    
    $data = [];
    if (isset($_GET['d1']) && isset($_GET['d2'])) {
        $d1 = strtotime($_GET['d1']);
        $d2 = strtotime($_GET['d2']);
        if ($d1 && $d2) {
            $start_date = date('Y-m-d', $d1);
            $end_date = date('Y-m-d', $d2);
    
    $result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
    
    $result->bindParam(':a', $start_date);
            $result->bindParam(':b', $end_date);
            $exec = $result->execute();
            if ($exec) {
                 $data = $result->fetchAll();
            }
        }
    }
    
    foreach ($data as $row):
    
     echo 'Total Software Profit for ' . $start_date = date('d-m-Y', $d1) . ' AND ' . $end_date = date('d-m-Y', $d2) . '<br>' . '&#163;' . $row['SUM(profit)'];
    
    endforeach;
    
    ?>
    
    </div>
    </div>
            
            <a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
            
        </body>
    </html>

  8. #8
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Two things spring to mind. Firstly, mysqli_num_rows() requires a query result as a parameter (see here) and $data is not a mysqli result but an array. Secondly, it would be much more sensible to evaluate the size of the array immediately after the data is fetched.

    Try this:

    Code:
            if ($exec) {
                 $data = $result->fetchAll();
                 $total_records = mysqli_num_rows($result);
            }
    If that fails I'll have a closer look tomorrow.

    Later: Another alternative is to simply get the number of rows in the array '$data' using the sizeof() function.

    Code:
            if ($exec) {
                 $data = $result->fetchAll();
                 $total_records = sizeof($data, 0);  // Second parameter is optional but if set to 1 will return the total number of elements in the array.
            }
    Last edited by styxlawyer; 02-06-2016 at 11:41 PM. Reason: Additional info.

  9. #9
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by styxlawyer View Post
    Two things spring to mind. Firstly, mysqli_num_rows() requires a query result as a parameter (see here) and $data is not a mysqli result but an array. Secondly, it would be much more sensible to evaluate the size of the array immediately after the data is fetched.

    Try this:

    Code:
            if ($exec) {
                 $data = $result->fetchAll();
                 $total_records = mysqli_num_rows($result);
            }
    If that fails I'll have a closer look tomorrow.
    Thank you really appreciate it, will try that first thing in the morning and post a update if it works or not, thank you again

  10. #10
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi

    Can you please check the modified coding, I used the alternative method you suggested and is paginating the search results to 5 per page but there is no second page where as on the second page should be 4 for the dates I am searching, below is the modified coding

    Code:
    <?php
    
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);
    
    session_start();
    if ($_SESSION['user']=='') {
        header("Location:../index.php");
    } else {
        include("../config.php");
        $sql = $dbh->prepare("SELECT * FROM users WHERE id=?");
        $exec = $sql->execute(array($_SESSION['user']));
        $user = $exec ? $sql->fetch() : null;
    }
    
    require_once('connect.php');
    
    $per_page=5;
    if (isset($_GET["page"])) {
    
    $page = $_GET["page"];
    
    }
    
    else {
    
    $page=1;
    
    }
    
    // Page will start from 0 and Multiple by Per Page
    $start_from = ($page-1) * $per_page;
    
    $data = [];
    if (isset($_GET['d1']) && isset($_GET['d2'])) {
        $d1 = strtotime($_GET['d1']);
        $d2 = strtotime($_GET['d2']);
        if ($d1 && $d2) {
            $start_date = date('Y-m-d', $d1);
            $end_date = date('Y-m-d', $d2);
            $result = $db->prepare("SELECT * FROM purchased_software WHERE sales_month BETWEEN :a AND :b ORDER BY id LIMIT $start_from, $per_page");
            $result->bindParam(':a', $start_date);
            $result->bindParam(':b', $end_date);
            $exec = $result->execute();
            if ($exec) {
                 $data = $result->fetchAll();
            }
        }
    }
    
    ?>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head> 
            <title>Search Data Between Two Dates</title>
            <link rel="stylesheet" type="text/css" media="screen" href="css/styles.css" />
            <link rel="stylesheet" type="text/css" href="css/tcal.css" />
            <script type="text/javascript" src="js/tcal.js"></script>
        </head>  
    
        <body>
            <div id="logo">
                <img src="images/logo/it-done-right.jpg" alt="" title="">
            </div>
    
            <script>
                var t;
                window.onload=resetTimer;
                document.onkeypress=resetTimer;
    
                function logout()
                {
                    alert("You are now logged out.");
                    location.href='../logout.php';
                }
                function resetTimer()
                {
                    clearTimeout(t);
                    t=setTimeout(logout,1800000) //logs out in 30 minutes
                }
            </script>
    
            <?php if ($user): ?>
                <div class='home-content'>
                    <center>
                        <h2>Hello, <?= $user['username']; ?></h2>
                        <a href='../logout.php'>Log Out</a>
                        <br><br>
                        <a href='../index.php'>Home</a>
                    </center>
                </div>
                <br>
            <?php endif; ?>
    
            <?php include("nav-menu.php"); ?>
    
            <br>
    
            <form action="search-data.php" method="get">
                From : <input type="text" name="d1" class="tcal" value="" />
                To: <input type="text" name="d2" class="tcal" value="" />
                <input type="submit" value="Search">
            </form>						
    
            <table class="view-repairs">
                <thead>
                    <tr>
                        <th>Software ID</th>
                        <th>Customer PayPal Email</th>
                        <th>Sales Date</th>
                        <th>Software Title</th>
                        <th>Quantity</th>
                        <th>Total Sale</th>
                        <th>Ebay Fees</th>
                        <th>PayPal Fees</th>
                        <th>Cost Price</th>
                        <th>Profit</th>
                        <th>Notes</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($data as $row): ?>
                    <tr>
                       <td><a href="view-specific-software-sale.php?id=<?= $row['id']; ?>"><?php echo $row['id']; ?></a></td>
                       <td><?php echo $row['customer_pp_email']; ?></td>
                       <td><?php echo date("d/m/Y", strtotime($row['sales_month'])); ?></td>
                       <td><?php echo $row['software_title']; ?></td>
                       <td><?php echo $row['quantity']; ?></td>
                       <td><?php echo $row['total_sale']; ?></td>
                       <td><?php echo $row['ebay_fees']; ?></td>
                       <td><?php echo $row['paypal_fees']; ?></td>
                       <td><?php echo '£' . $row['software_cost']; ?></td>
                       <td><?php echo '£' . $row['profit']; ?></td>
                       <td><?php echo substr($row['notes'], 0, 25); ?></td>
                       <td><?php echo $row['status']; ?></td>
                       <td><a href="add-update-software-sales.php?id=<?= $row['id']; ?>">Edit</a></td>
                    </tr>
                <?php endforeach; ?>
    
                </tbody>
            </table>
            
            <?php
            // Count the total records
    $total_records = sizeof($data, 0);
    
    //Using ceil function to divide the total records on per page
    $total_pages = ceil($total_records / $per_page);
    
    //Going to first page
    echo "<center><a href='search-data.php?page=1'>".'First Page'."</a>";
    
    for ($i=1; $i<=$total_pages; $i++) {
    
    echo "<a href='search-data.php?page=".$i."'>".$i."</a>";
    };
    // Going to last page
    echo "<a href='search-data.php?page=$total_pages'>".'Last Page'."</a></center>";
            
            ?>
            
        <div class="box-right" style="height: 90px !important">
    	<div class="box-middle-content">
    
    <?php
    
    $data = [];
    if (isset($_GET['d1']) && isset($_GET['d2'])) {
        $d1 = strtotime($_GET['d1']);
        $d2 = strtotime($_GET['d2']);
        if ($d1 && $d2) {
            $start_date = date('Y-m-d', $d1);
            $end_date = date('Y-m-d', $d2);
    
    $result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
    
    $result->bindParam(':a', $start_date);
            $result->bindParam(':b', $end_date);
            $exec = $result->execute();
            if ($exec) {
                 $data = $result->fetchAll();
            }
        }
    }
    
    foreach ($data as $row):
    
     echo 'Total Software Profit for ' . $start_date = date('d-m-Y', $d1) . ' AND ' . $end_date = date('d-m-Y', $d2) . '<br>' . '£' . $row['SUM(profit)'];
    
    endforeach;
    
    ?>
    
    </div>
    </div>
            
            <a href="view-software-sales.php">BACK TO VIEW ALL SOFTWARE SALES</a>
            
        </body>
    </html>

Similar Threads

  1. customising a maps with search results
    By jelly46 in forum Other
    Replies: 0
    Last Post: 11-08-2011, 12:53 AM
  2. Replies: 0
    Last Post: 12-28-2009, 09:14 AM
  3. PHP code to retrieve search results
    By GeordieD in forum PHP
    Replies: 1
    Last Post: 01-16-2009, 10:02 PM
  4. Replies: 0
    Last Post: 04-06-2008, 07:57 PM
  5. Split Search results into columns
    By e1seix in forum PHP
    Replies: 1
    Last Post: 07-15-2007, 10:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •