Populate more than one drop down using the same mysql query

前端 未结 3 667
礼貌的吻别
礼貌的吻别 2021-01-25 07:14

I have form with a number of drop boxes which have the numbers 1-5 in them. I can use this code to populate a drop down but was wondering if I can somehow only make the call to

相关标签:
3条回答
  • 2021-01-25 07:20

    Saving as a string would save you the processing of having to loop through the same data generating the same output multiple times. If this is what you want, you could do the following.

    Replace:

    echo '<select  class="assess" name="precontcons"   style="width:4em">' ;
    while($row = $result->fetch_assoc()){echo '<option value='.   $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
    ?> </select>
    

    with:

    $drop = '<select  class="assess" name="precontcons"   style="width:4em">' ;
    while($row = $result->fetch_assoc()){
      drop .= '<option value='.   $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
    }
    $drop .= '</select>';
    

    Then you can echo $drop several times if you want.


    If for whatever reason you want different select attributes, you could just save the options list and print the select around that, like this:

    $dropOptions = "";
    while($row = $result->fetch_assoc()){
      $dropOptions .= '<option value='.   $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
    }
    

    Then just echo '<select class="foo" name="bar">'.$dropOptions.'</select'>

    0 讨论(0)
  • 2021-01-25 07:20

    You could store the data into an array, and then print the data by enumerating the array.

    $risks = array();
    
    // Load values into array.
    while ($row = $result->fetch_assoc()) {
        array_push($row['riskNumDrop']);
    }
    
    
    // Drop down #1
    echo '<select>';
    foreach ($risks as $risk) {
        echo '<option value='. $risk .'>'. $risk .'</option>';
    }
    echo '</select>';
    
    // Drop down #2
    echo '<select>';
    foreach ($risks as $risk) {
        echo '<option value='. $risk .'>'. $risk .'</option>';
    }
    echo '</select>';
    
    0 讨论(0)
  • 2021-01-25 07:30

    Simply get the results into an array using the fetch_all method. It returns all rows from the result set into an associative or numeric array. The you can do whatever you want with such array:

    $result = $conn->query('SELECT * FROM riskNumDrop');
    if (!$result) {
       echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
       exit; // fetch_* functions cannot be called on error (sice $result is false)
    }
    
    // get all rows from the result
    $rows = $result->fetch_all(MYSQLI_ASSOC);
    
    // output first dropdown...
    echo '<select name="dropdown_1">';
    foreach ($rows as $row) {
        // options for the first dropdown (same as you did before)
    }
    echo '</select>';
    
    // output second dropdown...
    echo '<select name="dropdown_2">';
    foreach ($rows as $row) {
        // options for the second dropdown
    }
    echo '</select>';
    
    0 讨论(0)
提交回复
热议问题