【问题标题】:Add COUNT in Javascript alert box在 Javascript 警告框中添加 COUNT
【发布时间】:2020-05-31 02:17:15
【问题描述】:

此页面是用户选择学期和课程的地方,然后警报框将提示所选课程,如下图所示。

Image

Javascript

<script type="text/javascript">

$(document).ready(function(){

$("#prg").change(function(){
 var selectedcount = $('#prg option:selected').data('count'); 

alert ("The next intake for " +selectedcount + " will have    "    + "students"); 
}); </script>

身体

<div class="modal-body">
        <div class="form-group">
              <label for="title">Select Current Semester:</label>
              <select name="semester" class="form-control">
                  <option value="">--- Select Current Semester ---</option>

                  <?php
                    require('../setting/config.php');
                      $query = "SELECT DISTINCT semester FROM marketing_data ORDER BY semester DESC"; 
                      $do = mysqli_query($conn, $query);
                      while($row = mysqli_fetch_array($do)){
                          echo '<option value="'.$row['student_matric'].'">'.$row['semester'].'</option>';
                      }
                  ?>
              </select>
          </div>
          <div class="form-group">
              <label for="title">Select Programme:</label>
              <select id="prg" name="prg" class="form-control">
                <option value="">--- Select Programme ---</option>

                  <?php
                      $query2 = "SELECT student_prg, COUNT(student_prg) as count FROM marketing_data GROUP BY student_prg ORDER BY student_prg DESC"; 
                      $do = mysqli_query($conn, $query2);
                      while($row = mysqli_fetch_array($do)){
                          echo '<option value="'.$row['student_matric'].' data-count="'.$row['count'].'">'.$row['student_prg'].'</option>';
                      }
                ?>
              </select>

我希望提醒框也添加学生人数。例如:*多媒体工业(荣誉)学士的下届入学将有 36 个学生*

您能否就在何处以及如何添加计数查询提供帮助,以便它会显示在我提到的警报框中。

数据库

mysql> describe marketing_data;
+---------------+------------------+------+-----+---------+--------------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+--------------------+
| student_matric| varchar(10) unsigned | NO   | PRI | NULL    | auto_increment |
| student_prg   | text unsigned        | YES  |     | NULL    |                |
| semester      | varchar(10)          | YES  |     | NULL    |                |
| intake_year   | int(10)              | YES  |     | NULL    |                |
| student_city  | text                 | YES  |     | NULL    |                |
| city_lat      | varchar(20)          | YES  |     | NULL    |                |
| city_long     | varchar(20)          | YES  |     | NULL    |                |
| student_state | text                 | YES  |     | NULL    |                |
| state_code    | varchar(100)         | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+--------------------+

【问题讨论】:

    标签: javascript php html mysql


    【解决方案1】:

    您可以使用如下查询返回每条记录的计数:

    SELECT student_prg, COUNT(student_prg) as count FROM marketing_data GROUP BY student_prg ORDER BY student_prg DESC
    

    您的结果现在将计算每个 student_prg 值的数量。

    那么我可能会将计数添加为您正在编写的选项的数据属性:

    echo '<option value="'.$row['student_prg'].' data-count="'.$row['count'].'">'.$row['student_prg'].'</option>';
    

    然后您可以从新选择的选项中选择值。

    【讨论】:

    • 我已经按照上面的方法编辑了代码,@Alec Sanger .. 你的意思是这样吗?但是提示框现在根本没有提示...请协助检查...
    • var selectedcount = $('#prg option:selected').data('count');,然后只需将 selectedcount 添加到您的警报中。
    • 听起来您需要确保您的 javascript 代码实际加载正确。在ready.change 函数中添加一些console.log('test'); 语句,并确保您在控制台中看到这些语句。
    • 其实……你是说现在根本没有弹出警告框?
    • 是的@Alec Sanger ...它根本没有弹出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多