How to include a seperate header file? - php

Hey so I am currently working on an html/php page.
I have a header file that I am supposed to include in my page but for some reason it's not working and I have no idea, I've done the same thing for all my other pages and it has worked so I don't know why all of a sudden its not working.
<?php
include 'header.php';
?>
<center>
<p>
This page utilizes several postgreSQL method calls. Such as pg_connect(),
pg_query(), and pg_fetch_result().
</p>
<!-- setup the table -->
<table border="1" width="75%">
<tr><th width="50%">Make</th><th width="15%">Model</th><th width="20%">Year</th><th width="15%">MSRP</th></tr>
<?php
$output = ""; //Set up a variable to store the output of the loop
//connect
$conn = pg_connect("host=127.0.0.1 dbname=slotegraafd_db user=slotegraafd password=100658347" );
//issue the query
$sql = "SELECT automobiles.make, automobiles.model, automobiles.year, automobiles.msrp
FROM automobiles
ORDER BY automobiles.year ASC";
$result = pg_query($conn, $sql);
$records = pg_num_rows($result);
//generate the table
for($i = 0; $i < $records; $i++){ //loop through all of the retrieved records and add to the output variable
$output .= "\n\t<tr>\n\t\t<td>".pg_fetch_result($result, $i, "Make")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Model")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Year")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "msrp")."</td>\n\t</tr>";
}
echo $output; //display the output
?>
</table>
<!-- end the table -->
</center>
</body>
</html>
This is my full set of code with the include statement. There is no opening html or body tag because it is in the header file therefore I am not supposed to add it in this page.
Anyway any help?
Thanks.

Make sure that the file path is correct. If this new file is in a folder, then you need to change your include statement.
For example, if this new file is in a folder called foo, you could say: include '/foo/header.php
Or you can use a relative path: include ../header.php
Read up on file paths if needed.

It should work just the way your code looks like. Maybe your path of header.php isnt correct, or the content can't be displayed / executed so it shows nothing.

Related

how to pass a variable to a layout page PHP

Hello im using two files 1. layout.html.php(for all the HTML code) 2.layout.php(for all the PHP code)
Im trying to send a variable from the layout.php to layout.html.php file
The variable is equal to a list of all jobs from a database table
See code below
I want to use this list of jobs in a unordered list as links in layout.html.php file
I would appreciate any help i can get with this
CODE FOR LAYOUT.HTML.PHP
<li>Jobs
<ul>
<?php
foreach ($jobs as $job){?>
<li><?= $job['name']?> </li>
<?php }?>
</ul>
CODE FOR LAYOUT.PHP
<?php
require '../loadTemplate.php';
$stmt = $pdo->prepare('SELECT * FROM job');
$stmt->execute();
$jobs= $stmt->fetchAll();
$templateVars = ['stmt' => $jobs];
$output = loadTemplate('../templates/layout.html.php',['stmt' => $jobs]);
require '../templates/layout.html.php';
?>
If in a file A you got the variable X:
<?php
$frase = "abcdef";
?>
Then if you want to use the same variable in another file you can use:
<?php
include('A.php');
echo $frase;
?>
There is other ways to do that. You can also use the Session, cookies, or just pass in the url.
In my case, I like to do in that way:
I have a page home.php. In that page I represents a consult. So, in home.php I got:
<?php
include 'php/db.php'
$result = BuscaDados();
while($row = $result->fetch_assoc()){
// here you get all lines
}
?>
In db.php I got a implementation of my functions that return the consult. Try it out.

Order of inclusion and interpretation for php and html

I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.

PHP - passing variable from one PHP file to another PHP file

