Hi guys.
I am currently working on an application for a customer.
The problem I am having is in regards to 2 extracts of code.
The first extract is the code I am using to fetch all of the rows from a table called 'gifsleft':
The entire class file is below:
gifclass.php:
<?php
require_once("dataobject.php");
class gif extends dataObject{
protected $data = array(
"id" => "",
"gifnameleft" => "",
"gifnameright" => "",
"urltext" => ""
);
public static function getLeftGifs(){
$conn = parent::connect();
$sql = "SELECT * FROM gifsleft ORDER BY id DESC LIMIT 4";
$st = $conn->query($sql);
$leftgifs = array();
foreach( $st->fetchAll() as $gif ){
$leftgifs[] = new gif($gif);
}
return $leftgifs;
}
}
?>
....... and the code of the class it requires is below:
dataobject.php
<?php
require_once("config.php");
abstract class dataObject{
protected $data = array();
public function __construct( $data ){
foreach( $data as $key => $value ){
if( array_key_exists( $key, $this->data ) ){
$this->data[$key] = $value;
}
}
}
public function getValue($field){
if( array_key_exists( $field, $this->data ) ){
return $this->data[$field];
} else{
die("Field not found!");
}
}
public function getValueEncoded( $field ){
return htmlentities( $this->getValue( $field ) );
}
protected static function connect(){
try{
$conn = new PDO( DSN, USERNAME, PASSWORD );
$conn->setAttribute( PDO::ATTR_PERSISTENT, true );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ){
die( "connection failed " . $e );
}
return $conn;
}
protected static function disconnect( $conn ){
return $conn = "";
}
}
?>
...... The second extract - the code snippet I am using to try and fetch all of the rows from the database is below:
From my 'index.php' page:
<div class="col-lg-2 col-md-2 leftcol">
<?php
list( $leftGifs ) = gif::getLeftGifs();
foreach($leftGifs as $leftGif){
echo $leftGif->getValue('urltext');
}
?>
</div>
This echos absolutely nothing!
It should run through every row and echo all the different url addresses saved on my database.
..... But for some strange reason I am able to retrieve just one value (the newest field value from the latest row) with the code below:
<div class="col-lg-2 col-md-2 leftcol">
<?php
list( $leftGifs ) = gif::getLeftGifs();
echo $leftGifs->getValue('urltext');
?>
</div>
It Just doesn't make any sense, as I have used this same techinique in the past and it has never failed me!
Can someone tell me where I am going wrong?
Thanks.
Paul.