Skip to content

Commit

Permalink
now even users without crops show up.
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsalisbury committed May 14, 2024
1 parent 6d6a88b commit cd585e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 6 additions & 5 deletions public/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,12 @@ function get_all_users()
{
global $db;
// $results = $db->query('SELECT * FROM users');
$sql = 'SELECT users.*, GROUP_CONCAT(crops.crop, char(10)) as crops
FROM users
LEFT JOIN user_crops ON users.id = user_crops.user_id
LEFT JOIN crops ON user_crops.crop_id = crops.id
GROUP BY users.id';
$sql = 'SELECT users.*, IFNULL(GROUP_CONCAT(crops.crop, char(10)), "") as crops
FROM users
LEFT JOIN user_crops ON users.id = user_crops.user_id
LEFT JOIN crops ON user_crops.crop_id = crops.id
GROUP BY users.id';

$results = $db->query($sql);

if ($results === false) {
Expand Down
12 changes: 11 additions & 1 deletion public/pages/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@
$users = get_all_users();
foreach ($users as $user) {
$crops = isset($user['crops']) ? explode("\n", $user['crops']) : [];

// if the user has no crops, display an empty row

foreach ($crops as $index => $crop) {
echo '<tr>';
if ($index == 0) {
Expand All @@ -250,7 +253,14 @@
echo '<td rowspan="' . count($crops) . '">' . htmlspecialchars($user['lastname']) . '</td>';
echo '<td rowspan="' . count($crops) . '">' . htmlspecialchars($user['email']) . '</td>';
}
echo '<td>' . htmlspecialchars($crop) . '</td>';
echo '<td>';

if ($crop) {
echo htmlspecialchars($crop);
} else {
echo '<div class="text-muted">No crops selected</div>';
}
echo '</td>';
if ($index == 0) {
// Only display the delete button in the first row
echo '<td rowspan="' . count($crops) . '"><a href="/users/delete/' . $user['id'] . '" class="btn btn-danger">Delete</a></td>';
Expand Down

0 comments on commit cd585e7

Please sign in to comment.