Jump to content


Photo

Can't Display Books According To Category - Help Debug


  • Please log in to reply
3 replies to this topic

#1 magzaloo

magzaloo

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 13 December 2012 - 12:11 PM

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();
}
?>


#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,384 posts
  • LocationCanada

Posted 13 December 2012 - 12:28 PM

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 by Psycho, 13 December 2012 - 12:34 PM.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 magzaloo

magzaloo

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 13 December 2012 - 01:34 PM

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

'] ;

#4 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,384 posts
  • LocationCanada

Posted 13 December 2012 - 02:28 PM

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

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com