【发布时间】:2017-04-03 23:54:06
【问题描述】:
我知道有几个问题与此类似,我已经查看了它们,但我仍然没有设法修改我的功能以便工作。
所以我有这个函数 getCar_2();在functions.php 页面中。问题是它应该显示更多的汽车,因为我在数据库中有更多的汽车,但它只显示最后一辆。和getCar_1()一样,两个函数是一样的。
函数如下:
<?php
function getCar_2(){
global $connect;
$text = "";
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
$text = "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
return $text;
}
?>
并像这样显示在另一个页面上:
<?php
$car_1 = getCar_1();
$car_2 = getCar_2();
if(!empty($car_1) || !empty($car_2)){
?>
<div class="block">
<div class="up">
<div class="title"><h4>Cars</h4></div>
</div>
<div class="down">
<div class="column_1"><b><?php echo $car_1; ?></b></div>
<div class="column_2"><b><?php echo $car_2; ?></b></div>
</div>
</div>
<?php
}
?>
【问题讨论】:
-
查看你的代码应该返回空字符串或第一辆车 isset($car_model_2) && isset($car_image_2) ...删除循环内的返回..
标签: php html arrays while-loop return