Checked radio button with sql query listing

Asked

Viewed 77 times

0

Good afternoon, I have an appointment that calls me back radio buttons with some BD results for further handling, however when I put the checked it is marked in the last table result, how can I make it marked in the first result?? follows code

<?php
    $list_platform_don = $conn->prepare('SELECT * FROM type_donations WHERE platform_id ='.$_SESSION['platform']);
    $list_platform_don->execute();
    foreach($list_platform_don as $donate):
        echo '<input type="radio" name="donate_opt" class="donate_opt" value="'.$donate['coupons'].'" checked><p>'.$donate['name'].' </p><img src="imagens/'.$donate['img'].'">';
    endforeach;
?>

1 answer

0


If "lista_platform_don" is an array, a way to mark the first element would be like this:

foreach($list_platform_don as $index => $donate):
if ($index == 0) {
echo '<input type="radio" name="donate_opt" class="donate_opt" value="'.$donate['coupons'].'" checked><p>'.$donate['name'].' </p><img src="imagens/'.$donate['img'].'">';
} else {
echo '<input type="radio" name="donate_opt" class="donate_opt" value="'.$donate['coupons'].'" ><p>'.$donate['name'].' </p><img src="imagens/'.$donate['img'].'">';
}
endforeach;
  • understood, in case the index is used to manage the arrays separately, did not know very well how works this question of array, I will test here

  • worked really well, vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.