box
box

Reputation: 161

Looping HTML radio using PHP array

How would i change the following html code into a shorterend PHP array that i could reuse upon turning it into a function..

                <p class="ratingButtons">
                    <input type="radio" class="spacing" name="moRating1" value="1">1
                    <input type="radio" class="spacing" name="moRating1" value="2">2
                    <input type="radio" class="spacing" name="moRating1" value="3">3
                    <input type="radio" class="spacing" name="moRating1" value="4">4
                    <input type="radio" class="spacing" name="moRating1" value="5">5
                    <input type="radio" class="spacing" name="moRating1" value="6">6
                </p>

Im thinking i would need something like...

<?php
for ($i = 0; $i < count($array); ++$i) {
    print $array[$i];
}
?>

But im really confused on how i could then POST the result and repeat it for similar form sections with different names such as 'moRating2'

Something like this maybe for the function?

function generateRadioButtons($name, $values = 6) {
$o = '<p class="ratingButtons">' . "\n";
for ($v = 1; $v <= $values; $v++) {
    $selected = !empty($_POST[$name]) && $_POST[$name] == $v ? ' checked="checked"' : '';
    $o.= '<input type="radio" class="spacing" name="' . $name . '" value="' . $v . '"' . $selected . '>' . $v . "\n";
}
$o.= '</p>' . "\n";
return $o;
 }

Could anyone help me out? new to PHP and its all a bit overwhelming at the moment but overall im wanting to trim the HTML repetition by using a loop which i then turn into a function to reuse in similar html form questions... if that makes any sense

Thanks for the help!

Upvotes: 0

Views: 129

Answers (3)

Скач от
Скач от

Reputation: 212

This works for me:

function generateRadio ($name, $count = 6, $checked = '') {
    $output = '<p class="ratingButtons">'."\n";
    for($i=1; $i<=6; $i++) {
        $checkedStr = '';
        if($i == $checked) {$checkedStr = ' checked';}
        $output .= '<input type="radio" class="spacing" name="'.$name.'" value="'.$i.'"'.$checkedStr.'>'.$i.'<br />'."\n";
    }
    $output.='</p>';
    return $output;
}

echo generateRadio('moRating1', 6, 3);

And of course, this should be inside a form, something like:

<form method="post">
    <?php echo generateRadio('moRating1')?>
</form>

Upvotes: 0

sherwoor
sherwoor

Reputation: 185

The function you supplied works well. You just need to surround that in a form and add a submit button and you've got a working form. I was thinking something like this:

function generateRadioButtons($destination, $name, $values = 6) {
    $o = '<form name="my_form" action="' . $destination . '" method="post">
        <p class="ratingButtons">' . "\n";
        for ($v = 1; $v <= $values; $v++) {
            $selected = !empty($_POST[$name]) && $_POST[$name] == $v ? ' checked="checked"' : '';
            $o.= '<input type="radio" class="spacing" name="' . $name . '" value="' . $v . '"' . $selected . '>' . $v . "\n";
        }
    $o.= '</p><input type="submit" /></form>' . "\n";
    return $o;
}

Here you can pass in your destination, which is the page you will go to when you submit the form. e.g.:

echo generateRadioButtons("destination.php", "my_name", 4);

Will create your radio buttons and on pressing 'submit' you will be forwarded to destination.php, where you can pick up the result using

echo $_POST['my_name'];

Upvotes: 1

Dev
Dev

Reputation: 479

Try This for passing a loop

for ($i = 0; $i < count($array); $i++) 
{
?>
<input type="radio" class="spacing" name="moRating1" value="<?php echo $i; ?>"><?php echo $i; ?>
<?php
}

Upvotes: 0

Related Questions