php - 显示从下拉列表中选择相应选项后某个位置出现在相应表格中的次数

标签 php html mysql

我想请求帮助显示位置值在表中出现的次数,该表对应于用户从下拉列表中单击的选项。

这是我目前所做的: `

  $con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

  //query buildings table for the dropdown
  $bquery = mysqli_query($con, "SELECT building_ID, building_name FROM buildings");

  $selectedbldg = null;

  // if the form was submitted
  if (!empty($_POST['bldg'])) 
  {
    // store selected building_ID
    $selectedbldg = $_POST['bldg'];

    //count instances of location_name in delivery_transaction table; 
    $count = mysqli_query($con, "
    SELECT location_ID, COUNT(location_ID) 
    FROM delivery_transaction 
    GROUP BY (location_ID)
    ");
  }  
?>

<!--Building dropdown contents-->
<form name="bldg_form" method="post" action="">
<select name="bldg">
  <option value="">Choose Building</option>;
    <?php while ($row = mysqli_fetch_assoc($bquery)) : ?>
      <option value="<?= $row['building_ID'] ?>" <?= $row['building_name'] == $selectedbldg ? 'selected' : '' ?>><?= $row['building_name'] ?></option>
      <?php endwhile ?>
</select>
<input type="submit" name="view" />
</form>

<section class="row text-center placeholders">
  <!--For the table to display everytime user selects an option from dropdown-->
  <div class="table-responsive">
    <table class="table table-striped">
      <thead>
        <tr>
         <th>Location</th>
         <th>Number of Visits</th>
        </tr>
      </thead>
      <tbody>
        <!--PHP alternative syntax for control structures: if; open brace-a colon (:) and the closing brace-endif-->

        <!--the isset function to check if variable has value assigned or not ; mysqli_num_rows returns the number of rows in the result set-->
        <?php if (isset($count) && mysqli_num_rows($count)) : ?> 
          <?php while($row = mysqli_fetch_assoc($count)) : ?>
            <tr>
              <td><?= $row['location_ID'] ?></td>
              <td><?= $row['COUNT(location_ID)'] ?></td>
            </tr>
          <?php endwhile ?>
        <?php else : ?>
          <tr>
            <td>No results to display</td>
          </tr>
        <?php endif ?>
      </tbody>
    </table>
  </div>
</section>`

我认为这是错误的,因为它显示了所有位置 ID:
my current table for counting location instances

请帮忙:(

最佳答案

如果我理解正确,我相信你的问题正是 same as last one .您只是缺少一个 WHERE 子句。

看看这是否有效:

$con = new mysqli("localhost" ,"root" ,"" ,"user_databases");

//query buildings table for the dropdown
$bquery = mysqli_query($con, "SELECT building_ID, building_name FROM buildings");

$selectedbldg = null;

// if the form was submitted
if (!empty($_POST['bldg']))  {
    // store selected building_ID
    $selectedbldg = $_POST['bldg'];
    // the subquery is used to count how many times each location appears
    // for a particular building
    $count = mysqli_query($con, "
        SELECT lo.location_ID, lo.location_name, dt.num_visits
        FROM location lo
        JOIN (
            SELECT location_ID, COUNT(location_ID) AS num_visits
            FROM delivery_transaction 
            WHERE building_ID = {$selectedbldg}
            GROUP BY location_ID
        ) AS dt ON lo.location_ID = dt.location_ID
    ");

    // like before, better to use prepared statement
}
?>

<!-- ... -->

<section class="row text-center placeholders">
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Location</th>
                    <th>Number of Visits</th>
                </tr>
            </thead>
            <tbody>
            <!-- PHP alternative syntax for control structures: easier to read (imo) -->
            <!-- isset function is to ensure variable $count exist as it only gets declared in the IF condition (you would get an error otherwise) --> 
            <!-- mysqli_num_rows is to check if there are any results to loop over -->
            <?php if (isset($count) && mysqli_num_rows($count)) : ?> 
                <?php while($row = mysqli_fetch_assoc($count)) : ?>
                <tr>
                    <td><?= $row['location_ID'] ?></td>
                    <td><?= $row['num_visits'] ?></td>
                </tr>
                <?php endwhile ?>
            <?php else : ?>
                <tr>
                    <td>No results to display</td>
                </tr>
            <?php endif ?>
            </tbody>
        </table>
    </div>
</section>

更多值得阅读的好东西:

关于php - 显示从下拉列表中选择相应选项后某个位置出现在相应表格中的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446624/

相关文章:

php - Mobiweb 短信提供商 自动 dlr 转发

html - 粘性菜单间歇性闪烁

php - 使数据库记录更改可跟踪的最佳方法?

mysql - MySQL 有某种 "strict performance mode"吗?

MySQL - 如何显示每个线程的最新主题

html - 背景颜色未填充行的扩展表列

php - 使用 JOIN 时将 MySQL 值转换为 PHP 变量

php - Laravel 价格过滤器

php - 集成 AJAX 和 PHP

php - CodeIgniter: Hook (pre_controller)加载助手