2013-03-07 4 views
0

. Моя проблема заключается в том, что я не знаю, как подсчитать мои строки данных, которые были отфильтрованы и выведены из моего CSV-файла.Использование PHP для подсчета строк в таблице, в которой данные обрабатываются из файла CSV.

Я хочу сделать что-то вдоль линий:

$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>"; 
} 

Какой бы выход;

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. 

Однако, моя проблема в том, мои значения из файла CSV и данных, которые вывод был фильтруется заявление IF.

Файл CSV содержит значения, такие как:

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

Что мне удалось до сих пор является следующее:

<?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>'; 
?> 

и мой выход:

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. 

Это Безразлично» t, кажется, рассчитывает.

Я не лучший на PHP, поэтому мы будем очень благодарны за помощь.

ответ

0

Вы обновляете код $booklist каждый раз через петлю. Переместите его за пределы цикла, и вы должны быть хорошими.

$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>'; 
} 
} 
+0

он не всегда будет 1, если вы не будете повторно декларировать каждый раз. – Pitchinnate

+0

А, я всегда делаю самые простые ошибки. Спасибо за быстрый ответ! – Deen

0

Попробуйте переместить следующую строку;

$booklist = array(); 

выше время цикла

+0

вы также можете переместить $ number = count ($ booklist); после цикла – splash21

0

$number всегда будет 1 (или не определено в их нет). Вместо подсчета массива установите $number=0; перед циклом, а затем используйте $number++;