1

I've got a php page which will show a form. In the form you need to select a driver. Drivers are selected from a mysql db, that part works just fine. However, I would like to see that the user that is logged in, automatically is selected as default driver for this form.

What am I missing? Any help is greatly apprciated!

mysqli_set_charset($connection, 'utf8');

$query  = "SELECT name, substring_index(substring_index(mrb_users.user_qualified,'|',-2),'|',1) as driver, id  ";
$query .= "from mrb_users having driver like 'B%' ORDER BY name ";
$sql = mysqli_query($connection, $query);

$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['id'] ."' if('".$row['id']."'==='".$_SESSION['id']."') 'selected'>" .$row['name'] . ' - ' .$row['chauffeur'] ."</option>" ;
}

$conn->close();
New contributor
Napster81 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

It looks like the PHP if statement is included in double quotes and therefore printed verbatim. If so, you should be able to see it using your browser's HTML inspector.

For clarity, I recommend (1) factoring out the if statement to a separate line and (2) using sprintf() instead of concatenating a large number of strings:

$sel = ($row['id'] === $_SESSION['id'])
  ? 'selected'
  : '';
sprintf('<option value="%s" %s>%s - %s</option>',
        $row['id'], $sel, $row['name'], $row['chauffeur']);

  • Thanks, this for sure pointed me in the right direction. After reading your answer and looking in my browser inspection I noticed I was comparing two datatypes. And then I noticed the sprintf was not printed. My solutions is as below: $sel = ($row['id'] == $_SESSION['id']) ? 'selected' : ''; echo sprintf('<option value="%s" %s>%s - %s</option>', $row['id'], $sel, $row['name'], $row['chauffeur']); } – Napster81 yesterday

Your Answer

Napster81 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.