Trium918 Posted February 1, 2008 Share Posted February 1, 2008 Top_Nav links are stored in the MySQL database. The query process works fine, but I am having a hard time placing the getTopNav() function inside of the print <<<HEADER Here Document. I tried assigning getTopNav() function to a variable but that is like calling the function directly. Example: $pageTitle = getTopNav(); I am trying to insert getTopNav() Function into print <<<HEADER Thanks in advance! getTopNav() Function <?php // HEADER TOP_NAV function getTopNav() { $query="SELECT title FROM _pages"; $result = mysql_query($query); $num_results = mysql_num_rows($result); for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_assoc($result); $pageTitle = htmlspecialchars( stripslashes($row["title"])); // Output print <<<TOPNAV <li><a href="page.php?content=$pageTitle">$pageTitle</a></li> TOPNAV; }// End of For loop } ?> print <<<HEADER Here Document <?php print <<<HEADER <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Page</title> <link rel="stylesheet" href="style/index.css" type="text/css"> <link rel="stylesheet" href="browse/style/browse.css" type="text/css"> </head> <body> <div id="container"> <div class="top_bar"> <div class="middle_bar">November 13, 2007</div> </div> <div id="header"> </div> <div id="topnav"> <ul> <li><a href="index.php">Home</a></li> NEED TO PLACE VARIABLE HERE!!!!!!! </ul> </div> <div class="spacer"> </div> HEADER; ?> Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/ Share on other sites More sharing options...
wildteen88 Posted February 1, 2008 Share Posted February 1, 2008 You cannot call functions from within a HEREDOC statement, only variables Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455466 Share on other sites More sharing options...
PFMaBiSmAd Posted February 1, 2008 Share Posted February 1, 2008 In general, functions should return results instead of directly outputting them. This makes them general purpose and easy to reuse. If you changed your function so that it build the content in a string and returned it, you could then call that function before the start of your print <<<HEADER block and assign the content to a variable that you would then put into the print <<<HEADER statement. Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455473 Share on other sites More sharing options...
Trium918 Posted February 1, 2008 Author Share Posted February 1, 2008 The getTopNav() function is only returning one link from the database. Could someone please explain to me why it is not returning the other links? There are 11 links total. <?php function getTopNav() { $query="SELECT title FROM _pages"; $result = mysql_query($query); $num_results = mysql_num_rows($result); for ($i = 0; $i < $num_results; $i++) { $row = mysql_fetch_assoc($result); $pageTitle = htmlspecialchars(stripslashes($row["title"])); $pageTitle = "<li><a href=\"page.php?content=$pageTitle\">$pageTitle</a></li>"; }// End of For loop return $pageTitle; } ?> <?php $topNavLinks = getTopNav(); print <<<HEADER <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Page</title> <link rel="stylesheet" href="style/index.css" type="text/css"> <link rel="stylesheet" href="browse/style/browse.css" type="text/css"> </head> <body> <div id="container"> <div class="top_bar"> <div class="middle_bar">November 13, 2007</div> </div> <div id="header"> </div> <div id="topnav"> <ul> <li><a href="index.php">Home</a></li> $topNavLinks </ul> </div> <div class="spacer"> </div> HEADER; ?> Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455556 Share on other sites More sharing options...
PFMaBiSmAd Posted February 1, 2008 Share Posted February 1, 2008 $pageTitle = htmlspecialchars(stripslashes($row["title"])); $pageTitle = "<li><a href=\"page.php?content=$pageTitle\">$pageTitle</a></li>"; Each assignment statement replaces the contents in $pageTitle. The second line needs to be changed so that it concatenates each new line onto the existing contents. Use this - $pageTitle = htmlspecialchars(stripslashes($row["title"])); $pageTitle .= "<li><a href=\"page.php?content=$pageTitle\">$pageTitle</a></li>"; Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455571 Share on other sites More sharing options...
Trium918 Posted February 1, 2008 Author Share Posted February 1, 2008 The code below is only returning one value still. It should be eleven link returned. Thanks in advance! <?php // HEADER TOP_NAV function getTopNav() { $sql = "SELECT title FROM _pages"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { for ($i = 0; $i < $row = mysql_fetch_assoc($result); $i++) { $pageTitle = htmlspecialchars(stripslashes($row["title"])); $pageTitle .= "<li><a href=\"page.php?content=$pageTitle\">$pageTitle</a></li>"; }// End of For loop }else { echo "No Results Found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } return $pageTitle; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455585 Share on other sites More sharing options...
trq Posted February 1, 2008 Share Posted February 1, 2008 You never iterate through your for() loop. Its much simpler to use a while. Also, your still overiding the $pageTtitle variable. <?php function getTopNav() { $sql = "SELECT title FROM _pages"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { $pageTitle .= "<li><a href=\"page.php?content=" . htmlspecialchars($row["title"]) . "\">" . htmlspecialchars($row["title"]) . "</a></li>"; } return $pageTitle; } } return false; ?> ps: You really shouldn't need the call to stripslashes if your data was inserted correctly. Quote Link to comment https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455714 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.