-1

Hello guys I have this table :

ID| NUM_Slides |P1 | P2 | P3 | P4 | P5 | P6 | P7 | P8 | P9 | P10 | P11 | P12
3 | 2          |1  | 1  | 0  | 0  | 0  | 0  | 0  | 0  | 0  | 0   | 0   | 0

I want to select the values only from P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11 and P12 and insert them into a <option> tag

I got this so far but dont work

<?php
$query = "SELECT P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12 FROM slider_settings_img where slider_settings_img.ID = $ID";
$result1 =$connect->query($query);
$resultado = $result1->fetch_all();
?>

<select name ="selectedValue" id="selected" >
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value=""<?php echo $row1[1];?>"><?php echo $row1[1];?>"</option>
<?php endwhile;?>
</select>

because my query only return 1 array I want the Options to be display 1,1,0,0,0,0,0,0,0,0,0

can some 1 help me

2 Answers2

2

There are several problems.

  1. $resultado = $result1->fetch_all(); You can't fetch any more rows in your while loop if you've already fetched them all.

  2. while($row1 = mysqli_fetch_array($result1)):; Your query selects one row. You don't need to use a while loop at all.

  3. That while loop is set up to iteratively fetch rows from a query result, but you need to fetch one row and iterate over its columns.

  4. Couple of important typos.


<?php
$query = "SELECT P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12
          FROM slider_settings_img where slider_settings_img.ID = $ID";
$result1 = $connect->query($query);
$row = mysqli_fetch_assoc($result1);    // fetch one row
?>
<select name ="selectedValue" id="selected" >
<!-- iterate over each column of the row -->
<?php foreach ($row as $col): ?>
    <option value="<?php echo $col ?>"><?php echo $col ?></option>
<?php endforeach ?>
</select>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

How about this?

<select name ="selectedValue" id="selected" >
<?php while($row1 = mysqli_fetch_array($result1)) 
{
    for($i=2; $i<13; $i++) {
        echo '<option value="'.$row1[$i]'">'.$row1[1].'</option>';
    }
} 
?>
</select>
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24