我不小心删除了我的上一篇文章,所以在这里再次:
我有一个有几个零件的小仓库,我正在编写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的链接<——使用这个SQLfiddle
我希望这是在PHP或任何你都认为会最好的工作与我所提供的代码。,再次感谢。
如果我是你,我会使用PDO。PDO和mysql之前的mysql是一样的。简而言之:它"只是"另一个PHP的MySQL适配器(PDO代表PHP数据对象)。
如果使用PDO,这很可能是你想要做的:
步骤1—打开连接
在PDO中打开一个连接,你所要做的就是:
$dbh = new PDO('mysql:host=xxx.xxx.xxx.xxx;port=xxxx;dbname=xxx', 'username', 'password');
步骤2——创建查询
要从数据库中收集数据,你必须知道你在寻找什么。这就是查询的作用。假设我们想要从'Products'表中选择所有列,我们将执行以下操作:
$query = "SELECT * FROM products";
当然,当使用join和所有其他整洁的MySQL特性时,这些查询可能会变得更加困难,但是现在让我们保持简单。
步骤3—准备查询
在执行查询之前,准备好它:
$stmt = $dbh->prepare($query);
这样你的查询就可以启动了,所以让我们来执行你的查询:
$stmt->execute();
如果一切顺利,我们可以继续步骤4。
步骤4—检索数据
也许最重要的步骤(至少,如果您使用数据库并希望检索数据)是检索数据(呸…)。PDO(和mysqli)有一个简单的方法通过ResultSet循环:
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?
}
}
我可能遗漏了一些要点,但我想这可能会让你开始。
我能够使用下面的代码完成这个,感谢大家的帮助!
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 "";
}
}
}
}
再次感谢!