代码之家  ›  专栏  ›  技术社区  ›  Max

通过从下拉列表中选择字段名显示mysql表的字段数据

  •  0
  • Max  · 技术社区  · 7 年前

    我有以下代码将字段名填充到下拉列表中。我希望能够在页面的表格中显示所选字段的内容。将只选择一个字段名称。因此,它将是页面上的单列表。我在网上找不到类似的东西。如果有人能帮我,我会很高兴的。谢谢

    <?php 
    
    $host = 'localhost'; 
    $port = '3306'; 
    $server = $host . ':' . $port; 
    $user = 'root'; 
    $password = ''; 
    $link = count($t_tmp = explode(':', $server)) > 1 ? mysqli_connect($t_tmp[0], $user, $password, '', $t_tmp[1]) : mysqli_connect($server, $user, $password); 
    if (!$link) { 
        die('Error: Could not connect: ' . mysqli_error($link)); 
    } 
    $database = 'mydb'; 
    mysqli_select_db($link, $database); 
    $query = 'select * from mytable'; 
    $result = mysqli_query($link, $query); 
    if (!$result) { 
        $message = 'ERROR:' . mysqli_error($link); 
        return $message; 
    } else { 
        $i = 0; 
         echo '<select name="mySelect" id="mySelect">'; 
        while ($i < mysqli_field_count($link)) { 
            $meta =  
            mysqli_fetch_field_direct($result, $i); 
            echo '<option>' . $meta->name . '</option>'; 
            $i = $i + 1; 
        } 
    echo '</select>';
    } 
    mysqli_close($link); 
    ?>
    

    看起来是这样的:

    enter image description here

    选择框中的选项都是字段名称,而不是字段值。我需要的是在单击/选择字段名称时显示字段值。

    3 回复  |  直到 7 年前
        1
  •  0
  •   Th3_N3w_D3v3l0p3r    7 年前

    尝试以下操作:

    $country = mysqli_query($con, "SELECT country FROM countrys");
    echo mysqli_num_rows($country);//counts the amount of rows in the db

    echo '<select name="mySelect" id="mySelect">';

    while(mysqli_fetch_array($country)){

    while ($i = mysqli_fetch_array($country)) {

    echo '<option value="'.$i['country'].'">' . $i['country']. '</option>'
    }
    echo '</select>';

    始终工作正常;)

        2
  •  0
  •   poorly-written-code    7 年前

    听起来您希望在select上有一个事件处理程序。

    $('#mySelect').on('change', function() {
      $.post('your-file.php', { 
        action: 'getOptionInfo',
        option: $('#mySelect option:selected').text()
      }, function(optionInfoHTML) {
        $('.table').html(optionInfoHTML);
      }
    });
    

    https://jsfiddle.net/mwe55nm9/1/

    此外,我建议构建您选择的html并将其作为一个整体进行响应,而不是对每个选项进行反复。根据我的喜好,我会使用for循环。而循环似乎总是卡在无限循环中。

    ... code to connect to database ...
    switch ($_POST['action']) {
      case 'buildSelect':
        $options = '';
        for ($i = 0; $i < mysqli_field_count($link); $i++) { 
          $meta =  mysqli_fetch_field_direct($result, $i); 
          $options += . '<option>' . $meta->name . '</option>';
        } 
        echo '<select name="mySelect" id="mySelect">' . $options . '</select>';
        break;
      case 'getOptionInfo':
        echo "code to get info for $_POST['option']"
        break;
      default: echo 'Invalid action.'
    }
    

    https://api.jquery.com/jquery.post/

        3
  •  0
  •   Max    7 年前

    谢谢你的帮助!我在这里找到了答案: https://forums.phpfreaks.com/topic/307230-php-dropdown-menu-to-fetch-columnar-data/page-2#entry1558339

    代码如下:

    <table class="tbresult">
    
    <?php 
    
    include ("confige.php");
    
    $query = 'select * from employees'; 
    $result = mysqli_query($link, $query); 
    if (!$result) { 
        $message = 'ERROR:' . mysqli_error($link); 
        return $message; 
    } else { 
        $i = 0; 
        echo '<form name="selectSomething" action="" method="GET">';
         echo '<select name="mySelect" id="mySelect"  onchange="this.form.submit()">'; 
        while ($i < mysqli_field_count($link)) { 
            $meta =  
            mysqli_fetch_field_direct($result, $i); 
            echo '<option>' . $meta->name . '</option>'; 
            $i = $i + 1; 
        } 
    echo '</select>';
    echo '</form>';
    } 
    
    if(isset($_GET['mySelect'])) {
    $myselect = $_GET['mySelect'];  // needs to be after the above check
    
    $sql = "SELECT `$myselect` as mySelect FROM employees";  // add column alias
    $result = mysqli_query($link, $sql);   
    
      if ($result->num_rows > 0) {
    
       // output data of each row
       while($row = $result->fetch_assoc()) 
       {
            echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
    
       }
    
          echo "</table>";
    
    
    }
    
    } else { echo "0 results"; }
    
    
    mysqli_close($link);
    
    ?>