如何从查询中的一个字段获取信息以在另一个查询中使用? [英] How can I get the information from one field in a query to use in another query?
问题描述
我仍在尝试完成我的详细信息页面的修复,我需要再修复一个.
我从这个查询开始.
注意:recordID 实际上是通过单击一个项目从上一页进入的.此外,name、img、item_code 和 type_id 是我表中的字段,而不是外部来源.
$recordID = $_GET['recordID'];$query_master_details = "SELECT * FROM master_list WHERE master_list.master_id = $recordID";$master_details = mysqli_query($conn, $query_master_details) 或 die(mysqli_error());$row_master_details = mysqli_fetch_assoc($master_details);$totalrows_master_details = mysqli_num_rows($master_details);
现在,我创建了它来确定如何显示有关所述项目的详细信息:
<div class="category"><h2><?php echo $row_master_details['name'];?></h2></div><?php$crafted = "SELECT * FROM `master_list` WHERE `type_id` <= 3 AND `length` = $row_master_details.length ORDER BY RAND() LIMIT 1";$result = mysqli_query($conn, $crafted);$crafted = array();如果 (mysqli_num_rows($result) > 0) {while($row = mysqli_fetch_assoc($result)) {$crafted[] = $row;}}如果 ($row_master_details['type_id'] > 3) {?><p><strong>代码 1</strong></p><p><?php echo $crafted['name'];?></p><p><img src="img/<?php echo $crafted['img']; ?>"/></p><p><?php echo $crafted['item_code'];?></p><br><br><p><strong>代码 2</strong></p><p><?php echo $row_master_details['name'];?></p><p><img src="img/<?php echo $row_master_details['img']; ?>"/></p><p><?php echo $row_master_details['item_code'];?></p><p><?php echo $row_master_details['length'];?>字符<?php }else { ?><p><?php echo $row_master_details['name'];?></p><p><img src="img/<?php echo $row_master_details['img']; ?>"/></p><p><?php echo $row_master_details['item_code'];?></p><p><?php echo $row_master_details['length'];?>字符<?phpmysqli_free_result($master_details);?><?php } ?><!-- end .container2 --></div>解释这里发生的事情:
这会查看在上一页上单击的项目并找到有关它的信息.如果它的 type_id 为 4 或更高,我需要它执行以下操作:
- 查看当前项目的长度".
- 从 master_list 中选择 type_id 为 1、2 或 3 且长度与第一个匹配的所有项目.
- 选择 1 个随机匹配项.
- 以与第一个相同的方式输出name、img和item_code.
如果原来选中的item的type_id小于4,就只贴原始信息.这部分有效.第二个查询中的部分没有.
我觉得我需要另一个 $_GET[],但不确定如何去做.recordID get 附加到用作链接的图像.
谁能帮我查询这个以便我得到我想要的东西?
这是我试图做的事情的图片,以帮助更有意义:
这是通过手动选择要匹配的项目并将它们放在一起来完成的.我想要随机显示一个项目.
这是当前代码的样子.这与我从 MiK 的回答中得到的相同:
解决方案 我终于通过调整和 MiK 的回答解决了这个问题.
这是我最终得到的最终代码:
<div class="category"><h2><?php echo $row_master_details['name'];?></h2></div><?php$crafted = "SELECT * FROM `master_list` WHERE `type_id` <= 3 AND `length` = ".$row_master_details['length']." ORDER BY RAND() LIMIT 1";$result = mysqli_query($conn, $crafted);$citem = 数组();如果 (mysqli_num_rows($result) > 0) {while($row = mysqli_fetch_assoc($result)) {$citem[] = $row;}}如果 ($row_master_details['type_id'] > 3) {?><p><strong>代码 1</strong></p><p><?php echo $citem[0]['name'];?></p><p><img src="img/<?php echo $citem[0]['img']; ?>"/></p><p><?php echo $citem[0]['item_code'];?></p><br><br><p><strong>代码 2</strong></p><p><?php echo $row_master_details['name'];?></p><p><img src="img/<?php echo $row_master_details['img']; ?>"/></p><p><?php echo $row_master_details['item_code'];?></p><p><?php echo $row_master_details['length'];?>字符<?php }else { ?><p><?php echo $row_master_details['name'];?></p><p><img src="img/<?php echo $row_master_details['img']; ?>"/></p><p><?php echo $row_master_details['item_code'];?></p><p><?php echo $row_master_details['length'];?>字符<?phpmysqli_free_result($master_details);?><?php } ?><p><h4>(需要不同的制作物品?刷新页面!)</h4></p><!-- end .container2 --></div>MiK 提供的答案修复了查询,但结果没有显示,因为我在数组内部有一个数组并且不得不调用内部数组.此外,我必须修复数组名称,使其与查询不同.
I am still trying to finish fixing my details page and I need one more piece to fix it.
I start with this query.
NOTE:recordID actually comes in from the previous page by clicking an item.
Also, name, img, item_code, and type_id are fields in my table not outside sources.
$recordID = $_GET['recordID'];
$query_master_details = "SELECT * FROM master_list WHERE master_list.master_id = $recordID";
$master_details = mysqli_query($conn, $query_master_details) or die(mysqli_error());
$row_master_details = mysqli_fetch_assoc($master_details);
$totalrows_master_details = mysqli_num_rows($master_details);
Now, I have created this to determine how to display the details about said item:
<div class="container2">
<div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div>
<?php
$crafted = "SELECT * FROM `master_list` WHERE `type_id` <= 3 AND `length` = $row_master_details.length ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $crafted);
$crafted = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$crafted[] = $row;
}
}
if ($row_master_details['type_id'] > 3) {?>
<p><strong>Code 1</strong></p>
<p><?php echo $crafted['name']; ?></p>
<p><img src="img/<?php echo $crafted['img']; ?>" /></p>
<p><?php echo $crafted['item_code']; ?></p>
<br><br>
<p><strong>Code 2</strong></p>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php }else { ?>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php
mysqli_free_result($master_details);
?>
<?php } ?>
<!-- end .container2 --></div>
To explain what is happening here:
This looks at the item that was clicked on the previous page and finds the information about it.
If it has a type_id of 4 or higher, I need it to do the following:
- Look at the "length" of the current item.
- Select all the items from the master_list that has a type_id of 1, 2, or 3 and a matching length to the first one.
- Choose 1 random match.
- Output the "name, img, and item_code in the same fashion as the first one.
If the item selected originally has a type_id of less than 4, it just posts the original information. This part works. The part from the second query does not.
I have a feeling I need another $_GET[], but not exactly sure how to go about it. The recordID get was attached to the image used as a link.
Can anyone help me to query this so that I get what I am wanting?
Here is a pic of what I am trying to do to help make more sense:
This was done by manually choosing the item to match and putting them together. I want a random item to display.
Here is what the current code looks like. This is the same thing I get with MiK's answer:
解决方案 I finally managed to fix this issue with a combination of tweaking and the answer from MiK.
Here is the final code I ended up with:
<div class="container2">
<div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div>
<?php
$crafted = "SELECT * FROM `master_list` WHERE `type_id` <= 3 AND `length` = ". $row_master_details['length']." ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $crafted);
$citem = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$citem[] = $row;
}
}
if ($row_master_details['type_id'] > 3) {?>
<p><strong>Code 1</strong></p>
<p><?php echo $citem[0]['name']; ?></p>
<p><img src="img/<?php echo $citem[0]['img']; ?>" /></p>
<p><?php echo $citem[0]['item_code']; ?></p>
<br><br>
<p><strong>Code 2</strong></p>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php }else { ?>
<p><?php echo $row_master_details['name']; ?></p>
<p><img src="img/<?php echo $row_master_details['img']; ?>" /></p>
<p><?php echo $row_master_details['item_code']; ?></p>
<p><?php echo $row_master_details['length']; ?> Characters</p>
<?php
mysqli_free_result($master_details);
?>
<?php } ?>
<p><h4>(Need a different crafted item? Refresh the page!)</h4></p>
<!-- end .container2 --></div>
The answer provided by MiK fixed the query, but the results were not showing because I had an array inside the array and had to call the inner array. Also, I had to fix the array name so it wasn't the same as the query.
这篇关于如何从查询中的一个字段获取信息以在另一个查询中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文