【问题标题】:JQUERY - PHP - MySQL - Result array addition, more info addedJQUERY - PHP - MySQL - 结果数组添加,更多信息添加
【发布时间】:2014-03-21 13:08:36
【问题描述】:

我不小心删除了我的最后一个帖子,所以又来了:

我有一个小仓库,里面有几个零件,我正在编写一个 jquery、php、mysql 零件查找工具,我有一些零件作为套件提供,其中包含额外的零件编号。

当前查询需要在 mysql 表中有一列显示类似 HasChild 的内容,如果显示为“是”,那么它将显示结果数组中的其他数据。如何将它添加到我当前的结果循环中?

这是我目前正在使用的代码:

<?php

$dbhost = "dbhost";
$dbname = "dbname";
$dbuser = "dbuser";
$dbpass = "dbpass";


global $part_lookup_tool_db;

$part_lookup_tool_db = new mysqli();
$part_lookup_tool_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$part_lookup_tool_db->set_charset("utf8");


if ($part_lookup_tool_db->connect_errno) {
    printf("Connect failed: %s\n", $part_lookup_tool_db->connect_error);
    exit();
}

$html = '';
$html .= '<li class="result">';
$html .= '<h1 class="btn2"><center> oemprojString</center></h1>';
$html .= '<h2><b>Part Number: functionString</b></h2>';
$html .= '<h4><b>Stock Item Description:</b> nameString</h4>';
$html .= '<h4><b>Stock Type:</b> stocktypeString</h4>';
$html .= '<h4><b>Vendor:</b> vendorString</h4>';
$html .= '</br>';
$html .= '<a target="_blank" href="imageString">';
$html .= '<center><img class="imn" src="imageString" width="50%" height="50%"></center></a>';
$html .= '</br>';
$html .= '<h4><b>Notes:</b> notesString</h4>';
$html .= '</li>';


$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $part_lookup_tool_db->real_escape_string($search_string);


