hide div in a certain html page - php

apology for this newbie question.
I have created an html page (dash.html) that uses the same header as the other pages.
it calls this PHP function <?php include 'header.php'; ?>
the dash.html contains a special <div> made specially for that page; and it must be placed inside the header.php
im trying to figure out how to enable/disable a certain div on a certain html page.
will it require a PHP conditional statement?

Yes. The easiest way is to include a conditional line in the header, and pass the checked variable from each page that calls header. So, in your dash.php (it can't be a .html if it calls php, can it?):
<?php
$includediv = true; // set to false, or leave out, if you don't want the div
include('header.php');
?>
and in the header.php:
<?php
...some other code...
if($includediv){
...code to include div...
}
?>
This will continue to work as before for all other pages that call header.php.

You are trying to show the div only if header.php is present, right?
So, just set a variable inside header.php and use a conditional inside the HTML page.

Try this code
<?php
$path=explode('/',$_SERVER['REQUEST_URI']);
$page=end($path);
if($page=='dash.php')
{
?>
<div>your div for dash.php page</div>
<?php } ?>

Related

get wordpress only footer on external php page

I am working on a application where i only need to get wordpress footer
on outside php page i am using below code
<?php require('../wp-blog-header.php'); ?>
<?php wp_footer(); ?>
but it is loading NULL,when i useing below code
<?php require('../wp-blog-header.php'); ?>
<?php get_header(); ?>
<?php wp_footer(); ?>
it loads both header and footer but i want to load only footer please let me know what should i do
Thanks
Refer from this question.
You can use get_footer() function.
<?php
require __DIR__ . '/wp-blog-header.php';
// create false $wp_styles to prevent errors.
$wp_styles = new \stdClass();
$wp_styles->queue = [];
get_footer();
In case that your file is the same location with wp-blog-header.php then use the same path as shown above.
Warning! The result maybe invalid HTML as the elements are closed without open. Example </div> without matched <div>.
Alternative solutions.
Use cURL to fetch your home page and then use PHP Dom to grab only specific HTML part. Ideas: 1, 2
Use AJAX to fetch your home page and then use JS Dom to grab only specific HTML part. (See reference).
These 2 choices, I can't show the code because each WordPress theme use different HTML elements and styles.

How to change many webpages in a website?

I am making website and this website will have more than 20pages.
I am using my template to add a webpage. This template has header and footer, so I just add the body of new page.
But, what if I want to change navigation bar in the header, then I have to change all 20 pages that I already made to correct.
I want to know better way.
I read a book, and it says about "php include" function.
Should I use this function in the header and footer of each webpage to call header and footer file?
If I want to change the navigation bar in the header, all I can do is changing only one header file, then rest of website will be changed.
Is this correct way?
In this case, what do you do?
I am a beginner, so please advise me.
Thank you in advance.
You should build your website as following. The header.html would contain the navigation.
header.html
<!doctype html>
<html>
<head>
...
</head>
<body>
<nav>
...
</nav>
footer.html
<footer>
...
</footer>
</body>
</html>
page.php
<?php
require_once 'header.html';
?>
Your content goes here
<?php
require_once 'footer.html';
?>
You should use require_once so the header and footer will be imported only once per script and if the header or footer cant be found, the script will throw an exception and stop the "application".
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!
Yes this is the correct way or you could just copy your header and footer code to every .html file (if you don't like PHP)
Yes, this is the correct way. Try to think of the DRY principle - don't repeat yourself. Elements of your web page that are common across multiple pages can be coded once, then called in. If you need to update these elements, you update them once and it affects all pages.
You then "include" these elements into your page, and the elements are self contained files. As a basic example you would have header.php
<html>
<head><!-- all of your head meta tags in here--></head>
<body>
<div id="header"><!--your header elements and top menu in here --></div>
and footer.php:
<div id="footer"><!-- your footer elements in here--></div>
</body>
</html>
Don't forget in both header and footer files you can then put dynamic code if you wish. Then for each of your pages you would simply call these files in using include, include_once, require or require_once
<?php
include('header.php');
//this is where your actual page content goes
include('footer.php');
?>
A very basic example, but hopefully that makes sense to you.
Correct.
If you include another php file it will calculate/ask for input/do output (whatever you do in the file) as part of your main file.
If you include a php file that has a function you can later call this function without it showing in your main file. (Saves space in your main file)
So your 20 pages may only need a few lines of body text and the rest is header and footer. This will make changes very easy

PHP - check $get variable then clear div and include php file

Hello I am a noob in php. But I have something like this:
<div id="signupbox">
<!--A lot of stuff-->
</div>
so I have a php script just above this is check if a $get variable is activation_success, so I would echo a javascript to find #signupbox and change innerHTML, but I also want to include a file called login.php inside #signupbox.
The question is if I changed the div's innerHTML using javascript, the included .php file will also disappear.
In what way can I change the innerHTML and include a .php file inside?
I wanted to include login.php file because it is like a snippet so I can include it somewhere else and if I wanted to change, I can change the included file.
What I kinda what to do is echo('<script>$("#signupbox").html("Thank you!");</script>' . include("login.php"));
Use the load() jQuery method :
html = $('#signupbox').html(); // save the previous html
$('#signupbox').load('login.php', function(){
$(this).prepend(html); // add the previous html in the beginning
});
So, if I'm understanding correctly, you want to change the innerHTML of teh #box div but leave teh code generated by your include?
Why don't you make another, smaller div inside of #box and put only the stuff you want to disappear inside of it, then change just the inner div...
<div id="box">
<div id="innerBox">
<!--all the stuff that was in the #box, except for the include -->
</div>
<?php include "myinclude.php"; ?>
</div>
include.php is not disappearing, php is server side language..
you code is erasing existing content and rewriting.
'.html()' will erase all contents inside the target eleiment, include.php contents will be erased while executing .html() function in clientside
while adding more content in javascript,
$('#signbox').append('Thank you!');
instead of
$('#signbox').html('Thank you!');
hope this will help.

php include breaking

I have a php page that generates all the html and echo's it. Now I want to write a php script that I can use include to handle the footer code. that way if I need to update the footer on all the pages I can just edit the code in the included page.
But when I use include("footer.php"); and the footer page contains the footer code that works if it exists on the page it breaks and prints the code. Im very confused as too why?
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0)
Starts writing the code on the page if I include from 0)
Please help?
EDIT: The code in the footer is wrapped in <?php ?>
You need to wrap that code in footer.php with php tags.
<?php
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0) ...
?>
Dont your short hand notation of php tags <? ?>, use <?php ?>instead...
It sound like you didn't start php file with
<?php
Please make sure that your footer file has <?php in the beginning.
I'm not sure if you stated your question very clearly.
If you are echoing code out it will appear as soon as you include the file. Your file should look like this:
<?php require_once 'header.php';
// content goes here
require_once 'footer.php'; ?>

