magzaloo Posted December 13, 2012 Share Posted December 13, 2012 Hi there. I am just learning php and I am trying to create a page which displays categories and their books. So, there would be 3 categories per line, then another three, and so on and so on. This information is pulled from the database. I still have a bit of work, but I have run into a snag which has halted my efforts. The code below gives me an error " Can't use function return value". This error is located in the foreach loop. I know there is a lot of knowledge on this forum. I hope someone can help. <?php require("connect.php"); class Book { private $book_ID; private $book_title; private $book_author; private $book_category; public function __constructor($row) { $this->book_ID = $row['id']; $this->book_title = $row['title']; $this->book_author = $row['author']; $this->book_category = $row['category']; } public function show_book() { echo "<strong>" . $this->book_title($row) . "</strong><br />"; } private function get_category_name($id) { $query_category_id = "SELECT name FROM tCategory WHERE ID = '$id'"; $result = mysql_query($query_category_id); } } ?> <h1>Categories</h1> <?php //ERROR: Can't use function return value. foreach ($row as mysql_fetch_array($result, MYSQL_ASSOC)) { $book = new Book($row); $book->show_book(); } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 13, 2012 Share Posted December 13, 2012 (edited) The error is exactly as it states "Can't use function return value" A foreach loop is used to iterate over the elements in an array, correct? You used mysql_fetch_array() as the array parameter in the foreach() loop. mysql_fetch_array() is a function As the error states you cannot use the return value from a function for the array parameter in a foreach loop. But, looking at your code there wouldn't be anything returned from mysql_fetch_array() anyway since you never ran a query. You have a query to run in the class, but you never even initiated the class. EDIT: after looking at your code a little closer, it is full of problems. The constructor of the class assumes there are some values to assign to the properties of the class. But, it seems you are expecting to get those properties after running the method get_category_name(). You are putting the cart before the horse! I was going to try and rewrite it for you, but there isn't enough information. Please provide the details of the category table and the book table. Edited December 13, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
magzaloo Posted December 13, 2012 Author Share Posted December 13, 2012 Thank you so much for your help. It's hard for me to wrap my head around some of this at times. CREATE TABLE `pdn`.`tcategory` ( `id` INT NOT NULL , `name` VARCHAR( 200 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB; CREATE TABLE `tBook` ( `id` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY(id), `title` VARCHAR( 100 ) NOT NULL , `author` VARCHAR( 80 ) not null, `category' varchar(80)not null, `pubyr` INT, `pgs` int, `hrs` decimal(10,0), `numpdffiles'int, `engdesc` VARCHAR( 9000 ) not null, `poldesc` VARCHAR( 9000 ) NOT NULL , `filename` VARCHAR( 20 ) NOT NULL, `filext` VARCHAR( 3 ) NOT NULL, `imgext` VARCHAR( 3 ) NOT NULL, `imgname` VARCHAR( 25 ) NOT NULL, `pages` VARCHAR( 4 ) NOT NULL, `storelink` VARCHAR( 400) NOT NULL '] ; Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 13, 2012 Share Posted December 13, 2012 Well, I'm not really sure what you were trying to accomplish. You were outputting a header for categories and then it looks like you were attempting to output book titles. But, the following query will return a list of category names and the associated book titles. The last line can be used to only get the books from a particular category SELECT tcategory.name, tBook.title FROM tcategory LEFT JOIN tBook ON tBook.category = tcategory.id -- WHERE tcategory.id = $id Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.