Php 如何在变量中包含while循环
我有以下代码:Php 如何在变量中包含while循环,php,variables,Php,Variables,我有以下代码: <?php if(isset($_POST['indexSearchSubmit'])) { foreach($_POST['industryList'] as $selected) { $_POST['industryList'] = $selected; $locationListResults = $_POST['locationList']; $results = mysqli_query($con,"SE
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings WHERE location = '$locationListResults' AND industry = '$selected'");
while($row = mysqli_fetch_array($results))
{
echo $row['industry'];
echo $row['location'];
echo $row['title'];
echo $row['description'];
}
}
mysqli_close($con);
}
?>
放入数组
$list = array();
while($row = mysqli_fetch_array($results))
{
$list[] = $row;
}
将所有值放入一个数组中
$rows = array();
$x = 0;
while($row = mysqli_fetch_array($results))
{
$rows[$x]['industry'] = $row['industry'];
$rows[$x]['location'] = $row['location'];
$rows[$x]['title'] = $row['title'];
$rows[$x]['description'] = $row['description'];
$x++;
}
return $rows;
然后可以使用$rows作为数组
foreach($rows as $v){
echo $v['industry']." ".$v['location']."<br />";
echo $v['title']." ".$v['description']."<br />";
}
foreach($v行){
echo$v[“行业”]。“$v[“位置”]。”
;
回显$v['title']。“.$v['description']。”
”;
}
您应该为此使用。结果将是一个数组,其中每个元素是一行,每行是一个关联数组,其键与示例中的行相同
$data = mysqli_fetch_all($result);
$data[0]["industry"]; //Data in the first row
然后,您可以在$data
上循环以将其输出到页面上的任何位置。您可以尝试此方法
$arrayList = array();
while($row = mysqli_featch_array($results)){
$arrayList[] = $row;
}
print_r($arrayList);
我建议您使用json,因为您希望以这种方式使用它。您正在SQL注入自己,任何人都可以非常轻松地打印出您的所有列表。很抱歉,我使用了您的方法,但现在当我回显$rows[1]时它应该显示所有结果,但只显示一个结果,所以我想我做了一些错误的事情,因为它没有循环任何想法为什么?任何更改我可以获得追加投票,因为我不能再发布问题显示我可以循环数据吗?只需执行标准的foreach循环。任何更改我可以获得追加投票,因为我不能再发布问题