2

I am trying to put some php code in a code that is combined with HTML.

I have the following code:

<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" [IN THIS PLACE] name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>

On the [in this place] spot I want to put the following code:

if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
echo 'checked';
}

I have tried it multiple times but I just can't get it to work.

2
  • Try putting the conditional statement right after your while loop, while assigning a variable to it, like $checked="checked"; then echo/place the variable in the [in this place]. Commented Dec 12, 2014 at 16:21
  • possible duplicate of Selection option "selected" attribute Commented Dec 12, 2014 at 16:23

4 Answers 4

6

You can concatenate a ternary operator like this:

$result = mysqli_query($db, "SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
    echo '<div class="groepitem"><input type="radio" ' . ($_SESSION['gebruikers_klasid'] == $record['klas_id'] ? "checked" : "") . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
Sign up to request clarification or add additional context in comments.

6 Comments

All in one go ;) ternaries are so nice to work with.
It does not seem to work. When I add the following code: ($_SESSION['gebruikers_klasid'] == $record['klas_id'] ? "checked": "") It wont add 'checked'. When I add: if($_SESSION['gebruikers_klasid'] == $record['klas_id']) { echo 'checked'; } it DOES add 'checked'.
@Diederiikk Did you start your session?
@Diederiikk Now it should work! Forgot to add spaces. Just copy it again and it should work
@Diederiikk What Rizier meant was, is session_start(); included inside all pages using sessions?
|
0

You can do this many different ways, but if you want cleaner html code:

<?php

$result = mysqli_query($db,"SELECT * FROM klassen");

while ($record = mysqli_fetch_array($result)) {

    if ($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
        $checked = ' checked';
    } else {
        $checked = '';
    }

    echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';

}

?>

OR...

<?php

$result = mysqli_query($db,"SELECT * FROM klassen");

while ($record = mysqli_fetch_array($result)) {

    $checked = ($_SESSION['gebruikers_klasid'] == $record['klas_id']) ? ' checked' : '';

    echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';

}

?>

Comments

0

Another way is to exit your echo then run the conditional statement then resume your echo... eg

<?php 
$result = mysqli_query($db,"SELECT * FROM klassen");   
while($record = mysqli_fetch_array($result)) { 
    echo '<div class="groepitem"><input type="radio" '; 
   if($_SESSION['gebruikers_klasid'] == $record['klas_id']) { echo 'checked'; }
    echo ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>'; 
} 
?>

Comments

0

this should work

<?php

$result = mysqli_query($db,"SELECT * FROM klassen");

while($record = mysqli_fetch_array($result)) {
    if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
        $var = 'checked';
    }
    echo '<div class="groepitem"><input type="radio" '.$var.' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}

?>

EDIT: now session check it's inside the loop

4 Comments

That won't work; the comparison needs to be in the while loop.
@Brett Yep, that's why I said "similar" to what I said. It needs to be inside the while loop.
well interpreted, well... almost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.