1

I have information from the database that i list using foreach loop:

<table class="price_table">
   <caption>Prices</caption>
   <?php if($price= getCarPricePeriod($car->ID)):?>
        <?php foreach ($prices as $price): ?>
             <tr>
                  <td><input type="radio" name="price" value="<?= $price['Price'];?>"> </td>
                 <td><?= $price['Price'];?>$</td>
             </tr>
        <?php endforeach; ?>
   <?php endif; ?>
</table>

So with this loop i get prices with radio button. How to make first item from loop be checked. Just first need to be checked

If i add checked in loop all items will be checked or randomly.

0

2 Answers 2

1

You will have to use a variable flagging your element is first:

<table class="price_table">
    <caption>Prices</caption>
    <?php if($price= getCarPricePeriod($car->ID)):
        $first = true;
        foreach ($prices as $price): ?>
             <tr>
                  <td><input type="radio" name="price" value="<?= $price['Price'];?> <?= $first ? 'checked' : '' ?>"> </td>
                 <td><?= $price['Price'];?>$</td>
             </tr>
        <?php $first = false;
        endforeach; ?>
   <?php endif; ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

Quick way to do it is to have a variable to be the identifier

<table class="price_table">
   <caption>Prices</caption>
   <?php  if($price= getCarPricePeriod($car->ID)):?>
        <?php $checked=true; foreach ($prices as $price): ?>
             <tr>
                 <td><input type="radio" name="price" value="<?= $price['Price'];?>"<?=$checked ? ' checked' : '';?>> </td>
                 <td><?= $price['Price'];?>$</td>
             </tr>
        <? $checked = false; ?>
        <?php endforeach; ?>
   <?php endif; ?>
</table>

Comments

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.