2

I'm a newbie in PHP and I'm trying to make a todo list that communicates with a .csv file,. So far I've managed to write a function that writes the user input into the csv file, but I'm stuck on writing a function that would parse (I'm not even sure if this is the correct term) every line of the .csv file into a multi dimensional array, so I could display every line of the list to my convenience in the PHTML file.

Here's what I have so far :

`<?php
//
// ─── DATA ────────────────────────────────────────────────────────────────────
//

$user_entry = array(
    'title' => '',
    'description' => '',
    'date' => '',
    'priority' => ''
);

// puts the data the users entered into an array
$user_entry['title'] = $_POST['title'];
$user_entry['description'] = $_POST['description'];
$user_entry['date'] = $_POST['date'];
$user_entry['priority'] = $_POST['priority'];

//
// ─── FUNCTIONS ──────────────────────────────────────────────────────────────────
//

function writeInList() {
    //parses the $user_entry array into the .csv file
    global $user_entry;
    $file = fopen("todo.csv","a");
    fputcsv($file, $user_entry, ",");
    fclose($file);
}

function displayList() {
    //That's where I'm stuck.
    $file = fopen("todo.csv","r");
    $fileCountable = file("todo.csv");
    for ($i = 0; $i < count($fileCountable); $i++) {
        $csvContent = fgetcsv($file, 1000, ",");
        foreach ($csvContent as $value){
            $var[$i] = $value;
        }
        echo '<br>';
    }
    fclose($file);
}

//
// ─── MAIN CODE ─────────────────────────────────────────────────────────────
//

writeInList();

include 'todolist.phtml';`

I'm sorry if it has been discussed before. I've searched a lot and found similar questions but can't get to make it work in my own code. Thanks a lot in advance if anyone takes the time to take a look at my code !

This is also my very first time posting here so I hope I'm doing it right.

1 Answer 1

2

You did pretty good. You can look at fgetcsv documentation for more. I would have change you function so it will get the argument as input (try avoid using global)

// insert data
function writeInList($user_entry, $path ) {
    $file = fopen($path ,"a");
    fputcsv($file, $user_entry, ",");
    fclose($file);
}

//extract data
function getList($path, $limit = 100000) {
    $file = fopen($path, "r");
    if (!$file) return null; // or throw error or print to log
    $allRows = []; //
    while (($data = fgetcsv($file, $limit, ",")) !== FALSE) {
        $allRows[] = $data; // as fgetcsv return array already exlode by "," 
    }
    fclose($file);
    return $allRows; 
}

Now you have 2-Dim array return from getList. Use is as getList("todo.csv") and display as you pleased.

Hope that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Size limit (second param of fgetcsv()) of 1000 seems to be small. For a general function I would set it to 100000. Alternatively, add a second function param: getList( $path, $limit = 100000 ) and use the param in fgetcsv($file, $limit,",").
Thanks a lot, it works perfectly ! The key here is really the "while (($data = fgetcsv($file, 1000, ",")) !== FALSE)" I guess, although it took me a while to really understand what's going on here. You've helped me a lot anyway, thanks again !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.