Reputation:
So I have an array and form with input where I put an ID that should show the corresponding row from the array.
So far I have made this:
<?php
$file = 'people.txt';
$people = array();
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode("|", $row);
$id = $row[0];
$name = $row[1];
$status = $row[2];
$people[$id] = array('name' => $name, 'status' => $status);
}
fclose($ref);
foreach($people as $id => $person) {
if($_GET[person]==$id && $person[status]==1) {
echo $person[name] . "(" . $id . "): ready";
}
if($_GET[person]==$id && $person[status]==0) {
echo $person[name] . "(" . $id . "): not ready";
}
if($_GET[person]!=$id) {
echo "Person with this ID was not found";
}
}
?>
This shows the correct info of searched ID, but it also shows "person was not found" to every other person in the array. How can I get it to show only the person that matches the input?
Upvotes: 0
Views: 86
Reputation: 305
Since you are only searching for one "person ID", you do not need the second foreach loop in your code. I recommend refactoring a bit to include more error checking and also reading about type juggling and casting in PHP.
I've modified your code to include some hints that should point you in the right direction:
<?php
$file = 'people.txt';
$people = array();
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode('|', $row);
if (!isset($row[0]) || !isset($row[1]) || !isset($row[2])) {
continue;
}
$id = (int) $row[0];
$name = (string) $row[1];
$status = (int) $row[2];
$people[$id] = array('name' => $name, 'status' => $status);
}
fclose($ref);
$id = array_key_exists('person', $_GET) ? $_GET['person'] : null;
if (array_key_exists($id, $people)) {
$person = $people[$id];
if ($person['status'] === 1) {
echo $person['name'] . ' (' . $id . '): ready';
} else {
echo $person['name'] . ' (' . $id . '): not ready';
}
} else {
echo 'Person with ID ' . htmlspecialchars($id) . ' not found';
}
If you plan on people.txt becoming large in size, consider storing the data in a database such as MySQL. This will eliminate having to read the contents of the file into memory on every request.
Upvotes: 1
Reputation: 41810
Unless you need the array of all the people for some other purpose after this code, you can get only the person indicated by $_GET['person']
from the file. Just add a check inside the while loop.
$person = null; // initialize person
$ref = fopen($file, 'r');
while ($row = fgets($ref, 1024)) {
$row = explode("|", $row);
if ($row[0] == $_GET['person']) { // only get rows that match the searched id
$person = array('name' => $row[1], 'status' => $row[2]);
}
}
fclose($ref);
This will reduce the memory usage of your script by avoiding creating an array of the entire file. The benefit of this obviously depends on the size of the file.
(Even if you do need the entire array, you can still set $person
in an if
block in your while loop at the same time you are filling the $people
array.)
Then after that loop, $person
will be just one thing: either an array with the last matched row in your file, or null
. That means you won't have to find it in an array; you can output the status information like this:
if ($person) {
echo "$person[name] ($person_id): ";
echo $person['status'] ? 'ready' : 'not ready';
} else {
echo "Person with this ID was not found";
}
Upvotes: 1
Reputation: 820
Use a boolean variable to detect if the person you're searching for is found or not, The code could be like this:
$found = false;
foreach($people as $id => $person) {
if($_GET[person]==$id) {
$found = true;
if($person[status]==1)
echo $person[name] . "(" . $id . "): ready";
else if($person[status]==0)
echo $person[name] . "(" . $id . "): not ready";
//You can (break;) here if you're sure that the $id is identical
}
}
if($found == false)
echo "Person with this ID was not found";
Upvotes: -2
Reputation: 360692
There's no need AT ALL for the loop. If you already have the ID of the person, then there's no need to scan the array for that id, just use it directly:
if (isset($people[$id])) {
... do stuff with person # $id
} else {
... person does not exist
}
Upvotes: 4