Can some one tell me how to "include" a variable from another .php file without all its other content.
index.php
<?php
$info=file('somedir/somefile.php');
$v1=trim($info[2]);
$v2=trim($info[3]);
$v3=trim($info[4]);
?>
the somedir/somefile.php
<?php
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
All the other content there may not be runned or showed.
?>
Can anybody please help me??
Edit:
Its for my dynamic page.
<html>
<?php
include_once 'config.php';
include_once 'includes/mysqlconnect.php';
$url_slash=$_SERVER['REQUEST_URI'];
$url= rtrim($url_slash, '/');
//$url = basename($url);
$info=file('sites/'.$url.'.php');
$title=trim($info[2]);
?>
<head>
<meta charset="UTF-8">
<title>$title</title>
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
</head>
<body class="body">
<div class="container-all">
<?php include_once 'includes/header.php';?>
<div class="container">
<?php include_once 'includes/navigationbar.php';?>
<?php include_once 'includes/rightsidebar.php';?>
<div class="content"><?php
if ($url==''){
include_once "sites/home.php";
}
elseif (file_exists("sites/$url.php") && is_readable('/var/www/html/sites/'.$url.'.php')){
include_once '/var/www/html/sites/'.$url.'.php';
}
else {
include_once 'sites/404.php';
}
?></div>
<?php include_once 'includes/footer.php';?>
</div>
</div>
</body>
</html>
Hope you understand my question now.
Programming is just driving your thoughts :)
So what i want to say that your question is how you can include just some part of an included file and my answer is that you can achieve that by doing a test each time the main file is included from withing this file to see if the file is included internally or not and you can be more precise in a way that you split your main file into block which are loaded due suitable variable
Take a look for this workaround and hope you will understand what i mean
Supposing we have the main file named main.php contains that contents
<?php
echo 'I am a java programmer';
echo 'I know also PHP very well';
echo 'When the jquery is my preferred toast !';
?>
now i have three external files that will include that file each file is specific for one of this 3 programming language
So i will create my 3 files in this way :
File : java.php
<?php
$iamjavadevelopper = 1;
include_once("main.php");
?>
File : phpfav.php
<?php
$iamphpdevelopper = 1;
include_once("main.php");
?>
File : jquery.php
<?php
$iamjquerydevelopper = 1;
include_once("main.php");
?>
and my main.php will be coded in this way
<?php
if(isset($iamjavadevelopper))
echo 'I am a java programmer';
if(isset($iamphpdevelopper))
echo 'I know also PHP very well';
if(isset($iamjquerydevelopper))
echo 'When the jquery is my preferred toast !';
?>
By this way each one of our three external files will show just a part of the included file :)
The only way I can think of without cookies or session's is to make an if condition in the page.
like that:
index.php
<?php include('somedir/somefile.php');?>
the somedir/somefile.php
<?php
if ($pageName != 'somefile.php') {
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
} else {
// All the other content
}
?>
Save the variables in a separate file that can be included separately. Do it the sane way. Structure your code properly, don't try to invent solutions for problems you have because your structure is messy.
Related
I have a website with a template page that is something like this
<html>
<head>
<title>{{TITLE}}</title>
</head>
<body>
<div id = "header"> ... </div>
<?php include 'content.php'; ?>
<div id = "footer"> ... </div>
</body>
</html>
And then content.php would look something like this
<?php
$title = "xx";
#other php code here
?>
<p> more content </p>
My question is whether there is some way to set this up so that I am able to set the title from the file included in the middle of the page (without using javascript). I know that most people suggest including it at the top but if I were to do that the html would be at the top instead of between the header and the footer. I've wracked my brains for a while and I haven't really figured out a good way to do this (and there are a variety of possible files to be included; content.php is just an example, so I really do need some way to do this dynamically). I want to avoid putting too much code outside of the template. Any ideas?
Use output buffering:
<?php
ob_start();
include 'content.php';
$content = ob_get_clean();
?>
<html>
<head>
<title><?=htmlspecialchars($title)?></title>
</head>
<body>
<div id = "header"> ... </div>
<?=$content?>
<div id = "footer"> ... </div>
</body>
</html>
I'm not sure if you're using a templating engine or not (something like Laravel's Blade templates allow for this, I believe), but assuming you are using straight PHP, I have found the most efficient way to do this is to approach from the opposite direction and include the template file in each of my content files.
For example, I may have template.php, which has multiple functions, like this:
<?php
function createHeader($title, $keywords) {
//echo or <<<EOD your header with the set variables
}
function createFooter(...) { ... } //etc
And then, in my 'child' files, I would do:
<?php include('template.php'); ?>
<?php createHeader("My Website Front Page!", "fun, good times, joy"); ?>
<h1>My page content</h1>
<p>Content goes here</p>
<?php createFooter(...); ?>
This is a different structure from what you were attempting, though, and may not retain your intended structure.
Consider the code
<?php
.....
.....
$error="abc";
......
?>
<html>
<head></head>
<body>
.....
<?php echo $error ?>
.....
</body>
</html>
I am new to php. I want to access the same "error" variable at two parts in the same file. Is there any way to do so? Or I have to create another file with the "error" variable and then include it in the file where I need it again?
You should be able to access the variable as many time as you need it if it's part of the same scope.
This will work:
<?php $foo = 'bar' ?>
<hr />
<?php echo $foo; ?>
This will not:
<?php
function set_foo_variable() {
$foo = 'bar';
}
set_foo_variable();
?>
<hr />
<?php echo $foo; ?>
Make sure your variable is always in the same scope AND is set.
Here's more documentation on PHP scope: http://php.net/manual/en/language.variables.scope.php
Have you already tried accessing it?
You shouldn't have an issue doing something like the following:
<?php $error="abc"; ?>
<html>
<head>
</head>
<body>
<?php echo $error; ?>
</body>
<?php echo $error; // access #2 ?>
</html>
<?php echo $error; // access #3 ?>
Note:
For the future, I would really try to improve the code format of your questions, mention what you tried to do already and provide more details about your issue.
I am new to PHP. I have these 3 files :
index.php
functions.php (to organize functions)
header.php
I want to simplify(which has been done so far) the index.php page thus I do not need to write the and all stuff again and again. So I created header.php that can be loaded by index.php:
header.php
<!Doctype html>
<html lang="en">
<head>
<title>Learn PHP</title> <!--This is the problem, every page that loads header.php will have same title! -->
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
I have even simplified things further by making a function in functions.php so that I can just type "get_header()" in the index.php without writing the whole code again.
functions.php
<?php
function get_header(){
if (file_exists('header.php')){
require 'header.php';
}
else{
echo "There is an error retrieving a file";
}
}
?>
Now, how do I allow this index.php to have custom page title instead of the default given by header.php?
Am I missing something important. I have tried creating a variable and try to pass it to the functions.php, but it didn't work. Or is there any cleaner way to do this?
I am inspired by how wordpress organize their files, I have checked the wordpress file. And then I decided to try something from scratch so I understand better and improve my PHP skills.
I know can use POST and GET, but no I dont want to refresh or load a new page just to change a page title especially index.php
EDIT :
Here I included my index.php
<?php
require 'functions.php';
?>
<?php
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
It seems like all you need you want is simple includes. You're actually making it harder by using a function, here, because an include has the same scope as where it was included from. E.g.
header.inc
…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…
index.php
<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome
another-page.php
<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content
If you want to use a function, give it parameters.
function get_header($title = 'Some default title') {
…
}
the included file will have access to the variables in the function's scope.
in the functions.php
function get_header(){
if (file_exists('header.php'))
require 'header.php';
else echo "There is an error retrieving a file";
}
in the header.php, and in the balise title you call the session parameter
<!Doctype html>
<html lang="en">
<head>
<title>
<?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else 'Learn PHP'; ?>
</title>
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
and in the index.php
<?php
session_start();
$_SESSION['title-page'] = 'this is the welcome Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
and in another-page.php
<?php
session_start();
$_SESSION['title-page'] = 'this is an another Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
I'm working with XML and PHP to populate a web form drop down box.
I have a html page
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<header>
<h1>Title</h1>
</header>
<form>
<div id ="select xml">
<?php include 'dropdown.php'; ?>
</div>
</form>
</body>
</html>
and I am hoping to include the following PHP to generate the actual box with the file names of the XML.
<?php
//echo(substr(glob("xml/*.xml")[0],4));
echo "<p>
<label>Select list</label><br>
<select id = \"selectxml\">
<option value'0'>--Please Select--</option>";
$count = count(glob("xml/*.xml"));
$files = glob("xml/*.xml");
for ($i = 0; $i < $count; $i++) {
//echo(substr($files[$i],4));
//echo "<br>";
$filename = (substr($files[$i],4));
echo "<option value=$i+1>$filename</option>";
}
echo "</select><br>
<br>
</p>";
?>
I'm aware the PHP isnt perfect, but it works.
The issue is that the Include HTML does not work when I run the page - any ideas?
Change the extension to .php and you will be okay .
When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. and any PHP code, will be processed as a plain text .
I mean when you put :
<?php echo "hello" ?>
in a HMTL page, browser will show :
<?php echo "hello" ?>
But, if the page have .php extension, browser will show :
hello
Rename your html page from .html to .php
it;s impossible, but with jQuery.load function you can do this :
$("#select-xml").load("dropdown.php");
My layout.php calls include_javascripts() before my componet could call sfResponse::addJavascript(). Is there a "helper" or a "best practice" to handle this?
Do I have to Seperate the call sfResponse::addJavascript()? I were happy to avoid it.
Here ist my actual workaround:
<head>
<?php $nav = get_component('nav', 'nav') /* Please show me a better way. */ ?>
<?php include_javascripts() ?>
...
</head>
<body>
<?php /* include_component('nav', 'nav') */ ?>
<?php echo $nav ?>
...
</body>
Thanks
From: http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer
File Inclusion Configuration
// In the view.yml
indexSuccess:
stylesheets: [mystyle1, mystyle2]
javascripts: [myscript]
// In the action
$this->getResponse()->addStylesheet('mystyle1');
$this->getResponse()->addStylesheet('mystyle2');
$this->getResponse()->addJavascript('myscript');
// In the Template
<?php use_stylesheet('mystyle1') ?>
<?php use_stylesheet('mystyle2') ?>
<?php use_javascript('myscript') ?>
If your component is always used just move your javascript inclusion into the layout's rendering in your app's view.yml:
default:
javascripts: [my_js]
There is no need to separate the JS call when it is always used.
UPDATE:
If you must maintain the JS inclusion with the component you can place your component call in a slot before your include_javascripts() call to add it to the stack to be rendered and then include the slot in the appropriate place:
<?php slot('nav') ?>
<?php include_component('nav', 'nav'); ?>
<?php end_slot(); ?>
<html>
<head>
<?php include_javascripts() ?>
...
</head>
<body>
<?php include_slot('nav'); ?>
<?php echo $nav ?>
...
</body>
</html>
You may have to disable the sfCommonFiter which is now deprecated. The common filter automatically adds css / js into the layout.
Once you have done this, you can move the include_javascripts call anywhere within the layout.php file, below the include_component('nav', 'nav') call