我需要一些PHP的帮助。 只是一点点解释: (我试图使用此代码显示5个结果
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
} else {
header("Location: vendi.php");
}
echo '<br /><a href="/'.$path.'">Back to Archive</a>';
}
显示但是想要第1个结果(图像+新闻的标题和其他结果只是标题)。 希望我能清楚地写出我需要帮助的内容。 谢谢
答案 0 :(得分:1)
您的SQL语句仅提取一行。这不是一个完整的解决方案,但应该让你更接近:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$n_id = (int)$_GET['id'];
$path = '';
$count = 0;
$sql = 'SELECT * FROM test2 WHERE id BETWEEN ' . $n_id ' AND ' . ($n_id + 5);
$news = mysql_query($sql);
while ($result = mysql_fetch_array($news)) {
$title = $result['title'];
$date = $result['date'];
echo '<b>' . $title . ' | ' . $date . '</b>';
if ($count < 1) {
echo '<br/><img src="images.php?id='. $n_id .'>';
$count++;
}
}
if ($count == 0) { header("Location: vendi.php"); }
echo '<br /><a href="/'.$path.'">Back to Archive</a>';
}
答案 1 :(得分:0)
在循环搜索结果时,您可以检查您是否处于第一个结果:
$counter = 0;
while ($row = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
echo '<b>' . $title . ' | ' . $date . '</b>';
if ( $counter == 0 ) {
echo '<br /><img src="images.php?id='. $n_id .'>';
}
}
答案 2 :(得分:0)
这应该这样做。
$n_id = mysql_real_escape_string ($_GET['id']);
$path = '';
if(isset($n_id) && $n_id != "") {
$sql = 'SELECT * FROM test2 WHERE id="' . $n_id . '"';
$news = mysql_query($sql);
$first = TRUE;
if($result = mysql_fetch_array($news)) {
$title = mysql_result($news,0,"title");
$date = mysql_result($news,0,"date");
if($first == TRUE) {
echo '<b>' . $title . ' | ' . $date . '</b>';
$first = FALSE;
}
else {
echo '<b>' . $title . ' | ' . $date . '</b>
<br/>
<img src="images.php?id='. $n_id .'>';
}
}
else {
header("Location: vendi.php");
}
echo '<br /><a href="/'.$path.'">Back to Archive</a>';
}
}