Making an header.php [Bootstrap3] - php

I am trying to do some basic HTML and PHP.
What I would like to do is, take this template:
http://startbootstrap.com/template-overviews/scrolling-nav/
And cut the menubar out of the index.html, and make that into a seperate header.php, so I can use it on my other pages as well.
I tried just cutting the code out of it, then pasting it in the header.php and including this. However it does not show up.
Could anyone explain to me how I should do this, I read about it on W3Schools that it could be something with the CSS.
I tried copying this over, to it with no success.
(The code: http://pastebin.com/6XKNaw98)
If someone could explain me how I can make the menubar of this template into a seperate header.php file, I'd love to hear.

You only want the navbar from this template, get the source code and copy paste only the html declaration which provide this navbar into a header.php.
Next, You have to echo the html like
echo "<nav class='some-bootstrap-class'>
<ul>
<li>Some item</li>
....
</ul>
</nav>";
Finally, include it into your html with the include function like on your pastebin.
Please note that echoing (don't know how to say it) html like this in PHP is pretty ugly (for me and other web developpers)

Related

Execute the Code in a PHP Page And Echo the HTML Output

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()

Dynamic change of content in tabs

<?php --this is my index page--
header('Content-Type: text/html; charset=iso-8859-1');
include ('Header.php');
include ('Navigation.php');
include ('Content.php');
include ('Footer.php');
?>
<section class="tabs"> --this is my navigation tab--
<ul class="links1lvl">
<li class="active"><a>About</a>
<ul class="links2lvl">
<li class="active">O nás</li>
I'm creating a new web and I need help with some coding. The idea is, that I have 2 sections. First for tabs to choose desired content and second as place to display actual content.
The thing is, I've found a ton of guides but those inlude the content of all tabs on that index page, but I have 20+ pages so that's unreal. What I desire is a page, where only the actual content would change as I click different tabs, without refreshing the whole page AND wich is loaded from ,,external" html/php files. Now I don't need code, I will gladly learn as much as I can on my own, thing I need however is a direction. Where should I look for solution.
You need to use AJAX. This will allow you to trigger a call to an external file via Javascript, and display the result onto the current page without the need to refresh.

Making a header.php file

I want to make a header file, but am not experienced with PHP in almost any aspect. My uncle was telling me that I can use a header file with PHP, and that it was like a CSS with HTML.
The following is the HTML I want in my PHP:
<center><nav>
<ul>
<li>Home</li>
<li>Arcade
<ul>
<li>Action</li>
<li>Arcade</li>
<li>Puzzle</li>
<li>Vehicle</li>
<li>Violence</li>
<li>Defense</li>
<li>RPG</li>
</ul>
</li>
<li>Watch
<ul>
<li>TV Shows</li>
<li>Movies</li>
</ul>
</li>
<li>Extras
<ul>
<li>News</li>
<li>Updates</li>
</ul>
</li>
<li>Support</li>
</ul>
</nav></center>
How could I incorperate that into a header file?
I have looked at other websites such as W3Schools, but they don't seem to work. I think my main issue is that I am not sure how to put the HTML into the PHP document. When I do it, I try to type out the HTML as if it were header.html, only, ya know, it's not.
But anyways, if anybody could even just point me in the right direction of how to incorperate that HTML I included into a header.php file, and what to put in the html.
From what I have seen, the best scenario for including the PHP document in the HTML code is:
<html>
<?php
header('Location: http://www.example.com/');
?>
Right now, I'm putting that HTML in every single page. This is restricting me from making navigational tab changes!
My website is http://www.gameshank.com/
Again, thanks in advanced, sooo much!
Ah... the PHP header command is used to output a HTTP header, so I'm really not sure what you're hoping to achieve via its use.
What you want to do is save your generic header HTML/PHP code into a new PHP file (perhaps called "header.php") and then include the contents of that file within each of your existing PHP pages via an include statement. For example:
<html>
<head><title>Sample HTML page</title></head>
<body>
<?php include 'header.php'; ?>
<h1>Page specific content</h1>
<p>And stuff.</p>
</body>
</html>
By doing this, each page will automatically contain the contents of the header.php file (which will appear wherever you place the include statement) and any changes you require can simply be made to the header.php file.
You definitely can!
On your main page (Lets say index.php), on the top you can put in
<html>
<?php
include 'header.php';
?>
</html>
(Or whatever your header file is called.).
Put that HTML in header.php you want to include.
You have to use include like this
<?php
include('header.php');
?>
or use require
<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>
While you're at this, it's also a pretty good idea split the footer off of your page and place it in a seperate PHP file like you're doing with your header.
The point of this is it allows you to make changes to code that is featured in multiple pages throughout your site without having to go through every single page and make the same change.
For instance if you have "Copyright (c) 2013 Blah Blah Blah" at the bottom of 50 pages on your site, when 2014 rolls around you don't want to edit 50 pages to change 2013 to 2014. You'd just make it once in "footer.php".
And like the others have said, simply include the file in your pages.
<?php
include 'footer.php';
?>

Make a self-aware menubar

So, In the website I'm currently designing (HTML5, PHP, JS/JQuery and Bootstrap), I've got a basic menubar at the top of the page. Just your normal
<ul>
<li>Home</li>
<li class="active">About</li>
<li>Players</li>
<li>Rules</li>
</ul>
Now, there's a lot more to this, such as a login button, etc, but basically it's adding a LOT of clutter to the top of my pages, and I was wondering if there would be any way to put it in a header.php file.
My issue is how I can use it in multiple webpages and still have the class="active" part. The only thing I thought of was making a function where it takes the page name as a string and go through each line and does if (the page is the same as the link) { echo the element with the class="active" } else { echo the element without the class }
Thanks!
You probably want to extract your header to header.php as you said, and then use the PHP include method.
<?php
include 'header.php';
?>
As far as selecting the 'active' class, you could pass and set an '$active' variable on each page. And then, since the included file inherits the scope from the page where it's included, you can get the variable and preform your logic in the header.php page.
http://php.net/manual/en/function.include.php
You can include another php file located anywhere on your server, and have the menu html in there, in a function as you said.
As for keeping the class="active", since you already have it working somewhere, presumably in your main php file, you can just move it to the new header php file that will be included and re-factor if necessary.
I don't know what your code looks like at all, if you post part of it that does the menu, me or someone else will probably be able to help more.

Pagination Not Displaying

I am a front-end designer/code trying to style up pagination for a web app. It is called in one section of the application and called it with the following:
<?php echo $pagination;?>
And I used CSS + the Pagination JS file to style it up.
In another section of the application, which I have just been asked to style, it is called with this:
<?php echo $pages;?>
And looks entirely unstyled. When I replace $pages with $pagination the page control no longer appears. When I run a search for $pages through our repository I see no matches for $pages (not even in the pagination file).
Anyone have any idea why pagination breaks the control?
Thanks!
EDIT: I did a really bad job of explaining this. The problem is that I can find a reference to the variable $paginate in the repository (in paginate.js) and I was able to assign a CSS class to it through that JavaScript file, but I can't find any references to $pages, which I just can't wrap my head around. If I change the variable to something else that I think is defined (such as $paginate) it no longer displays the pagination.
EDIT: Figured it out, it was in the controller in CodeIgniter.
Well, I'm not quite sure if I my answer will help, but as far as I undertood your question
When $pages is called, in the source don't you see the HTML tags that those pages are? Why don't you try not assign smth in javascript but make styles in your CSS according to those html tags that are generated?
for ex.
<div class="smth">
<div class="your_$page_div">
<ul>
<li>text</li>
etc...
In your CSS do smth like div.smth div.your_$page_div ul li {some stile}
so you won't have to replace anything and assign smth in core file.
I hope I correctly understood what you were asking :P Cheers!
I just saw you figured out the problem

Categories