php - Using for loop to get the values for both session arrays and existing table values -


i have write-up form has 10 of same inputs, stored in array loop through , manipulate data accordingly. sample of form input:

    <form id="sanfiber" action="insert.php" method="post">     <table class="frame">     <tr>         <td><input name="host[]" class="field" type="text" size="15"             <?php if(isset($_session['hostname'])){ echo "value='            ".$_session['hostname']."'";}?> /></td>     </tr>     <tr>         <td><input name="host[]" class="field" type="text" size="15"          <?php if(isset($_session['hostname'])){ echo"value='".$_session['hostname']."'";}?> /></td>    </tr>        ...    </form> 

i have been able create loop insert new values in existing table on database, however, i've had trouble trying select query same way because values aren't pre-existing. loop tried returned first value. conflict have loop both session arrays , rows found on database. loop portion $row values work fine. session loop problem lies. select query , loop follows:

<?php  include ("connect.php"); session_start();  $query = "select * cable_request_san_fiber_detail cable_request_id = '".$id."'";   $result = mysqli_query($conn, $query);  while ($row = mysqli_fetch_assoc($result)) {     $label[]=$row['cable']; }        ($i=0, $n=0; $i<count($label), $n<count($_session['hostname']); $i++, $n++){     $_session['hostname'][$n]=$label[$i]; }     header('location:table.php'); $conn->close(); ?> 

in loop, combined 2 different loops: 1 counting , looping through of session variables , 1 looping through rows in table on database. there way correctly both loops or should perform them separately?

is there point having $label?

instead of:

while ($row = mysqli_fetch_assoc($result)) {     $label[]=$row['cable']; }        ($i=0, $n=0; $i<count($label), $n<count($_session['hostname']); $i++, $n++){     $_session['hostname'][$n]=$label[$i]; } 

why not 1 loop instead of two:

// clear old hostnames don't end duplicates $_session['hostname'] = array(); while ($row = mysqli_fetch_assoc($result)) {     $_session['hostname'][] = $row['cable']; } 

when outputting in html, want loop through array:

<?php foreach($_session['hostname'] $hostname) { ?>     <tr>         <td>             <input name="host[]" class="field" type="text" size="15" value="<?= $hostname ?>" />         </td>     </tr> <?php } ?>