Reputation: 188
I am searching a table in my DB
to return a tick if a name matches a $_GET['name']
variable that I have passed in the URL.
I want a tick displayed if the name exists and a cross if it does not.
The purpose is to populate a rota / timetable of availability.
The code I have so far;
<?php foreach($rota_sun as $rota_sunday): ?>
<?php if(strpos($rota_sunday['person_name'], $_GET['name']) !== false) { ?>
<span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
<?php } else { ?>
<span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
<?php } ?>
<?php endforeach; ?>
My code for the query is;
$query = "SELECT
rota_sunday.id AS rota_id,
rota_sunday.person_id,
rota_sunday.person_name
FROM rota_sunday WHERE rota_sunday.person_name = '$_GET['$name']'
";
try
{
$stmt = $conn->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rota_sun = $stmt->fetchAll(PDO::FETCH_ASSOC);
My table contains three rows;
The array I'm using is;
Array
(
[0] => Array
(
[rota_id] => 16
[person_id] => 0
[person_name] => Gina
)
)
You'll see that Gina exists - so if ?name=Gina
is in my URL a tick should be shown but ?name=Fred
or ?name=John
etc is in my URL a cross should be displayed.
Issue: The tick is displaying when the name exists but the cross isn't displaying when the name doesn't exist.
Upvotes: 1
Views: 74
Reputation: 72269
A working example:-
<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">
<?php
error_reporting(E_ALL); //check all type of errors
ini_set('display_errors',1); // display those if any happen
$_GET['name'] = 'fiksun';
$rota_sun = Array
(
'0' => Array
(
'rota_id' => 16,
'person_id' => 0,
'person_name' => 'Gina'
),
'1' => Array
(
'rota_id' => 16,
'person_id' => 0,
'person_name' => 'fiksun'
)
)
?>
<?php foreach($rota_sun as $rota_sunday): ?>
<?php if($rota_sunday['person_name']== $_GET['name']) { ?>
<span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
<?php } else { ?>
<span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i><?php echo $rota_sunday['person_name'];?></span>
<?php } ?>
<?php endforeach; ?>
Output:- http://prntscr.com/cf4ecd
Note:- download font-awesome library from here:- http://fontawesome.io/get-started/
Put the complete folder in your current working directory
Add the css file correctly.
this is working directory structure:- http://prntscr.com/cf4fuk
Change your query:-
$query = "SELECT rota_sunday.id AS rota_id, rota_sunday.person_id AS person_id, rota_sunday.person_name AS person_name FROM rota_sunday WHERE rota_sunday.person_name Like '%".$_GET['$name']."%'";
Upvotes: 1
Reputation: 566
This query will select just one person from DB.
$query = "SELECT
rota_sunday.id AS rota_id,
rota_sunday.person_id,
rota_sunday.person_name
FROM rota_sunday WHERE rota_sunday.person_name = " . $_GET['name'] . "LIMIT 1";
try
{
$stmt = $conn->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rota_sun = $stmt->fetch(PDO::FETCH_ASSOC);
If this person exist then $rota_sun will contain result if not then this will return false and you will get the red cross.
<?php if($rota_sun) { ?>
<span style="color:green; font-size:22px;"><i class="fa fa-check" aria-hidden="true"></i></span>
<?php } else { ?>
<span style="color:red; font-size:22px;"><i class="fa fa-times" aria-hidden="true"></i></span>
<?php } ?>
Upvotes: 0