2

Im trying to generate a DataTable of the list of Payables from the database using PDO and JQuery AJAX But i keep encountering this error on my PHP File. Im not sure whats wrong with my code.

The error message says:

[28-Jun-2016 16:33:12 Asia/Manila] PHP Notice:  Trying to get property of non-object in ** on line 121

Line 121 in pay_controller.php is:

if($allPayList->res == 0) { echo ""; }

This is the pay_controller.php

<?PHP
class PayController {

public function pay() {

    if (empty($_GET['sid']))
    return call('home', 'expire');

    $getSessionData = SessionData::getData($_GET['sid']);

    if($getSessionData->res = 0)
    {
        return call('home', 'expire');
    }
    date_default_timezone_set('Asia/Manila');
    $dtnow = date('Y-m-d H:i:s');
    $sexpire = date('Y-m-d H:i:s', strtotime($getSessionData->ssend));

    if($sexpire < $dtnow)
    {
        return call('home', 'expire');
    }

    $getMenu = MENU::getMenu();
    $menuid = 3;

    require_once('views/pay.php');

}

public function currentpay() {

    $curPayList = Payables::getCurrentPayable();

    echo "
        <div class='col-md-12'>
            <table id='currentpay' class='display' cellspacing='0' width='100%'>
                <thead>
                    <tr>
                        <th>Sequence</th>
                        <th>Date</th>
                        <th>Vendor</th>
                        <th>Reference No.</th>
                        <th class='text-right'>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
        ";

        if($curPayList->res == 0) {
            echo "";
        }
        elseif($curPayList->res == 1) {

            echo "<tr>
                    <td>" . $curPayList->seq . "</td>
                    <td>" . $curPayList->pdate . "</td>
                    <td>" . $curPayList->vendor . "</td>
                    <td>" . $curPayList->refno . "</td>
                    <td class='text-right'>" . number_format($curPayList->amount,2) . "</td>
                    <td>
                        <a OnClick='showCurPayDetails(". $curPayList->seq .")' class='btn btn-sm btn-default'>
                        <span class='fa-stack fa-fw'>
                          <i class='fa fa-square-o fa-stack-2x'></i>
                          <i class='fa fa-eye fa-stack-1x'></i>
                        </span>
                        VIEW DETAILS
                    </a>
                    </td>
                </tr>";
        }
        elseif($curPayList->res > 1) {
            foreach($curPayList as $cPL) {
                echo "<tr>
                        <td>" . $cPL->seq . "</td>
                        <td>" . $cPL->pdate . "</td>
                        <td>" . $cPL->vendor . "</td>
                        <td>" . $cPL->refno . "</td>
                        <td class='text-right'>" . number_format($cPL->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $cPL->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }
        }

            echo "
                </tbody>
            </table>
        </div>
        ";
}

public function allpay() {

    $allPayListing = Payables::getAllPurchase();

    echo "
        <div class='col-md-12'>
            <table id='allpay' class='display' cellspacing='0' width='100%'>
                <thead>
                    <tr>
                        <th>Sequence</th>
                        <th>Date</th>
                        <th>Vendor</th>
                        <th>Reference No.</th>
                        <th class='text-right'>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
        ";

        if($allPayListing->res == 0) {

            echo "";

        }
        elseif($allPayListing->res == 1) {
            echo "<tr>
                    <td>" . $allPayList->seq . "</td>
                    <td>" . $allPayList->pdate . "</td>
                    <td>" . $allPayList->vendor . "</td>
                    <td>" . $allPayList->refno . "</td>
                    <td class='text-right'>" . number_format($allPayList->amount,2) . "</td>
                    <td>
                        <a OnClick='showCurPayDetails(". $allPayList->seq .")' class='btn btn-sm btn-default'>
                        <span class='fa-stack fa-fw'>
                          <i class='fa fa-square-o fa-stack-2x'></i>
                          <i class='fa fa-eye fa-stack-1x'></i>
                        </span>
                        VIEW DETAILS
                    </a>
                    </td>
                </tr>";
        }
        elseif($allPayListing->res > 1) {
            foreach($allPayListing as $aPL) {
                echo "<tr>
                        <td>" . $aPL->seq . "</td>
                        <td>" . $aPL->pdate . "</td>
                        <td>" . $aPL->vendor . "</td>
                        <td>" . $aPL->refno . "</td>
                        <td class='text-right'>" . number_format($aPL->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $aPL->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }
        }

    echo "
                </tbody>
            </table>
        </div>
        ";
}

public function error() {

    require_once('404.php');

}

public function expire(){

    require_once('expire.php');
 }
}
?>

And this is the mod_pay.php

<?PHP
class Payables {

public $seq;
public $pdate;
public $vendor;
public $refno;
public $amount;
public $res;

public function __construct($seq, $pdate, $vendor, $refno, $amount, $res) {
    $this ->seq         = $seq;
    $this ->pdate       = $pdate;
    $this ->vendor      = $vendor;
    $this ->refno       = $refno;
    $this ->amount      = $amount;
    $this ->res         = $res;
}

public static function getCurrentPayable() {

    $db = Db::getInstance();

    $currentPay = $db->prepare('SELECT
                                payables.seq AS seq,
                                payables.purdate AS pdate,
                                companies.compname AS vendor,
                                payables.refno AS refno,
                                payables.balance AS amount
                            FROM 
                                payables
                            LEFT JOIN 
                                companies
                            ON 
                                companies.seq = payables.vendor
                            WHERE 
                                payables.paidstatus = 0
                            ');

    switch($currentPay->execute()) 
    {
        case True:

            $currentPayResCount = $currentPay->rowCount();

            if($currentPayResCount == 0) {

                $currentPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0);

            }
            elseif($currentPayResCount == 1) {

                $currentPayRes = $currentPay->fetch();
                $currentPayList = new Payables($currentPayRes['seq'], $currentPayRes['pdate'], $currentPayRes['vendor'], $currentPayRes['refno'], $currentPayRes['amount'], $currentPayResCount);

            }
            elseif($currentPayResCount > 1) {

                $currentPayRes = $currentPay->fetchAll();
                foreach($currentPayRes as $cPR) {
                    $currentPayList[] = new Payables($cPR['seq'], $cPR['pdate'], $cPR['vendor'], $cPR['refno'], $cPR['amount'], $currentPayResCount);
                }

            }
            return $currentPayList;
            break;

        case False:
            $currentPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0);
            return $currentPayList;
            break;             
    }
}


public static function getAllPurchase() {

    $db = Db::getInstance();

    $allPayables = $db->prepare('SELECT
                                payables.seq AS seq,
                                payables.purdate AS pdate,
                                companies.compname AS vendor,
                                payables.refno AS refno,
                                payables.balance AS amount
                            FROM 
                                payables
                            LEFT JOIN 
                                companies
                            ON 
                                companies.seq = payables.vendor
                            ');

    switch($allPayables->execute()) 
    {
        case True:

            $allPayResCount = $allPayables->rowCount();

            if($allPayResCount == 0) {

                $allPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0);

            }
            elseif($allPayResCount == 1) {

                $allPayRes = $allPayables->fetch();

                $allPayList = new Payables($allPayRes['seq'], $allPayRes['pdate'], $allPayRes['vendor'], $allPayRes['refno'], $allPayRes['amount'], $allPayResCount);

            }
            elseif($allPayResCount > 1) {

                $allPayRes = $allPayables->fetchAll();

                foreach($allPayRes as $aPR) {
                    $allPayList[] = new Payables($aPR['seq'], $aPR['pdate'], $aPR['vendor'], $aPR['refno'], $aPR['amount'], $allPayResCount);
                }

            }
            return $allPayList;
            break;

        case False:
            $allPayList = new Payables(NULL,NULL,NULL,NULL,NULL,0);
            return $allPayList;
            break;             
    }
  }
}
?>

Note: I'm duplicating the getCurrentPayable() function which does not return an error. It returns 1 result. getAllPurchase() should return 2.

Edit: I included the entire file. Edit: I edited it to reflect the recent changes I've made and it still showing the same error.

rainbasa
  • 69
  • 8
  • 1
    So: what _is_ `$allPayList` then? – arkascha Jun 28 '16 at 09:03
  • `$allPayList` should be an object. It should be the result of the database query in mod_pay.php – rainbasa Jun 28 '16 at 09:08
  • Looks like you're missing $allplaylist = new classname; – Jeff Jun 28 '16 at 09:11
  • 2
    But one of your conditions results in an array of these objects being returned? – Callan Heard Jun 28 '16 at 09:11
  • @Jeff, hmmm.. i cant seem to find where im missing – rainbasa Jun 28 '16 at 09:16
  • @CallanHeard Im not sure which condition does. the mod_pay.php creates the object. pay_controller.php only processes the result. – rainbasa Jun 28 '16 at 09:17
  • 1
    `$allPayList[] = new AllPayables($aPR['seq'], $aPR['pdate'], $aPR['vendor'], $aPR['refno'], $aPR['amount'], $allPayResCount);` is creating an array? Which is `elseif($allPayResCount > 1)` this condition – Callan Heard Jun 28 '16 at 09:19
  • @user3643369 when you call a class function you must first instantiate that class unless you're using some sort of class autoloader such as here: http://php.net/manual/en/language.oop5.autoload.php if not, I suggest simply changing your line that calls the $allPayList = AllPayables::getAllPurchase(); to: $allPayList = new AllPayables; $allPayList->getAllPurchase(); – Jeff Jun 28 '16 at 09:24
  • @CallanHeard is it not creating an array of objects instead? As in understand it, `$allPayList[] = new AllPayables` - this part creates the object for each instance and appends it to the array. – rainbasa Jun 28 '16 at 09:33
  • 1
    @user3643369 - Yes, but you are then returning that *array* and trying to use it as an object – ImClarky Jun 28 '16 at 09:34
  • 1
    Can you debug and inform us which condition is being used to instantiate the object? If an array is being returned, you can't simply access one single object using `$allPayList->res` but would have to index the array – Callan Heard Jun 28 '16 at 09:35
  • @CallanHeard Ahuh! I got it. Maybe the reason it is not being read as object is because it is indeed an array. An array of objects. I will try to loop through it first before calling `$allPayList->res` – rainbasa Jun 28 '16 at 09:39
  • Yes, if PHP *tells* you it's not an object, that's because it really isn't an object. – deceze Jun 28 '16 at 09:59

6 Answers6

1

So for those wondering, the correct answer to this question and the solution to the problem was that one of the multiple conditions being used within the 'mod_pay.php' file to initialise the object in question was actually returning an array of these objects and so could not be directly accessed as an object using if($allPayList->res == 0) { echo ""; }.

Without knowing more about the intention of the system, I can't really offer the most suitable method for dealing with multiple records being returned from the database.

Callan Heard
  • 727
  • 1
  • 8
  • 18
0

Possibly, the data you are fetching does not exist into the join table. Be sure to skip that data from fetching or else add "ON DELETE CASCADE" or "ON UPDATE SET NULL" option while applying reference key in your data table.

Dev007
  • 16
  • 1
0

As @arkascha asks, we need to see the function getAllPurchase() which I'm guessing can return null or some other non-object result in some circumstances. Because it's not returning an object, you cannot call any properties or methods on anything returned from this method, stored in your case in $allPayList and is why PHP tells you so in its error message.

The easy "fix" is to test $allPayList->res using isset($allPayList->res) or !empty($allPayList->res), if either of these return true, only then can you actually call $allPayList->res.

The better fix, is to ensure you do a better job of returning something meaningful in getAllPurchase() i.e. an empty instance of a return object so that there is always a res property available and it's that that might be true, false or possibly null

theruss
  • 1,690
  • 1
  • 12
  • 18
0

$allPayList is an ARRAY and u use it as a object.

First check all variables inside the $allPayList with print_r() method. Then u will easily find out the hierarchy of the array.

Cheetah
  • 71
  • 1
  • 2
0

Try to use a proper approach to return value from the Payables class getAllPurchase() method. In your code, static method getAllPurchase() in some case returns Object (instance) of Payables class & in other case it returns Array of Objects of Payables class.

So, use an approach to check for returned value in $allPayListing variable is an array of objects or an object of Payables class:

Use below code to by-pass the error occured at line 121:

