A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP is generating a numeral “1” on output of a function. I've never seen it do this before - page 1
User InfoPosts
PHP is generating a numeral “1” on output of a function. I've never seen it do this before#1
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. Why?

posted date: 2008-12-11 20:37:00


Re: PHP is generating a numeral “1” on output of a function. I've never seen it do this before#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 20:37:01


Re: PHP is generating a numeral “1” on output of a function. I've never seen it do this before#3
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 "..."

posted date: 2008-12-11 20:43:00


Re: PHP is generating a numeral “1” on output of a function. I've never seen it do this before#4
From php.net Reference:"Return ValuesReturns 1, always."http://ca.php.net/manual/en/function.print.phpYou should assign $output to be the output that you would like, then use print to display that output.

posted date: 2008-12-11 20:49:00


Re: PHP is generating a numeral “1” on output of a function. I've never seen it do this before#5
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())orb) 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.

posted date: 2008-12-11 22:05:00


Re: PHP is generating a numeral “1” on output of a function. I've never seen it do this before#6
It looks like you got some pretty good answers to your question in December of 2008. Why didn't you accept any of them?

posted date: 2011-08-08 06:52:00


Re: PHP is generating a numeral “1” on output of a function. I've never seen it do this before#7
cringes at the bare sql with no protection (that is, if I'm reading it right, not very familiar with PHP or SQL)

posted date: 2012-05-28 18:04:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.