if (strlen($search_string) >= 1 && $search_string !== ' ') {
    $query = 'SELECT * FROM search WHERE function LIKE "%' . $search_string . '%" OR name LIKE "%' . $search_string . '%"';


    $result = $part_lookup_tool_db->query($query);
    while ($results = $result->fetch_array()) {
        $result_array[] = $results;
    }

    if (isset($result_array)) {
        foreach ($result_array as $result) {


            $display_function = preg_replace("/" . $search_string . "/i", "<b class='highlight'>" . $search_string . "</b>", $result['function']);
            $display_name = preg_replace("/" . $search_string . "/i", "<b class='highlight'>" . $search_string . "</b>", $result['name']);
            $display_image = preg_replace("/" . $search_string . "/i", "" . $search_string . "", $result['image']);
            $display_stocktype = preg_replace("/" . $search_string . "/i", "<b class='highlight'>" . $search_string . "</b>", $result['stocktype']);
            $display_vendor = preg_replace("/" . $search_string . "/i", "<b class='highlight'>" . $search_string . "</b>", $result['vendor']);
            $display_notes = preg_replace("/" . $search_string . "/i", "<b class='highlight'>" . $search_string . "</b>", $result['notes']);
            $display_oemproj = preg_replace("/" . $search_string . "/i", "<b class='highlight'>" . $search_string . "</b>", $result['oemproj']);


            $output = str_replace('nameString', $display_name, $html);
            $output = str_replace('functionString', $display_function, $output);
            $output = str_replace('imageString', $display_image, $output);
            $output = str_replace('stocktypeString', $display_stocktype, $output);
            $output = str_replace('vendorString', $display_vendor, $output);
            $output = str_replace('notesString', $display_notes, $output);
            $output = str_replace('oemprojString', $display_oemproj, $output);

            echo($output);
        }
    } else {

        $output = str_replace('nameString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('functionString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('imageString', 'http://i.imgur.com/default.png', $output);
        $output = str_replace('stocktypeString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('vendorString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('notesString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('oemprojString', '<font color="red">No Data Found.</font>', $output);

        echo($output);
    }
}
?>

我做了一些研究,我认为这是我需要添加的内容,但我就是不知道在哪里:

if (isset($result_array)) {
    foreach ($result_array as $result) {

        if ($result['HasChildren'] == "yes") {

            echo $result['ChildParts'];
        } elseif ($result['HasChildren'] == "no") {

            echo $result['nochildren'];
        }
    }
}

再次感谢任何帮助我的人,我非常感谢我能得到的所有帮助。

这是 SQLfiddle.com 的link

我希望它使用 PHP 或任何你们认为最适合我提供的代码的方式。 - 再次感谢。

【问题讨论】:

  • 您能否举例说明您希望最终输出的外观如何?
  • @PatrickQ 基本上我想显示任何与最终用户搜索字符串匹配的所有结果,但如果它确实有子部分也显示该搜索结果的附加信息,请告诉我你是否不明白我的意思。我会尝试设置一个可以在线玩的示例。
  • "...显示该搜索结果的附加信息..." 什么信息?怎么展示?乍一看,我会说你想要重组你的表,这样你就有一个kits 表,其中包含kitPartIDchildPartIDquantity 列。或者类似的东西。
  • @PatrickQ 没错,我已经重组了我的表格,并且正在为你制作一个模型,我还会在 Photoshop 中制作一些简单的东西让你看到结果。跨度>
  • @PatrickQ -- 和家人度过了一个长周末,明天 EOD 之前我会为你准备好一切。

标签: php jquery mysql sql


【解决方案1】:

如果我是你,我会使用 PDO。 PDO 与 mysqli 之前的 mysql 相同。简而言之:它“只是”另一个用于 PHP 的 MySQL 适配器(PDO 代表 PHP 数据对象)。

如果使用 PDO,这很可能是您想要做的:

第 1 步 -- 打开您的连接

要在 PDO 中打开连接,您只需:

$dbh = new PDO('mysql:host=xxx.xxx.xxx.xxx;port=xxxx;dbname=xxx', 'username', 'password');

第 2 步 -- 创建查询

要从数据库中收集数据,您必须知道要查找的内容。这就是查询的目的。假设我们要从“产品”表中选择所有列,我们将执行以下操作:

$query = "SELECT * FROM products";

当然,当使用 JOINS 和所有其他简洁的 MySQL 功能时,这些查询可能会变得更加困难,但现在让我们保持简单。

第 3 步 -- 准备查询

在执行您的查询之前,请做好准备:

$stmt = $dbh->prepare($query);

这样您的查询就可以启动了,让我们执行您的查询:

$stmt->execute();

如果一切顺利,我们可以继续第4步。

第 4 步 - 检索您的数据

也许最重要的一步(至少,如果您使用数据库并想要检索数据)是检索数据(Duh...)。 PDO(和 mysqli)有一种简单的循环结果集的方法:

while($rs = $stmt->fetch(PDO::FETCH_ASSOC)) {
    //Do something with your data
}

这可能有点快,但它的作用是进行一个while循环,直到$stmt->fetch(PDO::FETCH_ASSOC) 中的所有元素都被循环完。我正在使用 PDO::FETCH_ASSOC,因为它使我们能够使用

$column1Value = $rs['column1'];

如果该列当然存在。

关于它应该如何工作的闲聊已经够多了,让我们来看看你的问题。

这应该是你的问题的代码(虽然它没有经过测试):

$dbh = new PDO('mysql:host=xxxxx;port=xxxx;dbname=xxxx', 'username', 'password');
$query = "SELECT * FROM yourTable";
$stmt = $dbh->prepare($query);
$stmt->execute();

while ($rs = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $hasChildren = $rs['hasChildren'];
    if ($hasChildren === 'yes') {
        //Continue getting data from children
    } else {
        //Stop the function?
    }
}

我可能遗漏了一些要点,但我认为这可能会让你开始。

【讨论】:

  • 这根本不能回答用户的问题。虽然使用 PDO 的观点是有效的,但它属于注释。
【解决方案2】:

我能够使用下面的代码完成这项工作,谢谢大家的帮助!

if (strlen($search_string) >= 3 && $search_string !== ' ') {
    $query = 'SELECT * FROM search WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%" OR stocktype LIKE "%'.$search_string.'%" OR vendor LIKE "%'.$search_string.'%" OR notes LIKE "%'.$search_string.'%" OR oemproj LIKE "%'.$search_string.'%"';


    $result = $imn_tool_db->query($query);
    while($results = $result->fetch_array()) {
        $result_array[] = $results;
    }

    if (isset($result_array)) {
        foreach ($result_array as $result) {


            $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['function']);
            $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['name']);
            $display_image = preg_replace("/".$search_string."/i", "".$search_string."", $result['image']);
            $display_stocktype = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['stocktype']);
            $display_vendor = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['vendor']);
            $display_notes = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['notes']);
            $display_oemproj = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['oemproj']);


            $output = str_replace('nameString', $display_name, $html);
            $output = str_replace('functionString', $display_function, $output);
            $output = str_replace('imageString', $display_image, $output);
            $output = str_replace('stocktypeString', $display_stocktype, $output);
            $output = str_replace('vendorString', $display_vendor, $output);
            $output = str_replace('notesString', $display_notes, $output);
            $output = str_replace('oemprojString', $display_oemproj, $output);

            echo($output);
        }
    }else{

        // Format No Results Output
        $output = str_replace('nameString', '<font color="red">No Data Found.</font>', $html);
        $output = str_replace('functionString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('imageString', 'http://i.imgur.com/default.png', $output);
        $output = str_replace('stocktypeString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('vendorString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('notesString', '<font color="red">No Data Found.</font>', $output);
        $output = str_replace('oemprojString', '<font color="red">No Data Found.</font>', $output);

        echo($output);
    }

    if (isset($result_array)) {
        foreach ($result_array as $result) {

                if($result['hasChildren']=="yes") { ?>
                           <center>
                           <table class='kitTable' width='98%' border='1' cellpadding='2' cellspacing='0' bordercolor='556587'>
                              <tr>
                                <td width='70%' class='kitHeading'><?php echo $result['kitHeading'];?></td>
                                <td width='15%' class='kitHeading'><?php echo $result['partHeading'];?></td>
                                <td width='15%' class='kitHeading'><?php echo $result['qtyHeading'];?></td>
                              </tr>
                              <tr>
                                <td><?php echo $result['kitPart1'];?></td>
                                <td class='trCenter'><?php echo $result['childPart1'];?></td>
                                <td class='trCenter'><?php echo $result['childPartQTY1'];?></td>
                              </tr> <?php

                        if($result['kitPart2']=== NULL) { ?>
                              <tr>
                                <td><?php echo $result['kitPart2'];?></td>
                                <td class='trCenter'><?php echo $result['childPart2'];?></td>
                                <td class='trCenter'><?php echo $result['childPartQTY2'];?></td>
                              </tr>
                             <?php }

                            ?>  
                            </table>
                            </center>
                            <?php
                    }

                    elseif($result['hasChildren']=="no") {
                        echo "";
                    }               
        }
    }

}

再次感谢!

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2015-03-11
    • 1970-01-01
    相关资源
    最近更新 更多