PHP - How to Change Results from While Loop if Any Row Result Matches

18
March 16, 2019, at 01:20 AM

I've looked for the result for this, and can't seem to find anything dealing with this specific issue...

I'm using a while loop to template HTML from my MySQL table.

Everything works, but I can't figure out how to say 'if any row result in the return from the while loop equals 'true' change it to 'checked'.

So I've got for example:

$result = mysqli_query($conn, "SELECT * FROM hardware_requests");

    while($row = mysqli_fetch_array($result)) {       
     echo "<input  type='checkbox' " . $row["checkbox1"] . "/>
           <input  type='checkbox' " . $row["checkbox2"] . "/> ";
    }

The result of $row[checkbox1] is 'true' and the result of $row[checkbox2] is null (in the table there's no entry for that cell)

For the life of me, I can't figure out how, inside the while loop, to do something like:

if ($row[] == 'true') {
   $row[] = 'checked';
}

With $row[] checking every record in the database.

...

I've edited this (initially quotes around "checkbox1" and "checkbox2" in the HTML were missing...

To clarify more, the developer who initially set up the code had checkbox ticks on the front end entered into the database as 'true' (and now there's quite a robust database of filled out checkboxes).

Some pieces of what I'm trying to complete have like 30+ checkboxes, and I want to be able to set the property 'checked' for the input HTML code (in setting up something that is going to be a tracking system for what's been completed based on the input to the database).

So instead of having to individually set each checkbox, i.e.

$row["checkbox1"] = 'checked',
$row["checkbox2"] = 'checked'

up to 30+ checkboxes, I was hoping to just loop through every row and use an if statement to find out which currently equal 'true', and if so, change to 'checked'.

Thanks again for the help!

Answer 1

One possibility is to convert the boolean values to strings in your query.

$result = mysqli_query($conn,
 "SELECT
   IF(checkbox1,'checked','') AS checkbox1,
   IF(checkbox2,'checked','') AS checkbox2
 FROM hardware_requests");

Then your template can stay the same. You should quote those array keys, though.

while($row = mysqli_fetch_array($result)) {
     echo "<input  type='checkbox' " . $row['checkbox1'] . "/>
           <input  type='checkbox' " . $row['checkbox2'] . "/> ";
}
Answer 2

You can use a conditional expression.

while($row = mysqli_fetch_array($result)) {       
     echo "<input  type='checkbox' " . ($row['checkbox1'] == "true" ? "checked" : "")  . "/>
           <input  type='checkbox' " . ($row['checkbox2'] == "true" ? "checked" : ""). "/> ";
}
READ ALSO
Apache2 + PHP-FPM - undefined function apache_getenv()

Apache2 + PHP-FPM - undefined function apache_getenv()

I have a big problem because for several hours I am trying to run a project that uses SetENV in PHPWhen he even tries to use a simple function apache_getenv();

28
How to definte variables for fields without data in them?

How to definte variables for fields without data in them?

I've got a number of calls to data in a WordPress site using a number of custom fieldsThe fields aren't required to fill out on the back end

13
laravel join 4 tables

laravel join 4 tables

I started to learn the Laravel 58 framework and got a problem

29
Multiple search from the database [duplicate]

Multiple search from the database [duplicate]

This question already has an answer here:

37