Skip to content

Commit 85a8721

Browse files
committed
THANK YOU JASON!!!
1 parent cce8717 commit 85a8721

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

codeup_todo_list/todo.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,57 @@
33
// Create array to hold list of todo items
44
$items = array();
55

6-
// The loop!
7-
do {
6+
// List array items formatted for CLI
7+
// Return string of list items separated by newlines.
8+
// Should be listed [KEY] Value like this:
9+
// [1] TODO item 1
10+
// [2] TODO item 2 - blah
11+
// DO NOT USE ECHO, USE RETURN
12+
13+
function list_items($list) {
14+
$place = '';
815
// Iterate through list items
9-
foreach ($items as $key => $item) {
10-
$key++;
11-
// Display each item and a newline
12-
echo "[{$key}] {$item}\n";
16+
foreach ($list as $key => $item) {
17+
$place .= "[" . ++$key . "]" . " " . $item . PHP_EOL;
18+
// Display each item and a newline
19+
}
20+
return $place;
21+
}
22+
23+
// Get STDIN, strip whitespace and newlines,
24+
// and convert to uppercase if $upper is true
25+
function get_input($upper = FALSE) {
26+
// Return filtered STDIN input
27+
if($upper == TRUE) {
28+
return strtoupper(trim(fgets(STDIN)));
29+
} else {
30+
return trim(fgets(STDIN));
1331
}
32+
}
33+
34+
// The loop!
35+
do {
36+
// Echo the list produced by the function
37+
echo list_items($items);
1438

1539
// Show the menu options
1640
echo '(N)ew item, (R)emove item, (Q)uit : ';
1741

1842
// Get the input from user
1943
// Use trim() to remove whitespace and newlines
20-
$input = trim(fgets(STDIN));
44+
$input = get_input(true);
2145

2246
// Check for actionable input
2347
if ($input == 'N' || $input == 'n') {
2448
// Ask for entry
2549
echo 'Enter item: ';
2650
// Add entry to list array
27-
$items[] = trim(fgets(STDIN));
51+
$items[] = get_input();
2852
} elseif ($input == 'R' || $input == 'r') {
2953
// Remove which item?
3054
echo 'Enter item number to remove: ';
3155
// Get array key
32-
$key = trim(fgets(STDIN));
56+
$key = get_input();
3357
// Remove from array
3458
unset($items[--$key]);
3559
}

0 commit comments

Comments
 (0)