0
    <?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 ?????
4
Contributors
9
Replies
27
Views
4 Years
Discussion Span
Last Post by diafol
1

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);
    ?>
0

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

Edited by kakalahori

0

diafol >> still it remains the same

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

would there be a possible error of installation

0

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

0
<?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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.