duanruoyu6675 2016-01-24 08:13
浏览 70
已采纳

SELECT Input仅从数据库返回有限值

This might sound odd but hopefully someone could figure out and help me with this.

I am retrieving a number of values from the MySql database. I have at least 300 rows in my database which is being exported to my JavaScript file. These values are presented as <option> in my <select> input. When I run my file, the <select> input did not return any <option> value.

I wonder what was going on and realize that there are a lot of values returned. So I put a limit on the SQL data and I concluded that the <select> input only returns a maximum of 13 values if there is no WHEREfunction in the php code and only 10 values if there is a WHERE function.

I did tried putting the data manually without retrieving data from database and it only accepts 25 values out of at least 300 values.

My questions are:

  • Are there really limits on the data displayed in the <select> input? If there is, is there any way to bypass it? If none, what could have went wrong?

These are the codes in case you need it. I just simplified and remove few data to visualize what I mean.

HTML

<form action="" method="post" id="demo-form2" data-parsley-validate class="form-horizontal form-label-left">
    <div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Search Topic
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
    <select id="userDropdown" name="tid" class="select2_single form-control col-md-7 col-xs-12"></select>

    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Description <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="description" name="description"  class="form-control col-md-7 col-xs-12" />
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Assigned to <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="assign" name="assign" class="form-control col-md-7 col-xs-12" />
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="last-name">Comments: <span class="required">*</span>
    </label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text" id="comments" name="comments"   class="form-control col-md-7 col-xs-12">
    </div>
</div>

<div class="form-group">
    <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Words</label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input id="words" class="form-control col-md-7 col-xs-12" type="text" name="words">
    </div>
</div>


  </div>
<div class="ln_solid"></div>
<div class="form-group">
    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
        <input type="submit" name="submit" class="btn btn-success" value="Update" />
    </div>
</div>

JAVASCRIPT

 <script>



    //sql data...
    var data = [   <?php
        $req = mysql_query('select * from tasks');
            while($dnn = mysql_fetch_array($req))
              {
              ?>  {username: '<?php echo $dnn['username'] ?>', tid: '<?php echo $dnn['tid'] ?>', description: '<?php echo $dnn['description'] ?>'},
    //this would return at least 300 results

<?php } ?>

    ];

    var dropdown = $('#userDropdown');
    dropdown.append('<option value="" >Search for Topic ID</option>');
    for(var i = 0; i < data.length; i++){
        var item = data[i];
        dropdown.append('<option value="' + item.tid + '" >' + item.tid + '-' + item.description + '</option>');
    }

    $('#userDropdown').change(function(){
        var user = this.value;
        var dataItem = $.grep(data, function(e){ return e.tid == user; });

        if(dataItem.length > 0){
            $('#description').val(dataItem[0].description);
            $('#assign').val(dataItem[0].assign);
            }
    });
    </script>

There is no problem with the code. I just included it for reference.

EDIT: Remove LIMIT on the php script to avoid confusion.

  • 写回答

3条回答 默认 最新

  • douguachan2879 2016-01-24 09:28
    关注

    There is no limit to the <select> element. a more elegant way to do this job is to use the here-doc syntax in php like this :

    var data = [
        <?php
            $req = mysql_query('select * from tasks');
            while($dnn = mysql_fetch_array($req, MYSQL_ASSOC)){
                echo <<<DATA
                {
                    username : '{$dnn['username']}',
                    tid : {$dnn['tid']},
                    description : '{$dnn['description']}'
                }           
    DATA;
            }
        ?>
    ];
    

    also keep in mind that :

    this javascript-php snippet should be embedded in the view parsed by php engine

    you should not put it in an external asset that would load after parsing the view by php & loaded via browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么