HTML tags question

Something basic that i don't understand:
I have header.php with navigation bar for my site. Inside it, there's a <head>...</head> section.
Now, in each other page of my site, I'm using require_once 'header.php' so that each page will show the navigation bar. But, I need also specific <head>...</head> sections to the different page.
For example, in page customers.php, I'm using <script>...</script> to include the jQuery library. I don't need to include it in other pages.
Now, searching the web I see that multiple head tags is wrong syntax.
So, how can anyone:
avoid multiple "head" tags
WHILE
separating his work to different PHP files and including them ?
You have to change your page structure and employ templates.
Instead of loading header at the top of the code, you have to do it at the bottom!
And page code should output not a word, but collect all data in variables.
And only after that output can be started by calling template.
A example layout is going to be like this:
First. page itself.
it outputs nothing but only gather required data and calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
Next, template.php which is your main site template, consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
And, finally, links.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
easy, clean and maintainable.
there are many advantages in such approach:
as requested, you can populate header with actual page-relevant data.
HTTP headers can be sent as well, before any output. It includes cookies, sessions, cache-control and many more.
it's 2011 today. AJAX era. You may wish change your code to return JSONed data instead of whole HTML page. It's going to be easy using such layout.
Imagine you're going to create very similar site with just different design. You will have to change only templates and don't touch engine files. That's really great advantage of using templates.
Here are some simple ways you can look at.
You can have jQuery on the pages
that don't need it; once it's
downloaded it will be cached so it
still wont use more bandwidth.
You can move out the closing </head>
tag from header.php and close the
<head> tag in the page that's including
header.php.
You can include javascript anywhere
on a page, not only in the header.
You can also do something like this.
Before you do require_once 'header.php'; you put a variable called $jquery = true;
In your header.php file you check if $jquery is set to true, if it is, you include jQuery.
in header.php
you can type like this
<head>
<?php echo $script; ?>
</head>
then in your customers.php
you can first assign the variable
$script = '<script>...</script>'
then
require_once 'header.php'
One possible solution.
You create a global variable before including header.php.
You test this variable in header.php.
If it is true, You print script or something. Something like this:
<!-- Fragment of header.php -->
<?php if ($i_want_jquery): ?>
<script ...>
...
</script>
<?php endif; ?>
On the other hand, a template may be a better solution.

Categories