tags:

views:

171

answers:

4

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 an HTML page, it puts the "1" immediately following the closing div tag. What gives?

+9  A: 

See the line

$output = print "<li><a href=\"$url\" target=\"_blank\">$title</a> $price</li>";

you should probably remove the print after the $output =

Or maybe you just need to remove the $output =

I am not quite sure what you intend.

To explain, $output is getting the return value of print "..."

Zoredache
+6  A: 

From php.net Reference:

"Return Values

Returns 1, always."

http://ca.php.net/manual/en/function.print.php

You should assign $output to be the output that you would like, then use print to display that output.

barfoon
A: 

Nice. Thanks guys.

littlerobothead
+2  A: 

From what you've written 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;

It's really a problem with the fact that you are "printing" the returned value. In your above example why return anything? You could either;

a) set up the function as a routine that just does something and returns nothing. (ie: just remove the return $output and the print in print holiday_hitlist())

or

b) create a function that returns the data you want and then do something with it.

An example of 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 function above helps to separate the presentation (ie: the HTML) from your data. While not perfect in this example you will be able to see all your html in one block and debug it a lot easier.

Mark Nold