I call the getTitle function but it only returns the last DB value. I presume that is because there are many rows in the DB with the same 'code' However when I call the function I want to get all the values for TITLE where code is something in the DB not just the last one.

Help Please! Thank you very much!!!

class Entry
    {

        private $title;
        private $date;
        private $place;
        private $description;
        private $code;

        function __construct($c)
	{
		$this->code=$c;
		$dbc=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Failed'.mysql_error());
		mysql_select_db(DB_NAME);
		$query="SELECT TITLE FROM entry WHERE CODE='".$c."'";
		$result=mysql_query($query);
			while($row=mysql_fetch_array($result))
				{
				$this->title=$row['TITLE'];
				
				}
		mysql_close();
	}

        function getTitle()
        {
            return $this->title;
        }






    }
?>

Recommended Answers

All 8 Replies

Member Avatar for rajarajan2017

Check your query returning more rows or not by

if (mysql_num_rows($result) > 1) { echo "returned more than one row" }
else echo "no rows retuned"
$this->title=$row['TITLE'];

The above code is a single title variable should not hold many row values. So make that variable as an array and push whatever values you have in your database using that while loop

You can mak this->title an array

class Entry
   {
   private $title = array();

change the constuctor:

$result=mysql_query($query);
while($row=mysql_fetch_array($result))
   {
   $this->title[]=$row['TITLE'];
   }

Hi pzuurveen.

thanks for the help.

How should I now change the getTitle function?

Should I use the tooString() function?

Thanks for the help!

why would you change the getTitle() function? its fine already, it just a matter of how you retrieve it

so you retrieve it like this, first lets instantiate you class Entry

<?php
$obj = new Entry(); // instantiate class Entry
$title = $obj->getTitle(); // get array title

foreach($title as $val){ // loops through all array 
echo $val."<br />"; // test result
}
?>

Thanks so much!!!!!!

Would not be able to do this without you guys!!!

Vaultdweller-> The reason why I wanted to change the getTitle fuction is beacuse I thought that maybe one could do the for each inside your class...but its working now so thanks a lot! (I am a PHP newbie)

$p->setContent('<table border="1">

<tr><td>Info: '.$u->getInfo().'</td>
<td>#####</td></tr></table>')

How can I get the result ie $val to display where the ##### is in the code above?

<?php

$obj = new Entry(); // instantiate class Entry
$title = $obj->getTitle(); // get array title
$content = ""; 

foreach($title as $val){ // loops through all array 
$content += "<td>".$val."</td>"; 
}

$p->setContent('<table border="1">

<tr><td>Info: '.$u->getInfo().'</td>
'.$content.'</tr></table>');


?>

Is there any way in which I can turn the $content into a link to new unique page which draws info from the database?

for example: $content is the posts users have made and is displayed in a list on his home page but I want them to be able to click on it as well and take the user to a new page...

Thank you for your help VD.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.