0

Can somebody tell me what went wrong with my code :

class sub_category
{
    private $con;
    function __construct()
    {
        $this->con = new mysqli("localhost", "root", "", "whatever");
    }

    function show_all()
    {
        $per_page=5;
        if(isset($_GET['page']))
        {
            $page = $_GET['page'];
        }
        else
        {
            $page = 1;
        }

        $start_from = ($page-1)*$per_page;

        $sql = "SELECT sub_kategori.id_sub_kategori, kategori.nama_kategori, sub_kategori.nama_sub_kategori FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori ORDER BY sub_kategori.id_sub_kategori LIMIT ?, ?";
        $stmt = $this->con->prepare($sql);
        $stmt->bind_param('ii', $start_from, $per_page);
        $stmt->execute();
        $stmt->bind_result($id, $namecat, $cat);
        while($stmt->fetch())
        {
            echo "<tr>";
            echo "<td>$id</td>";
            echo "<td>$namecat</td>";
            echo "<td>$cat</td>";
            echo "<td><a href='javascript:'>Update</a></td>";
            echo "<td><a href='sub_category.php?ghgfh=$id'>Delete</a></td>";
            echo "<td><a href=''>Report</a></td>";
            echo "</tr>";
        };
        $stmt->close();
        $page_sql = "SELECT sub_kategori.id_sub_kategori, kategori.nama_kategori, sub_kategori.nama_sub_kategori FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori ORDER BY sub_kategori.id_sub_kategori";
        $stmt_page = $this->con->query($page_sql);
        $total_record = $stmt_page->num_rows;
        $total_page = ceil($total_record/$per_page);
        echo "</tbody>";
        echo "</table>";
        echo "<center>";
        echo "<ul class='pagination'>";
        echo "<li><a href=sub_category.php?page=1>First Page</a></li>";
        for($i=1;$i<=$total_page;$i++)
        {
            if(isset($_GET['page']))
            {
                if($i==$_GET['page'])
                {   
                    echo "<li class='active'><a href=sub_category.php?page=$i>$i</a></li>";
                }
                else
                {
                    echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
                }
            }
            else
            {
                echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
            }
        }
        echo "<li><a href=sub_category.php?page=$total_page>Last Page</a></li>";
        echo "</ul>";
        echo "</center>";
        $stmt_page->close();
    }

    function show($nama_sub_kategori)
    {

        $per_page=5;
        if(isset($_GET['page']))
        {
            $page = $_GET['page'];
        }
        else
        {
            $page = 1;
        }

        $start_from = ($page-1)*$per_page;

        $nama_sub_kategori = "%$nama_sub_kategori%";
        $sql = "SELECT sub_kategori.id_sub_kategori, kategori.nama_kategori, sub_kategori.nama_sub_kategori FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori WHERE sub_kategori.id_sub_kategori LIKE ? OR sub_kategori.nama_sub_kategori LIKE ? OR kategori.nama_kategori LIKE ? ORDER BY sub_kategori.id_sub_kategori LIMIT ?, ?";
        $stmt = $this->con->prepare($sql);
        $stmt->bind_param('sssii', $nama_sub_kategori, $nama_sub_kategori, $nama_sub_kategori, $start_from, $per_page);
        $stmt->execute();
        $stmt->bind_result($id, $namecat, $cat);
        while($stmt->fetch())
        {
            echo "<tr>";
            echo "<td>$id</td>";
            echo "<td>$namecat</td>";
            echo "<td>$cat</td>";
            echo "<td><a href=''>Update</a></td>";
            echo "<td><a href='sub_category.php?ghgfh=$id'>Delete</a></td>";
            echo "<td><a href=''>Report</a></td>";
            echo "</tr>";
        };
        $stmt->close();
        $page_sql = "SELECT count(*) as total FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori WHERE sub_kategori.id_sub_kategori LIKE ? OR sub_kategori.nama_sub_kategori LIKE ? OR kategori.nama_kategori LIKE ? ORDER BY sub_kategori.id_sub_kategori";
        $stmt_page = $this->con->prepare($page_sql);
        $stmt_page->bind_param('sss', $nama_sub_kategori, $nama_sub_kategori, $nama_sub_kategori);
        $stmt_page->execute();
        $stmt_page->bind_result($row);
        $total_record = $row;
        echo $total_record;
        $total_page = ceil($total_record/$per_page);
        echo "</tbody>";
        echo "</table>";
        echo "<center>";
        echo "<ul class='pagination'>";
        echo "<li><a href=sub_category.php?page=1>First Page</a></li>";
        for($i=1;$i<=$total_page;$i++)
        {
            if(isset($_GET['page']))
            {
                if($i==$_GET['page'])
                {   
                    echo "<li class='active'><a href=sub_category.php?page=$i>$i</a></li>";
                }
                else
                {
                    echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
                }
            }
            else
            {
                echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
            }
        }
        echo "<li><a href=sub_category.php?page=$total_page>Last Page</a></li>";
        echo "</ul>";
        echo "</center>";
        $stmt_page->close();
    }
 }

The show_all() function is working perfectly fine ! it show data and pagination with no problem !

The show() function show me data it's working too ! but the pagination gave me NULL result !

I mean the

echo $total record;

It gave me null pagination ! is there something wrong with my code ?

7
  • Change $stmt_page = $this->con->prepare($page_sql); to $stmt_page = $con->prepare($page_sql); Commented Dec 16, 2015 at 19:23
  • Nope, it gave me error now :( Notice: Undefined variable: con in C:\xampp\htdocs\whatever\login\admin\sql\sub_category.php on line 108 Fatal error: Cannot access empty property in C:\xampp\htdocs\whatever\login\admin\sql\sub_category.php on line 108 Commented Dec 16, 2015 at 19:25
  • It inside class by the way and i put my $con variable inside construct function, it's working on other function but not on this one :( Commented Dec 16, 2015 at 19:27
  • 1
    If you want a function to return something use return not echo. Commented Dec 16, 2015 at 19:27
  • No, sorry, I don't want to return any value, just want to show it. Commented Dec 16, 2015 at 19:28

2 Answers 2

1

Add a name (for example total) to the column:

 SELECT count(*) as total FROM sub_kategori ....

And then just echo the value:

echo $row->total
Sign up to request clarification or add additional context in comments.

1 Comment

Do a var_dump($row) before the echo, and tell me what it prints.
1

Perhaps I'm missing something obvious in that I haven't used the $this in this way in php; that is, to specify access to a global variable outside a specific class. When sharing variables across functions, $global is used to specify the shared (con in this case) variable.
In your showAll function, you set $total_record = $stmt_page->num_rows; in your second (show) function you set $total_record = $row; where $row does not appear to have been initialized ... hence NULL is correct.

4 Comments

No it's not like that. I write it only "$row" because i already bind the result of "count(*)" from the query on "$row" variable, it's how mysqli bind result works in taking value from the executed query.
I use "count(*)" and bind_result because bind_param in mysqli not support num_rows.
Right, so in your example (thanks for highlghting the bind), $row is bound to the resultset. So to get the number of rows, wouldn't you want to use $row->num_rows instead of just $row?
bind_param in mysqli not support num_rows, that's why i have to use "count(*)" and bind_result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.