gpt4 book ai didi

PHP/MySQL 双循环

转载 作者:行者123 更新时间:2023-11-29 13:49:02 25 4
gpt4 key购买 nike

我正在处理一个目录,其中一些列表具有与其关联的图像,而其他列表则没有。我想知道如何在循环中编写循环来获取结果。

例如,用户选择他们想要结果的州,查询将转到数据库,请求该州的所有列表。

<?php
if (isset($_POST['searchButton'])) {

$state = $_POST['state'];

$query = "SELECT * FROM directory LEFT JOIN directory_images ON directory.id = directory_images.user_id WHERE directory.state = '$state' ";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) == 0) {
echo "<p>Sorry, there are no listings in '$state', check back soon!</p>\n";
}
else
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$has_support_pics = $row['file_name'];
?>

<h4><?php echo $name ?></h4>
<p><?php echo $address ?><br/>
<?php echo $city . ' ' . $state . ', ' . $zip; ?><br/>
</p>
<?php
// check to see if ID has extra images
if (isset($has_support_pics)) {
$query2 = "SELECT file_name FROM directory_images WHERE user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());
echo $query2.'<br/>';
?>
<ul class="support_images">
<?php
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$support_image = $row['file_name'];
echo $support_image.'<br/>';
}
?>
</ul>
</div>
<br/>
</div>
<?php
}
echo "<hr/>";
}

}?>

最佳答案

不要在循环中运行查询 - 使用联接。这是一个教程:http://thewebmason.com/tutorial-parent-child-lists/

关于PHP/MySQL 双循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16975192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com