PHP generates the number "1" at the output of the function. I've never seen this done before
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;
}
On the HTML page, it puts a "1" immediately after the closing tag div
. What for?
source to share
From php.net reference:
"Return values
Returns 1, always. "
http://ca.php.net/manual/en/function.print.php
You have to assign $ output to the output you want, then use print to display that output.
source to share
From what you wrote, I think you are doing something like:
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");
or maybe
$somevar = holiday_hitlist("mytab","myuser");
print $somevar;
This is really a problem with the fact that you are "printing" the return value. In the above example, why return anything? You can:
a) set up a function as a routine that just does something and returns nothing. (i.e. just remove return $ output and print in print holiday_hitlist () )
or
b) create a function that returns the data you want and then do something with it.
Example b) is:
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>";
The above function helps to separate the presentation (i.e. HTML) from your data. While you are not perfect in this example, you will be able to see all your html in one block and debug it much easier.
source to share