<?php
    // Dump the games.csv file into an array
    $games = fgets('file/games.csv');
    // Parse the array
    foreach($games as $game)
    {
    echo "{$game}";
    }
    ?>



Error i get 
Warning: fgets() expects parameter 1 to be resource, string given in C:\wamp\www\test\peartest.php on line 3
Call Stack


    #   Time    Memory  Function    Location


1   0.0004  671880  {main}( )   ..\peartest.php:0
2   0.0004  672008  fgets ( )   ..\peartest.php:3

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\test\peartest.php on line 5
Call Stack
#   Time    Memory  Function    Location
1   0.0004  671880  {main}( )   ..\peartest.php:0

------------------------------
Any solution to it ?????

You need to fopen the file first, see the example in the manual.

I think you have to define a variable of type file and pass it as parameter to the fgets():

<?php
    $file = fopen("file/games.csv","r");

    // Dump the games.csv file into an array
    $games = fgets($file);
    // Parse the array
    foreach($games as $game)
    {
    echo "{$game}";
    }

    fclose($file);
    ?>
Member Avatar for diafol

Why not use fgetcsv()?

cmps >> ok it works but it doesnot shoes the full result .... data is found missing
pritaeas >> i was actually uing file() and i refered the fget() from the manual ..im a biggener so can u please fix the codes in the script so that i can study it

diafol >> wait let me try it

diafol >> still it remains the same

-------------

would there be a possible error of installation

ok here it is

<?php
  $file = fopen("file/games.csv","r");
// Dump the games.csv file into an array
$games = fgetcsv($file);
// Parse the array
foreach($games as $game){
echo "{$game}<br />";
}
fclose($file)
?>

-------------------

the out put is
Title
Price
Publisher
Commetns
additonal

while the data below it is missing

<?php
 $file = fopen("file/games.csv","r");
 // Make HTML_Table available to the script
 require 'HTML/Table.php';

 // Dump games.csv into an array
$games = fgetcsv($file);

// Set Table CSS class
$attrs = array('class' => 'game');
// Create a new instance of the HTML_Table class
$table = new HTML_Table($attrs);
// Output the header
$header = explode(',', $games[0]);
for($header_count = 0; $header_count < count($header); $header_count++) {
$table->setHeaderContents(0, $header_count, $header[$header_count]);
}

// Set Header CSS class
 $table->setRowAttributes(0, array('class' => 'header'));

 // Parse the array
 for($row_count = 1; $row_count < count($games); $row_count++) {

 // Convert each line into an array
 $game = explode(',', $games[$row_count]);

 // Set Row CSS class
 $table->setRowAttributes($row_count, array('class' => 'row'));

 // Output the next row
 for($col_count = 0; $col_count < count($game); $col_count++) {
 $table->setCellContents($row_count, $col_count, $game[$col_count]);
 }

 }

 // Set attributes for alternating rows
 $altRow = array('class' => 'altrow');
 $table->altRowAttributes(1, null, $altRow);

 // Output the HTML table to the browser
 echo $table->toHtml();
fclose($file)
 ?>

please any one check this code

Member Avatar for diafol

This is a copy of the first example on php.net maual entry on fgetcsv:

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

The while loop should set each 'line' as $data array.
The foreach (or 'for' in this case), gives each item in that row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.