Jump to content

[SOLVED] Print <<< Here Document


Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/88885-solved-print/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455473
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455556
Share on other sites

$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>";

Link to comment
https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455571
Share on other sites

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455585
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/88885-solved-print/#findComment-455714
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.