Implement "pagename.html#id_of_section" similar functionality in php - php

I am currently working on a php project in which i needed to implement a page redirection.In HTML the page redirection
<a href=abc.html#section>Pagename<a/>
works proper to get me to any section of the page. But in php i tried to implement the same with tag and include method which gives me error.the code i tried is
if(?tag==tagname)
include('pagename.php#section');
How can i redirect visitor to some other page on some section which is in middle of the page?? implementing abc.html#section in php. Please give your guidance. Thanx in advance.
The structure is
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<php>
if(?tag==home)
include(home.php);
</end php>
<footer>
</end body>
</end html>
This works fine but it will always open new page from the top. I needed to take users to home.php#section. Please help me out.

use $_GET to access query-paramters.
e.g.:
index.php?foo=bar&some=thing
$_GET['foo']; //contains bar
$_GET['some']; // contains thing
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<?php
if(isset($_GET) && $_GET['tag'] && $_GET['tag'] == 'home') {
include(home.php);
}
?>
<footer>
</end body>
</end html>

Related

How to add meta tag's values for seo using PHP?

I am using one header file for every page which will show the HTML head(which includes meta tags and other CSS links)
Here I have used everything as dynamic as if I like in the case of canonical tags I have used $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_HOST'] and all other links to CSS are also accessible even if it can be any file from any directory.
Now I want to create a one-time title meta tag & description tags like this will be my main file looks like
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_content; ?>" />
//Other meta tags & linking to css & js files
</head>
<body>
// Content ........
</body>
</html>
Now here is the twist is that many people will say that just use
$page_title = "Here is the title of the specific page";
$page_content = "Here goes the long-form description describing the page of specific page";
Yes, this would have worked out if the pages were different and had the same place to put that all before including the header file.
But my index page looks like this let me explain it too. (Using Bulma as a framework)
<?php
require 'include/db_connect.php';
require 'include/header.php';
?>
<form name="submitform" method="POST">
<div class="columns is-multiline" id="wrapper">
<div class="column is-6-desktop is-12-tablet" id="main_content">
<div class="box">
<?php
if($country)
{
?>
<?php require 'inc/country.php'; ?>
<?php
}
else if($country && $state)
{
?>
<?php require 'inc/country_state.php' ?>
<?php
}
else if($country && $state && $district)
{
?>
<?php require 'inc/country_state_district.php'; ?>
<?php
}
else
{
?>
<?php require 'inc/other_than.php'; ?>
<?php
}
?>
</div>
</div>
</div>
</form>
<?php require 'footer.php'; ?>
</body>
</html>
The main point here is that I am using the dropdown button which gets auto-submitted using js that's not relevant here but from the top, I have just explained what is the structure of my code.
Now as you can see the if-else structure which includes other files that create dynamic pages but their code starts only from the body and not from the head directly so I am not able to add those title tags and descriptions.
Now how to add title & description tags uniquely to each of these pages.
Any solutions, please... Thanks in Advance
Like already written in the comments. You should define the vars ahead of require 'include/header.php'; or use an MVC structure.
If you still want to stay with the existing code ob_get_contents() might help you.
Use ob_start(); at the beginning to tell PHP to not print any output and instead write the output to a buffer.
You can then at a later point use ob_get_contents(); to fetch the output buffer and print it.
You have to search in the output buffer for an identifier like %page_title% then and replace it with the actual value before sending the output.
This may look like following:
echo str_replace(%page_title%, $page_title, ob_get_contents());
Still I'd rather suggest restructuring your code, as the solution with output buffer is slower, uses more memory and is poor to debug.

In php how to call or link another php page?

I am a beginner for this language and I don't know how to call another page or link
in other page please help how?
You can combine PHP and HTML together.
If you need a hyperlink in your page, you can either echo the html tags or embed the PHP code inside the HTML
Below is an example how I embedded the PHP inside the HTML tags
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="width:1250px;">
<div>
<ul>
<li>Home</li>
<?php if (isset($_SESSION['user'])) { ?>
<li>
<li class="dropdown">
Account
<div class="dropdown-content">
Profile
My Articles
</div>
</li>
</li>
<li style="float:right">Logout</li>';
<?php } else { ?>
<li style="float:right">Register</li>
<li style="float:right">Login</li>';
<?php } ?>
</ul>
</div>
</body>
And below is the example of how to use html tags inside the PHP code:
<?php
echo "<html>";
echo "<title>HTML with PHP</title>";
echo "<b>My Example</b>";
print "<i>Print works too!</i>";
?>
For page redirects, you can use the below code in your PHP :
header('location: targetfile.php');
The PHP code located after the header() will be interpreted by the server, even if the visitor moves to the address specified in the redirection. In most cases, this means that you need a method to follow the header() function of the exit() function in order to decrease the load of the server:
<?php header('Location: /directory/mypage.php'); ?>
A basic link is created by wrapping the text (or other content, see Block level links) you want to turn into a link inside an element, and giving it an href attribute (also known as a Hypertext Reference , or target) that will contain the web address you want the link to point to

