Jump to content

[SOLVED] Using variable of footer.php


smartguyin

Recommended Posts

In my index.php i had included header.php and footer.php

My index.php page
[code]<?
include "header.php

//////// BODY

include "footer.php
?>[/code]

My header.php page
[code]<?
echo "<html>
<head>
<title>$titlename</title>
<head>
<body>";
?>[/code]

My footer.php page
[code]
<?
///// the variable $title i got it from database here

$row['title'] = $titlename;

///// I WANT TO USE THIS $titlename IN HEADER.PHP

echo "</body>
</html>";
?>
[/code]


Now in header file there is title tag.... i want to use one $var variable from footer.php file but i am not able to do it coz footer.php file is included after header.php...... sooo i cannot get the Title in header.php file to be displayed........

footer.php file is the main body which interacts with mysql database and works.... sooo i suppose there is product title stated as a variable in footer.php i want to use tht variable in header.php .........
please tell me how to do it...........
Link to comment
https://forums.phpfreaks.com/topic/32546-solved-using-variable-of-footerphp/
Share on other sites

Well... chances are you're going to run your footer function AFTER your header function, so your point is moot anyways.  You're going to have to run the code for the footer in both locations and just not do the display part in the header for the footer.... or just run the code in the header... the variable will still be available for the footer when you include it.
[quote]can any one explain me the logic which i can use[/quote]

You cannot use variables before they are difined. However, if you wrap your variable within a function, you can call this function before its defined. eg;

[code]
<?php
  $foo = bar();
  echo $foo;

  function bar() {
    return "this is foo";
  }
?>
[/code]

This is of course a poor way of getting around your problem however. Ideally you need to restructure your entire logic so that it runs from top -> bottom.
I had solved my prblem by using this below

i used function to get wht i wanted.... in header.php i used

function setup_page ($title) {
<html>
<head>
<title>$title</title>
</head>
}


In footer.php

setup_page("$row['title']")

And in this was i was able to trf the content i want from footer to header.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.