H_MZ 2015-03-30 09:23 采纳率: 0%
浏览 21

动态查询生成器

I am creating a custom MySQL database query UI. Inside this UI I have a Query Builder interface that I would like to dynamically append query properties based on the user selections in order to create a dynamic query. Please see the below picture for a visual description

Query Builder UI

From the picture above I would like to append CHARACTER_SET after the FROM and append as asterisk when ALL is selected from the table and so forth with the key being the positions where I place the generated variables.

How can I achieve this with JQuery?

My JavaScript

Selecting a Table

$(document).on("change", ".tbl_list", function () {
    var tbls = new Array();
    $("input:checkbox[name='tbl[]']:checked").each(function () {
        tbls.push($(this).val());
    });
    var tbl = tbls.join('|');
    var db = window.sessionStorage.getItem("db");
    $.ajax({
        type: "POST",
        url: "ajax2.php",
        data: {
            tbl: tbl,
            db: db
        },
        success: function (html) {
            console.log(html);
            $("#tblField").html(html).show();
        }
    });
});

Selecting All option

  $(document).on("click", ".tblall", function () {
        if (this.checked) {
            // Iterate each checkbox
            $('.tblst').each(function () {
                this.checked = true;
            });
        } else {
            $('.tblst').each(function () {
                this.checked = false;
            });
        }
    });

EDIT

As requested HTML for my DIVs

Table Selector

while ( $row = mysqli_fetch_array ( $tbl_list ) ) {
        ?>
        <input type="checkbox" name="tbl[]" class="tbl_list"
    value="<?php echo   $row [0];   ?>" />
 <?php echo $row [0]; ?> 
<br>

Query Builder

<div id="qryDisplay">
            <fieldset>
                <legend> Query Builder</legend>
                <div id="qryView">
                    <p>SELECT   FROM</p>
                </div>
            </fieldset>
        </div>

What I have tried so far

Using .append I can add data to the end of the paragraph so this would be ideal for my Table name. However its a function and i'm not sure how I would implement the code below into my select table function.

$("#qryView > p").append("  " tblName);
  • 写回答

1条回答 默认 最新

  • weixin_33698043 2015-03-30 10:41
    关注

    Anyway, not considering the logic behind the selection of multiple tables my approach would be to store selections in hidden input fields and at the end construct from the hidden fields the query.

    <input type="hidden" value="" name="hiddenTables" id="hiddenTables" />
    

    fill field according to selections in your function from above:

    $("input:checkbox[name='tbl[]']:checked").each(function () {
        tbls.push($(this).val());
        if($('#hiddenTables').val() == ""){ 
           $('#hiddenTables').val($(this).val()); 
        }else{
           $('#hiddenTables').val($('#hiddenTables').val()+','+$(this).val());
        }
    });
    

    At the end create your query:

    // hidden field for field selection, same as above.
    var fieldselection = '*';
    if($('#hiddenFieldselection').val() != ""){
        fieldselection = $('#hiddenFieldselection').val();
    }
    
    $("#qryView > p").html("SELECT " + fieldselection + " FROM " + $('#hiddenTables').val()); 
    

    This needs to be adjusted the way you need it of course and I haven't tested any of this... So that's up to you :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助