    if(!is_array($allPayListing))
    {
        if(isset($allPayListing->res) && $allPayListing->res == 0) {
            echo "";    
        }
    }
Sharry India
  • 341
  • 1
  • 9
0

I finally solved it. Thanks to all of your help.

It turns out, since the getAllPurchase() function is returning 2 records, the condition creates an array of objects. Since it is an array, It cannot be read as object.

The first function getCurrentPayable() function only returns 1 record thus the condition creates an object.

Here is the updated working code.

public function allpay() {

    $allPayListing = Payables::getAllPurchase();

    echo "
        <div class='col-md-12'>
            <table id='allpay' class='display' cellspacing='0' width='100%'>
                <thead>
                    <tr>
                        <th>Sequence</th>
                        <th>Date</th>
                        <th>Vendor</th>
                        <th>Reference No.</th>
                        <th class='text-right'>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
        ";

        if(!is_array($allPayListing)) {
            if(isset($allPayListing->res) && $allPayListing->res == 0) {
                echo "";    
            }
            else {

                echo "<tr>
                        <td>" . $allPayList->seq . "</td>
                        <td>" . $allPayList->pdate . "</td>
                        <td>" . $allPayList->vendor . "</td>
                        <td>" . $allPayList->refno . "</td>
                        <td class='text-right'>" . number_format($allPayList->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $allPayList->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }
        }
        else {

            foreach($allPayListing as $aPL) {
                echo "<tr>
                        <td>" . $aPL->seq . "</td>
                        <td>" . $aPL->pdate . "</td>
                        <td>" . $aPL->vendor . "</td>
                        <td>" . $aPL->refno . "</td>
                        <td class='text-right'>" . number_format($aPL->amount,2) . "</td>
                        <td>
                            <a OnClick='showCurPayDetails(". $aPL->seq .")' class='btn btn-sm btn-default'>
                            <span class='fa-stack fa-fw'>
                              <i class='fa fa-square-o fa-stack-2x'></i>
                              <i class='fa fa-eye fa-stack-1x'></i>
                            </span>
                            VIEW DETAILS
                        </a>
                        </td>
                    </tr>";
            }

        }

    echo "
                </tbody>
            </table>
        </div>
        ";
}

I first counted the contents of $allPayListing then throw the condition that if it is more than 0 or 1 then it means its an array. I have to loop through the array first to access the objects.

rainbasa
  • 69
  • 8
  • Instead of saying 'I finally solved it' and writing your own answer, could you possibly accredit the person who did actually solve it and help you (i.e. me) by at least acknowledging the source of your solution in your answer; or better yet, accept my answer? Much appreciated – Callan Heard Jun 29 '16 at 13:03