0

This is my code for reading a CSV file using an array. But it shows an error called Undefined offset:1 Please can anyone say what is wrong with my code? Also I want to print the result in an HTML table. How can I do that?

class ProjectController extends Controller
{
 public function index($id)
{
    $project = Project::find($id);
    $data = array();
    // read the CSV and assign the data to $data
    $filename = public_path("/uploads/{$id}.csv");
    $file = fopen($filename, "r");
    $all_data = array();
    while (($data = fgetcsv($file, 200, ",")) !== FALSE) {

        $Keywords = $data[0];
        $Avg_search_volumn = $data[1];
        $Top_of_the_page_bid_low = $data[2];
        $all_data = $Keywords . " " . $Avg_search_volumn . " " . $Top_of_the_page_bid_low;

        array_push($array, $all_data);
    }
    fclose($file);

    echo "<pre>";
    print_r($all_data);
    die;
    return view('home')->with('project', $project)->with('data', $data);
   }
  }
Perera
  • 21
  • 5
  • Can you please edit your question and show us the output of `var_dump($data);` for the first and second iteration of your while loop? Place this at the very top of your loop. – waterloomatt Dec 07 '20 at 02:40
  • Don't use code like `$Avg_search_volumn = $data[1];` if you don't know that `$data` has that many elements. Check count of `$data` before trying to access elements. – miken32 Dec 07 '20 at 03:15

0 Answers0