function holiday_hitlist($tablename, $hit_user){
global $host, $user, $pass, $dbname;
$link = mysql_connect($host, $user, $pass, $dbname);
print "<div class=\"hit_list\">
<h3>My Holiday Hitlist</h3>
<p>Five things I want the most, based on my desirability ratings.<br/>You can't go wrong with this stuff!</p>
<ol>";
$sql = "SELECT title, URL, price FROM $dbname.$tablename WHERE user='$hit_user' AND rank >= 3 ORDER BY date DESC LIMIT 5";
$result = mysql_query($sql) or die ("Couldn't retrieve holiday hit list for this user. " . mysql_error());
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$url = $row['URL'];
$price = "$" . $row['price'];
$output = print "<li><a href=\"$url\" target=\"_blank\">$title</a> $price</li>";
}
print "</ol></div>";
return $output;
}
在HTML页面上,它会在关闭div
标记后面紧跟“1”。为什么?
答案 0 :(得分:9)
见行
$output = print "<li><a href=\"$url\" target=\"_blank\">$title</a> $price</li>";
你应该在$ output =
之后删除打印件或许你只需要删除$ output =
我不太清楚你的意图。
为了解释,$ output正在获得print“...”的返回值
答案 1 :(得分:6)
来自php.net参考:
“返回值
始终返回1。“
http://ca.php.net/manual/en/function.print.php
您应该将$ output指定为您想要的输出,然后使用print显示该输出。
答案 2 :(得分:2)
根据你所写的内容,我认为你正在做的事情如下:
function holiday_hitlist($tablename, $hit_user){
/* connections etc
*/
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$url = $row['URL'];
$price = "$" . $row['price'];
$output = print "<li><a href=\"$url\" target=\"_blank\">$title</a>$price</li>";
}
print "</ol></div>";
return $output;
}
print holiday_hitlist("mytab","myuser");
或者
$somevar = holiday_hitlist("mytab","myuser");
print $somevar;
您正在“打印”返回的值这一事实确实存在问题。在上面的例子中为什么要返回什么?你也可以;
a)将该函数设置为只执行某些操作并且不返回任何内容的例程。 (即:只需删除返回$ output 并打印打印holiday_hitlist())
或
b)创建一个返回所需数据的函数,然后用它做一些事情。
b)的一个例子是;
function holiday_hitlist($tablename, $hit_user){
/* connections etc
*/
while($row = mysql_fetch_array($result)) {
$title = $row['title'];
$url = $row['URL'];
$price = "$" . $row['price'];
$output .= "<li><a href=\"$url\" target=\"_blank\">$title</a>$price</li>";
}
return $output;
}
$somevar = holiday_hitlist("mytab","myuser");
print "<div class=\'hit_list\'>
<h3>My Holiday Hitlist</h3>
<p>Five things I want the most, based on my desirability ratings.<br/>You can't go wrong with this stuff!</p>
<ol>
$somevar
</ol></div>";
上述功能有助于将演示文稿(即:HTML)与您的数据分开。虽然在这个例子中不完美,但你可以在一个块中看到所有的html,并且可以更容易地调试它。