我有这个查询,我认为foreach有点错误....
public function lastsixcars(){
// this function checks whether the user name exists and if its a match
$result = $this->db->prepare("SELECT carID FROM AutoMarket_Selling ORDER BY carID DESC LIMIT 6");
$result->execute();
foreach($result as $result){
echo $car = $result['ID'];
}
}
我在页面上有这些......
<a href="details.php?carID=<?php echo $car ?>">
<a href="details.php?carID=<?php echo $car ?>">
<a href="details.php?carID=<?php echo $car ?>">
<a href="details.php?carID=<?php echo $car ?>">
<a href="details.php?carID=<?php echo $car ?>">
<a href="details.php?carID=<?php echo $car ?>">
...我希望数据库中的最后6个ID按顺序排列
因此...
<a href="details.php?carID=1st ID">
<a href="details.php?carID=2nd ID">
<a href="details.php?carID=3rd ID">
<a href="details.php?carID=4th ID">
<a href="details.php?carID=5th ID">
<a href="details.php?carID=6th ID">
任何人都可以告诉我如何做到这一点
感谢
答案 0 :(得分:1)
使这个功能
function lastsixcars(){
// this function checks whether the user name exists and if its a match
$result = $this->db->prepare("SELECT carID FROM AutoMarket_Selling ORDER BY carID DESC LIMIT 6");
$result->execute();
foreach($result as $results){
$return .= '<a href="details.php?carID='.$results['carID'].'">car '.$results['carID'].'</a>';
}
return $return;
}
这是php部分
echo lastsixcars();
答案 1 :(得分:1)
使用
echo $car = $result['carID'];
简而言之
foreach($result as $result){
echo "<a href='details.php?carID=".$result['carID']."'>Car".$result['carID']."</a>";
}
答案 2 :(得分:0)
对于foreach部分,这应该有效:
$results = array_slice($result, count($result) - 6);/* get the last 6 in the same order they are in the database*/
foreach ($results as $car) {
echo '<a href="details.php?carID='.$car['0'].'">'; //assuming ID is the first field returned
}
希望它有所帮助。