random unwanted '1' appearing in html

I'm getting random 1's throughout my html page in chrome.
Can't seem to find this issue on forums or SO.
This is an image of how the page is being rendered:
My php file looks like this:
<!DOCTYPE html>
<html lang="en">
<?= include('head.php') ?>
<body>
<div id="wrapper">
<?= include('nav.php') ?>
<div id="page-wrapper">
I have no clue what's causing this, can anybody push me in the right direction?
From my comments:
Simple: it's the ='s - <?= is short hand for "echo" so remove those.
and change your <?= to <?php since short tags are not enabled on your system.
The 1's are boolean which tell your includes did work/that the file(s) exist.
If it would have failed, then those would have appeared as 0's.

Sidemenu In codeigniter

I just try to bulid control panel using MVC (codeigniter), I just need way to how make a side menu in home page and when I click in the link in menu the page open in the right side, and the menu stay in the left side.
I need coorect way to do this using MVC.
I use this simple way to make Views to make exactly what your asking.
<?php
//This is mainpage.php
$data['page_id'] = $_GET['page_id'];
?>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<div>
<!-- HERE Comes Side Bar -->
<?php $this->load->view('sidebar'); ?>
</div>
<div>
<!-- HERE Comes Main Content -->
<?php $this->load->view('maincontent',$data) ?>
</div>
</body>
</html>
Mainpage.php is a master page who holds code for both, side menu as well as the content page
This code if for side bar sidebar.php in view folder
<div>
<ul>
<li>Dashboard</li>
<li>Settings</li>
<li>Logout</li>
</ul>
</div>
sidebar.php page only holds different links, but all links have GET variable named "page_id" which decides which page is to be displayed in the content page.
Now, in mainpage.php you can notice that maincontent.php is loaded as view with passing $data which has page_id as variable, which is drived from sidebar. this will help to display the content in content side.
This code is for maincontent.php in view folder
<?php
if(file_exists(APPPATH.'views/'.$page_id.'.php')){
$this->load->view($page_id);
}else{
show_404();
}
this has always worked for me in non ajax page displays...so will work for you to.
Thanks...

My first php not working

Trying to load my first php site in a browser; Firefox displays odd coding tags, IE asks if I want to "open" the file, instead of loading the page. I think I copied a tutorial 100%, so I suppose it really should work.. Is there anything else I need to do to make this work, or may I have some code error?
functions.php
<?php
function displayContent($content) {
echo implode($content);
}
function displaySidebar($sidebar) {
echo implode($sidebar);
}
?>
template.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<meta name="author" content="SS" />
<link href="site.css" rel="stylesheet"type="text/css" />
</head>
<body>
<div class="container">
<div class="header">
header
</div>
<div class="sidebar">
<?php displaySidebar($sidebar); ?>
</div>
<div class="content">
<?php displayContent($content); ?>
</div>
<div class="footer">
footer
</div>
</div>
</body>
index.php
<?php
//include our functions
require_once ('functions.php');
// declare arrays
$content=array();
$sidebar=array();
//handle page request
$t=$_GET['t'];
if($t == 'about') {
//display about page //can we load a full template?
$content[]="<h2>about</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
} else {
//display the home page
$content[]="<h2>home page</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
}
//Important this is on bottom
//I guess content must be ready on load.
include ('template.php');
?>
OUTPUT: (Firefox)
about"; $content[]="
Content
"; $sidebar[]=""; } else { //display the home page $content[]="
home page
"; $content[]="
Content
"; $sidebar[]=""; } //Important this is on bottom //I guess content must be ready on load. include ('template.php'); ?>
Uh, have you already installed Apache and PHP, and have you put the files in Apache's web root? Because PHP is not HTML: it's not parsed by the browser. It's executed by the server, which then returns the HTML response to the browser.
If you are working locally, try installing wamp or xampp, if you're working on a host. I suggest looking for one that supports php and mysql.
It could be that your server doesn't have PHP set up, or maybe your files aren't ending in .php.
To check, I'd remove what you've done so far, and make a new file called index.php. In it, I'd just write:
<?php
echo "Hi!";
?>
Then go to the page. If that works, then you know your script has some issue, if not, then you know the server isn't set up correctly.
If that does work, you may want to change echo "Hi!"; to say phpinfo();. That tells you all about the PHP install, which may come in handy to know at some point!

Categories