Jump to content

Can't Display Books According To Category - Help Debug


magzaloo

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

 

'] ;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.