Domanda

My problem is that I have no idea how to count my rows of data which have been filtered and output from my CSV file.

I want to do something along the lines of:

$author = $_GET['author'];

$booklist = array();
$title1 = 'Harry Potter and the Deathly Hallows';
array_push($booklist, $title1);
$title2 = 'Harry Potter and the Goblet of Fire';
array_push($booklist, $title2);
$title3 = 'Harry Potter and the Prisoner of Azkaban';
array_push($booklist, $title3);

if (strtolower($author) == strtolower('J.K. Rowling')){
    echo '<h1>', ucwords($author), '</h1>';
    echo "<p>" . $title1 . "</p>";
    echo "<p>" . $title2 . "</p>";
    echo "<p>" . $title3 . "</p>";
    $number = count($booklist);
    echo "<p>We have $number of $author 's books.</p>";
}

Which would output;

J.K. Rowling
Harry Potter and the Deathly Hallows
Harry Potter and the Goblet of Fire
Harry Potter and the Prisoner of Azkaban
We have 3 of J.K. Rowling 's books.

However, my problem is, my values are from a CSV file and the data that is being output has been filtered by an IF Statement.

The CSV file contains values like:

name,author,isbn,price
name,author,isbn,price
name,author,isbn,price
name,author,isbn,price

What I have managed so far is the following:

<?php
$author = $_GET['author'];

echo '<h1>', ucwords($author), '</h1>';
echo '<table border=1>'; //start table
$handle = fopen("books.csv", "r");

while (($books = fgetcsv($handle)) !== FALSE) {  
    list($bookname, $bookauthor, $bookisbn, $bookprice) = $books;

    if (strtolower ($author)==strtolower($bookauthor)) {
        $booklist = array();
        array_push($booklist, $bookname);
        $number = count($booklist);
        echo '<tr><td>',$bookname, //names
        '</td><td>',$bookauthor, //authors
        '</td><td>',$bookisbn, //ISBN
        '</td><td>',$bookprice, //price
        '</td></tr>';
    }
}
fclose($handle);
echo '</table>'; //end table
echo '<p> There are ', $number, ' books by this author. </p>';
?>

and my output is:

J.K. Rowling
title1, J.K. Rowling, ISBN, Price
title2, J.K. Rowling, ISBN, Price
title3, J.K. Rowling, ISBN, Price
There are 1 books by this author. 

It doesn't seem to count.

I am not the best at PHP so some help would be greatly appreciated.

È stato utile?

Soluzione

You are redeclaring $booklist each time through the loop. Move it outside the loop and you should be good.

$booklist = array(); //move here
while (($books = fgetcsv($handle)) !== FALSE) {  
list($bookname, $bookauthor, $bookisbn, $bookprice) = $books;

if (strtolower ($author)==strtolower($bookauthor))
 {
   array_push($booklist, $bookname);
   $number = count($booklist);
   echo '<tr><td>',$bookname, //names
   '</td><td>',$bookauthor, //authors
   '</td><td>',$bookisbn, //ISBN
   '</td><td>',$bookprice, //price
   '</td></tr>';
}
}

Altri suggerimenti

Try moving the following line;

$booklist = array();

to above the while loop

$number is always going to be 1 (or undefined are there are none). Instead of counting the array, set $number=0; before the loop and then use $number++;

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top