Hi
I'm trying to pull data from a database, it's working fine, i know this because I echo the values.
There is however an issue with the array that's causing me stress. Basically, as I pull the values from each row in the database I'm adding them to values and pushing them into an array, the code marked below shows the code and location of the echo:
<?php
class page
{
private $articleList = array();
function getPageTitles()
{
# Get classes
require('includes/erroroutput.class.php');
require('includes/db.class.php');
# Create private instances of classes
$error = new erroroutput;
$db = new db;
# Make connection and retreive data
if(!$db->connect_db())
{
$error->addError("Database connection failed.");
}
else
{
$sql = "SELECT `id`, `title` FROM `articles` ORDER BY `id` DESC";
if( $result = $db->query_db($sql) )
{
while($row = $db->fetch_db_array($result))
{
$article->id = $row['id'];
$article->title = $row['title'];
$this->articleList[] = $article;
echo $article->id." "; // ECHO IS HERE
echo $article->title."<br>"; // ECHO IS HERE
}
}
else
{
$error->addError("Information could not be added");
}
}
}
function displayLI()
{
foreach($this->articleList as $article)
{
echo "<li id='".$article->id."'>".$article->title."</li>\n";
}
}
}
?>
My problem is that when I call the array values using displayLI() it shows the last value only but as many times as there are records. For example, if I had articles with the following db values:
ID, TITLE
1, This is an example title
2, PHP Builder is awesome
3, I love PHP
4, Cheese on toast
When I call the displayLI() i get
4, Cheese on toast
4, Cheese on toast
4, Cheese on toast
4, Cheese on toast
This is the total number of records in the table, but every value has assumed the name of article ID 4 with the title "Cheese on toast".
Please help, i'm stuck and it's seriously bugging me!!!
Thanks in advance!