I am new to PHP and trying to pass variable from one page to another page. Initially, I have a HTML page which contains the frames as below.
<!DOCTYPE html>
<html>
<frameset cols="70%,*">
<frame src="index.php">
<frame src="slider.php">
</frameset>
</html>
As seen above, I have 2 PHP pages, in which am trying to send some values from index.php file to my slider.php file. My index.php file is as below.
<?php
$names = file('demo.csv');
$page = $_GET['page'];
$pagedResults = new Paginated($names, 20, $page);
$handle = fopen('demo.csv', 'r');
if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{
}
echo "<table border='3' bgcolor='#dceba9' style='float:center; margin:50'>";
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
while ( $row = $pagedResults->fetchPagedRow())
{
echo "<tr><td>";
$row1 = str_replace( ',', "</td><td>", $row );
echo $row1;
echo "</td></tr>";
}
fclose($handle);
echo "</table>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
<form method="get" action="slider.php">
<input type="hidden" name="totalcolumns" value="3">
<input type="submit">
</form>
This is my slider.php file.
<?php
$totalcolumns = $_GET['totalcolumns'];
echo "My next value should get printed";
echo $totalcolumns;
?>
<input type="text" data-slider="true" data-slider-range="100,500" data-slider-step="100">
</html>
As seen above, I am trying to retrieve the value with the name "totalcolumns". However, I am not able to retrieve the value in my slider.php file. I also tried using SESSION as suggested in this link, but no luck. Can someone please let me know what am doing wrong?
You should be able to use the $_SESSION. This is:
$_SESSION['totalcolumns'] = $columns --> your value here in the first script
your value will be stored in the $columns variable in the second
$columns = $_SESSION['totalcolumns']
You can also review the require or include functions. These functions make one file depend on another one, it is as if you directly paste one file on the other.
It is not a good practice to pass variables with these functions. You should use Session
http://php.net/manual/en/function.require.php
Btw, don't use framesets
You should use sessions and you should not use html framesets or iframes, it is a bad practice. If you don't want to reload the whole page by any change, you should use javascript.
You can use $_REQUEST instead of $_GET or just it as:
<?php
if(array_key_exists('totalcolumns', $_GET)) {
$totalcolumns = $_GET['totalcolumns'];
echo "My next value should get printed";
echo $totalcolumns;
?>
may this help you
i'd id the frames first, removing the src for the second one
<!DOCTYPE html>
<html>
<frameset cols="70%,*">
<frame src="index.php" id="f1">
<frame src="" id="f2">
</frameset>
</html>
then change the index.php adding this piece of code at the end
<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=3";
</script>
or perhaps if you have the totalcolumns in your php
<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=<?php echo $totalcolumns;?>";
</script>

php crumbtrail using file names

Im trying to create a crumb trail for a custom web script that drills down three levels into a link system. there are not many pages so my idea seem less work than to integrate someone else's script !
this is how i mark or identify the page in the header of each page
first level page
<?php $page = 'index_week'; ?>
second level page
<?php $page = 'index_week_list'; ?>
third level page which is also the details page and no deeper levels exist after this
<?php $page = 'index_week_ details'; ?>
My questions are...
1-how do i give each of these a label to that i can show them in the crumb trail and not show ""index_week_ details""
2-how do i link the file name to go back to a previous level with the identifier i used so that it picks up the listing id i used to filter that page's content form the database?
Below the header are and beginning of each page to see my dilemma !
the first lever page
<?php
include("inc_login_config.php");
include("inc_gen_constants.php");
include("inc_meta_header.php");
require("inc_dbsql.php");
include("inc_link_sql.php");
?>
<?php $page = 'index_week'; ?>
</head>
<body marginwidth="0" marginheight="0">
<!-- End of all general inclusuions -->
<?php
$db = new LinkSQL($dbname);
$homecataresult = $db->getchildcatalog(0);
$table="linkexcatalog";
mysql_connect ($dbhost,$dbuser,$dbpassword);
#mysql_select_db ($dbname);
$result = mysql_query("select catalogid,catalogname,parentid from $table where arentid='0' order by priority asc" );
$num_fields = mysql_num_fields($result);
$num_rows = mysql_num_rows($result);
$row_cnt = 0;
while ($num_rows>$row_cnt)
{
$record = #mysql_fetch_row($result);
?>
<?php print "$record[1]"; ?><br>
<?php $row_cnt++;} ?>
<?php mysql_close(); ?>
The other Two PHP files looks much the same except i have different LEVEL page id in them ?
I hope i don't confuse to much !
Ta any help appreciated.
Break up your string (into an array) on _'s, then loop on that array to create the breadcrumbs.
Here's a quick example:
<?php
$me = 'index_week_details';
//Break into an array
$parts = explode('_', $me);
//Debug
//print_r($parts);
//Create breadcrumbs
$path = '';
foreach($parts as $part) {
$path .= $part . '_';
echo '' . $part . '' . "\n";
}
You can play with it live here:
http://codepad.org/dFpujKZ8
Input:
index_week_details
Output:
index
week
details

execute a php page in mpdf and make a pdf

mpdf seems to convert html page quite smoothly ..
but when i use php code which gets value from database then mdpf is outputting the php code in the pdf and not the executed results..
enter code here
<body>
<form >
<?php
include 'connection.php';
$query="select * from androidlogin";
$result=mysql_query($query);
while($i = mysql_fetch_array($result))
{
echo "<h3>". $i['user']. "&&" . $i['pass']. "</h3>";
}
?>
</form>
</body>
and the output to the pdf file is like
<?php include 'connection.php'; $query="select * from androidlogin";
$result=mysql_query($query);
while($i =mysql_fetch_array($result))
{
echo "". $i['user']. "&&" . $i['pass']. "";
}
?>
i want the php page to execute and then get converted to pdf , pls help ..
Please make changes in your php.ini file.Open dll extension in Your file this will solve your problem.

Categories