I am trying to get all comics owned by a user to appear within the foreach loop I created however it is just doing the else part of the statement. I can’t work out why it isn’t showing what I want to appear if you could have a look and any ideas would be helpful.
Model
ComicData.php
[php]<?php
class ComicData {
protected $comicID, $issue, $yearReleased, $name, $writers, $universe, $image;
//database fields
public function __construct($dbrow) {
$this->comicID = $dbrow['ComicID'];
$this->issue = $dbrow['Issue'];
$this->yearReleased = $dbrow['YearReleased'];
$this->name = $dbrow['Name'];
$this->writers = $dbrow['Writers'];
$this->universe = $dbrow['Universe'];
$this->image = $dbrow['Image'];
}
//returning comic id
function getComicID() {
return $this->comicID;
}
//returning issue
function getIssue() {
return $this->issue;
}
//returning year
function getYearReleased() {
return $this->yearReleased;
}
//returning name
function getName() {
return $this->name;
}
//returning writers
function getWriters(){
return $this->writers;
}
//returning universe
function getUniverse(){
return $this->universe;
}
//returning image
function getImage(){
return $this->image;
}[/php]
ComicDataSet.php
[php]<?php
require_once(‘Model/Database.php’);
require_once(‘Model/ComicData.php’);
class ComicDataSet {
protected $_dbHandle, $_dbInstance = null;
//get instance of database
public function __construct() {
$this->_dbInstance = Database::getInstance();
$this->_dbHandle = $this->_dbInstance->getdbConnection();
}
//function for creating comic entry with parameters
public function addComic($issue, $yearReleased, $name, $writers, $universe, $image, $owner) {
//SQL Query inserting information into entry table
$sqlQuery = "INSERT INTO comics(Issue,YearReleased,Name,Writers,Universe,Image, Author)VALUES('$issue', '$yearReleased', '$name', '$writers', '$universe','$image','$owner')";
// prepare a PDO statement
$statement = $this->_dbHandle->prepare($sqlQuery);
// execute PDO statement
$statement->execute();
}
//function for showing each comic
public function showComic($UserID) {
//declare new array
$comic = array();
//SQL Query Selecting equivalent information to be displayed
$sqlQuery = 'SELECT Issue,YearReleased,Name,Writers,Universe,Image, FROM comics WHERE author ="' . $UserID . '" ORDER BY ComicID ASC';
// prepare a PDO statement
$statement = $this->_dbHandle->prepare($sqlQuery);
// Execute PDO statement
$statement->execute();
// While loop fetches each row matching query
while ($row = $statement->fetch()) {
$comic[] = array('comicID' => $row['ComicID'],
'issue' => $row['Issue'],
'UserID' => $UserID,
'yearReleased' => $row['YearReleased'],
'name' => $row['Name'],
'writers' => $row['Writers'],
'universe' => $row['Universe'],
'image' => $row['Image']
);
}
//returning comic array
return $comic;
}
[/php]
View
home.phtml
[php]<?php require('template/header.phtml') ?>
<?php if (isset($_SESSION['error'])) { echo ' <!-- Collect the nav links, forms, and other content for toggling -->
<!-- Search -->
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search For Comic">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<strong><p class="navbar-text">Sort Comics By : </p></strong>
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Order Comics<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Ascending Order</a></li>
<li><a href="#">Descending Order</a></li>
<li><a href="#">Numerical Order</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-left">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Universe<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Dark Horse Comics</a></li>
<li><a href="#">DC</a></li>
<li><a href="#">IDW</a></li>
<li><a href="#">Image</a></li>
<li><a href="#">Marvel</a></li>
<li><a href="#">Valiant</a></li>
</ul>
</div>
</div>
<div class="col-md-12">
<br>
<!--/.Panel for Comics -->
<!-- Count for Comics -->
<?php
if(count($comics)){
?>
<div class="panel panel-default">
<div class="panel-heading panel-heading-green">
<h3 class="panel-title">My Comics</h3>
</div>
<!--/.row1 -->
<!-- Display each comic by id -->
<?php foreach($comics as $list): ?>
<div class="panel-body">
<div class="row">
<div class="col-md-3 comic">
<a href="product.html">
<?= ($list['Image'] <> "" ? "<img style='max-width:200px; max-height:200px;' src='Images/{$list['image']}'/>" : "") ?>
</a>
<div class="comic-title">
<?= ($list['Name'] <> "" ? $comic['name'] : "") ?>
</div>
<div class="comic-add">
</div>
</div>
<!-- End of Foreach Statement for Comics -->
<?php endforeach; ?>
<!-- Else show this message if user has no entries -->
<?php
}else {
?>
<p><b>You haven't posted any comic yet!</b></p>
<?php } ?>
</div>
<?php require('template/footer.phtml') ?>[/php]
Controller
home.php
[php]<?php
session_start();
require_once(‘Model/LoginDataSet.php’);
require_once(‘Model/ComicDataSet.php’);
$view = new stdClass();
$view->pageTitle = ‘Profile Page’;
$comic = new ComicDataSet();
$comics = array();
$comics = $comic->showComic($_SESSION[‘Email’]);
require_once(‘View/home.phtml’);[/php]
Any ideas on what i am not doing correctly ?