0

So basically I've started working with OOP and the script I've wrote based on a tutorial is giving me 2 problems:

When i try to convert an object into a variable i'm getting this error:

Notice: Trying to get property of non-object in /home/stream/V2/templates/homepage.php on line 14

The code on line 14 is:

<a href=".action=viewSeries&amp;seriesId=<?php echo $series->id ?>"><?php echo htmlspecialchars( $series->id ) ?></a>

Second instead of turning the object into a variable and trying to access data directly from it i'm getting nothing.

I basically have 3 pages to help get data from a table:

Homepage.php

    <?php include( "templates/include/header.php" ); ?>

    <ul id="headlines">

    <?php
    echo $results['totalRows'];
    foreach( $results['series'] as $series ){ ?>

    <li>
    <h2>
    <!-- remember to add htmlspecialchars -->
    <span class="title"><?php echo $results['series']->description ?></span> 

    <a href=".action=viewSeries&amp;seriesId=<?php echo $series->id ?>"><?php echo htmlspecialchars( $series->id ) ?></a>
    </h2>
    </li>


    <?php 
    } ?>

    </ul>

    <p><a href="./?action=archive">Series Archive</a></p>
    <?php include( "templates/include/footer.php" ); ?>

index.php

    <?php

require( "core/config.php" );
$action = isset ( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ){
    case 'series':
        series();
    break;

    case 'viewSeries':
        viewSeries();
    break;

    default:
        homepage();
}

function series(){
    $results = array();
    $data = Series::getList();
    $results['series'] = $data['results'];
    $results['totalRows'] = $data['totalRows'];
    $results['pageTitle'] = "Series Title";
    require( TEMPLATE_PATH . "/series.php" );
}   

function viewSeries(){
    if( !isset( $_GET['seriesId'] ) || !$_GET['seriesId'] ){
        homepage();
        return;
    }

    $results = array();
    $results['series'] = Series::getBydId( (int)$_GET['seriesId'] );
    $results['pageTitle'] = $results['series']->title . "| Sub title";
    require( TEMPLATE_PATH . "/viewSeries.php" );
}

function homepage(){
    $results = array();
    $data = Series::getList( HOMEPAGE_NUM_SERIES );
    $results['series'] = $data['results'];
    $results['totalRows'] = $data['totalRows'];
    $results['pageTitle'] = "Streamaton Homepage";
    require( TEMPLATE_PATH . "/homepage.php");
}

And this is the piece of code from the Series.php class:

    //Return all (or range of) Series objects in the db
public static function getList( $numRows=100000, $order="id DESC" ){
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT id, description FROM series ORDER BY :order LIMIT :numRows";
    //SQL_CALC_FOUND_ROWS *
    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->bindValue( ":order", $order, PDO::PARAM_STR );
    $st->execute();
    $list = array();

    while( $row = $st->fetch() ){
        $series = new Series( $row );
        $list = $series;    
    }


    //Now get the total number of series that match the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";
    $totalRows = $conn->query( $sql )->fetch();
    $conn =  null;
    return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );

}

The whole series.php code looks like this: http://pastebin.com/f95A8NPr

tereško
  • 58,060
  • 25
  • 98
  • 150
  • What's in `$results['series']`? – Jay Blanchard Jun 26 '15 at 19:38
  • Shouldn't `$results['series']->description` be `$series->description` ? – Maximus2012 Jun 26 '15 at 19:39
  • 1
    You might also want to try `var_dump($series)` within the `foreach` loop to see what that looks like. – Maximus2012 Jun 26 '15 at 19:40
  • `$results` is a local variable in the functions in `index.php`. You can't access it in `homepage.php`. Also, I don't see where `homepage.php` includes `index.php`. – Barmar Jun 26 '15 at 19:44
  • @JayBlanchard Results from each row in my table – Jonattan Davelaar Jun 26 '15 at 19:47
  • @Barmar It's the index.php that include the homepage within it. Scroll all the way down in the index.php code – Jonattan Davelaar Jun 26 '15 at 19:48
  • @Maximus2012 i've did the var dump with the $series and $results['series'] and i'm getting Null, so i'm guessing that the problem is either in my index.php or series.php – Jonattan Davelaar Jun 26 '15 at 19:49
  • I think you need to include index.php on Homepage.php and call some sort of function that will actually populate the value of `$results` – Maximus2012 Jun 26 '15 at 19:51
  • @Maximus2012 See his response to my comment. It's the other way around. – Barmar Jun 26 '15 at 19:52
  • @Barmar yes you are right. I think Series.php needs to be included/required on index.php then. In either case, I there is a scope to simplify the code. – Maximus2012 Jun 26 '15 at 19:54
  • @Maximus2012 the series.php code is being included in the config.php which is included in the index.php – Jonattan Davelaar Jun 26 '15 at 20:01
  • What does the constructor of your `Series` class look like? Can you paste the complete code for series.php class ? I am not entirely sure if this part of the code: `$series = new Series( $row );` is correct. Maybe you need to break down the `$row` array into individual variables(`$id`, `$description`) and then pass those to the constructor of the `Series()` class ? – Maximus2012 Jun 26 '15 at 20:07
  • This is how the constructor looks like: http://pastebin.com/qSG4jBSa This is all the code for series.php: http://pastebin.com/f95A8NPr – Jonattan Davelaar Jun 26 '15 at 20:10
  • Please update your question to add more code rather than linking to an external website. – Maximus2012 Jun 26 '15 at 20:11
  • The series.php was already in my question, it was added at the end of my question. – Jonattan Davelaar Jun 26 '15 at 20:12
  • 2
    I took a quick look at the code of your constructor and looks like you have `_construct` in place of `__construct` . Do you want to change that and see if it makes any difference in addition to the changes suggested by the answer from @Barmar ? http://php.net/manual/en/language.oop5.decon.php – Maximus2012 Jun 26 '15 at 20:15
  • @Maximus2012 THANK YOU, when i try to using var dump it's actually outputting data. The only problem i have now is when i try access a specific data by doing: description ?> It's still showing up empty – Jonattan Davelaar Jun 26 '15 at 20:25
  • Replace `$results['series']->description` with `$series->description` – Maximus2012 Jun 26 '15 at 20:26
  • Dear @Maximus2012 thanks for spending your valuable time in helping a stranger like me, i'm really grateful for all your help!! PS:It worked. – Jonattan Davelaar Jun 26 '15 at 20:29

1 Answers1

2

This loop is wrong:

while( $row = $st->fetch() ){
    $series = new Series( $row );
    $list = $series;    
}

The last line should be:

    $list[] = $series;

so that it pushes each series onto the array. Your code is replacing the array with a series each time through the loop.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks this answer helped solved a part of my problem, but when i try var dump it's still giving me null. http://pastebin.com/iwW5k3j9 – Jonattan Davelaar Jun 26 '15 at 19:59
  • If everything is successful is it suppose to show no results? Because i'm not getting anything. – Jonattan Davelaar Jun 26 '15 at 20:11
  • If everything is successful then you should definitely see some results. Do you have PHP error reporting turned on for your code in addition to exception handling for PDO ? – Maximus2012 Jun 26 '15 at 20:13
  • @JonattanDavelaar See the comment above about the typo in `__construct()`. – Barmar Jun 26 '15 at 20:18