winter
winter

Reputation: 59

getting the value inside while loop

Is there any way to get the name attribute while it is in the while loop? I want to call the name attribute of the radio button to insert to database

if ($result = $conn - > query($query)) {
    while ($row = $result - > fetch_row()) {
        //$app_rows.append($row[1])
        $app_rows = array($row[1]);
        ?>
        < tr >
            < td id = "app_name" > <? php foreach($app_rows as $values) {
                echo $values; ?> < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P0 "; ?>"
                value = "P0" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P1 "; ?>"
                value = "P1" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P2 "; ?>"
                value = "P2" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P3 "; ?>"
                value = "P3" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P4 "; ?>"
                value = "P4" > < /td> <? php
            } ?>
            < /tr> <? php
    }
    $result - > close();
}

Upvotes: 1

Views: 85

Answers (2)

Ascaliko
Ascaliko

Reputation: 76

you can use array in attribute name, eg in your code

<td> <input type="radio" name="radioVal[]" id="<?php echo $values. "-P0"; ?>" value="P0"></td>
<td> <input type="radio" name="radioVal[]" id="<?php echo $values. "-P1"; ?>" value="P1"></td>
<td> <input type="radio" name="radioVal[]" id="<?php echo $values. "-P2"; ?>" value="P2"></td>

then in backend

$radioVal = $_POST['radioVal'];
if(count($radioVal)!=0){
$radioIsVal = '';
for ($i=0; $i < count($radioVal); $i++) { 
 $radioIsVal .= $radioVal[$i].',';
}
$radioIsVal = rtrim($radioIsVal ,',');
// $radioIsVal the value is P0, P1, P2
}else{
echo 'no radio value';
}

may it can help you

Upvotes: 1

J.WCT
J.WCT

Reputation: 92

if($result = $conn->query($query))
{
    while($row = $result->fetch_row())
    {
        $app_rows = array($row[1]);
        echo"<tr>";
        foreach($app_rows as $values)
        {
            echo"<td id='app_name'>".$values."</td>";
            for($c=0;$c<=4;$c++)
            {
                echo"<td> <input type='radio' name='".$values."' id='".$values.'-'.$c."' value='P".$c."''></td>";
            }
        }
        echo"</tr>";
    }
}

Upvotes: 0

Related Questions