开发者

Using array of numbers to toggle radio button visibility. PHP

开发者 https://www.devze.com 2023-03-28 06:45 出处:网络
I have an array of numbers that can be 1-24. ($timelist) I want to use this arra开发者_如何学Pythony to make a select time form. How would I use this array to toggle the visibility of a radio button?

I have an array of numbers that can be 1-24. ($timelist) I want to use this arra开发者_如何学Pythony to make a select time form. How would I use this array to toggle the visibility of a radio button?

For example: I have array(1,2,4) and radio buttons 1AM, 2AM, 3AM, and 4AM. I want radio button 3AM to not show based on the array in my php script.

Any ideas on how to make this work?

What I got so far is a form where the user selects a date and hits enter.

It then sends them to a php file named validate.php via get method.

The script will do it's job and place available times into an array.

I want to use that array (1-24) to display the available times as radio buttons (12AM to 12PM) where the user can select one based on a time available and continue the application process.


$timelist = array( 1, 2, 4 );
foreach($timelist as $time):
    echo "<input type='radio' name='time' value='$time' /> $radio AM<br />";
endforeach;

Something like that, you mean?

Edit: to have nice formatting, you could do something along the lines of

$time%12 . ( $time >= 12 ? 'PM' : 'AM' )


From your question, I understand that you initially do send the radio input to the client in the response HTML, and you want to hide it afterwards? This doesn't make much sense - simply prepare the response HTML according to the elements in the array, instead of rendering everything and than hiding it. Do something like:

<?php foreach ($timelist as $time): ?>
    <input type="radio" name="time" value="<?php echo $time?>" /> <?php echo $time?>
<?php endforeach; ?>


for military time inclusion try this

<?php $i = 1; ?>
<?php while ($i <= 24): ?>
    <input type="radio" name="time" value="<?php echo $i; ?>" /> 
    <?php echo (12 < $i ? $i .' AM': ($i -12).' PM'); >
    <?php $i++; ?>
<?php endwhile; ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消