Ben Ben - 4 months ago 11
MySQL Question

Can't get simple if / else statement to work

I am searching a table in my

to return a tick if a name matches a
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 AS rota_id,
FROM rota_sunday WHERE rota_sunday.person_name = '$_GET['$name']'
$stmt = $conn->prepare($query);
catch(PDOException $ex)
die("Failed to run query: " . $ex->getMessage());
$rota_sun = $stmt->fetchAll(PDO::FETCH_ASSOC);

My table contains three rows;

  1. id

  2. person_id

  3. person_name

The array I'm using is;

[0] => Array
[rota_id] => 16
[person_id] => 0
[person_name] => Gina


You'll see that Gina exists - so if
is in my URL a tick should be shown but
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.


A working example:-

<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">

  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; ?>


Note:- download font-awesome library from here:-

Put the complete folder in your current working directory

Add the css file correctly.

this is working directory structure:-

Change your query:-

$query = "SELECT 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']."%'";