Reputation: 83
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&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&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
Upvotes: 0
Views: 118
Reputation: 780673
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.
Upvotes: 2