PHP if div contains word include page - php

I want to use PHP only, no jquery to include a page if a div contains the word YES. The reason for this is so that I can use CMS to change the content inside a div from YES to NO to include a page or not. I have tried preg_match, but it searches either a variable or exact phrase in the PHP itself. This is what I am looking for:
<div id="available">YES</div>
<?php if (preg_match("/YES/i", ID="available")) {
include 'page.php';
} else {
echo "Not Available.";
}
?>
I need the ID="available" to search the div id="available" and if YES is found, include the page, if not, echo "Not Available." Thanks for any help.

I figured it out. This will allow you to edit your PHP variable with cushycms to include a page. If the div id="available" is anything other than Yes, the page2.php will not be included on page1.php. The code is attached below.
Page1.php
<h1>This is Page1.php </h1>
<?php
// locate page you want to check div on - page2.php
$content = file_get_contents('page2.php');
// input your specific div id from - page2.php
$first_step = explode( '<div id="available">' , $content );
$second_step = explode("</div>" , $first_step[1] );
// name your variable - in this case - $available
$available = trim($second_step[0]);
// input your output variable - $available and word to match in div - 'Yes'
if(false !== stripos($available, 'Yes')){
// if word in div=id'available' on page2.php is Yes - include page or echo
include 'page2.php';
}
?>
Note: on page2.php for this and cushycms to work, you have to put the div id="available" inside another div to have classes or styles - see example below
Page2.php
<div id="wrapper" class="cushycms">
<div id="available">Yes</div>
</div>
<h2>Please include this rental into the page</h2>

Related

how to make redirect that includes data about the link that was clicked?

I have a page with two links (text link & banner link),
that should lead to the same redirect page
(on my domain).
The redirect would be to a link that shall include a variable,
that indicates which of the two link was clicked.
e.g.:
<?php
header("Location: http://external-domain.com/?ref=[value]");
die();
?>
wheareas the "value" should be "text" / "banner" or something similar.
How can I do this?
I'm not a web programmer at all so I don't have much technical knowledge,
I guess one possible solution (which I would rather avoid) would be to give a separate id for the text link and for the banner, e.g.:
text link:
http://mydomain.com/redirect.php?id=text
banner link:
http://mydomain.com/redirect.php?id=banner
whereas redirect.php would contain:
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
But that would mean that I have to use a different id for these internal links;
I would rather find a way to use the same exact internal links (maybe for example, have each link in a "div" or a "class" and get the div/class name somehow, and have them appear as the [value].
*EDIT:
Let me emphasize again: I'm looking for a way to do this without having to use any "?id=[something]" at the end of the text link or the banner link.
Thanks!
If you're already outputting the banner/text do this for the banner
<img src="imageHere.png">
And this for the text
text here
Then catch the id get values with
<?php
$id = $_GET['id'];
if ($id == 'banner'){
echo 'The clicked link was a banner';
}
else{
echo 'The clicked link was text';
}
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
should be
<?php
$source = $_GET['id'];
header("Location: http://external-domain.com/?ref=".$source);
die();
?>
Basic concatenation.

Setting custom page titles for every pages

Currently my site title looks like:
<title>My Site Title</title>
The above code is added on 'header.php' file, so every pages has the same page title.
I need to set different titles for different pages.
for example,
<title>
if 'contact.php' then title= 'Contact Us'
else if 'faq.php' then title= 'FAQ'
else if 'add.php' then title= 'Add'
else title= 'My Site Title'
</title>
somebody please help me!!
I guess contact.php include 'header.php';. Then something like this would work:
contact.php:
<?php
$title = 'Contact Us';
include 'header.php';
// your code
?>
header.php:
<?php
echo '<title>'.$title.'</title>';
Tip: have a look at template engines. I like smarty for example. Maybe someone will comment on this with some other examples ;)
Make a variable in your script called $page and use that variable in the template file.
Business logic for page Home, for example:
<?php
.
.
.
$page = 'Home';
render($page);
View logic page for Home:
.
.
.
<title>
<?php echo $page; ?>
</title>
.
.
.
This is just a concept, it is not a fully functional code.
Split your header in to 2 seperate php files, one before the title, and one after the title (this will work with other page specific data, see note at end of answer)
then the top of your pages should look like:
<?php include_once("inc/begin-head.inc");?>
<title>My Title</title>
<meta name="description" content="description"></meta>
<?php include_once("inc/end-head.inc");?>
There are some other solutions, such as make header a class and define variations to it, and then call a function to output the head completly
Please note, there are a LOT of other paged specific tags. Title, Meta Description, Canonical url link, meta keywords, open graph data .....
You can try like this and use basename($_SERVER['PHP_SELF']) and now lookup for the $title[key]
$title = array();
$title['home.php'] = 'My home page';
$title['Hello.php'] = 'My title';
I'd advice you to use an array with titles instead of a series of ifs (respectively a switch)
<?php
$file = basename($_SERVER['PHP_SELF']);
$titles = array(
'contact.php' => 'Contact Us',
'faq.php' => 'FAQ',
'add.php' => 'Add',
);
if(array_key_exists($file, $titles){
echo '<title>'.$titles[$file].'</title>';
}else{
echo '<title>Ny Site Title</title>';
}
?>
To add title dynamically , first set the following code in header.php file :
<title>
<?php
if(isset($title) && !empty($title)) {
echo $title;
} else {
echo "Default title tag";
}
?>
</title>
and set title in each page before including header as :
$title = "Your Title";
include 'header.php';
How I did this for anyone curious in the future...
I have a "pagetitles.php" page that contains this code:
$page_files=array(
"admin"=>"Admin Panel",
"profile"=>"Your Profile",
"billing"=>"Billing / Subscriptions",
"pricing"=>"Our Pricing",
"settings"=>"Your Settings",
"bugs"=>"Bug/Feature Tracker",
"search"=>"Search Results",
"clients"=>"My Clients");
if(isset($_GET['rq'])){
if(in_array($_GET['rq'],array_keys($page_files))) {
$pagetitle = $page_files[$_GET['rq']];
}
}
Then I include that file at the very top of my index.php page, and echo $pagetitle where I want the title to be. BUT this also requires another file to handle serving the specific pages, using a ?rq request
In my "page_dir.php" file, I have the following that handles ?rq= pages (ex: www.example.com?rq=home will load the home page, with the above page title that's inside of "home" array)
Here's the page_dir.php file:
$page_files=array(
"noaccess"=>"pages/noaccess.php",
"home"=>"pages/dashboard/home.php",
"lists"=>"pages/dashboard/lists.php"
);
if(isset($_GET['rq'])){
if(in_array($_GET['rq'],array_keys($page_files))) {
include $page_files[$_GET['rq']];
}else{
include $page_files['home'];
}}else{
include $page_files['home'];
}
This page_dir.php file, I put on the index page where I want main content to show up at... I then have each individual page with just the content (like home.php file is just home.php content without the navbar and footer)
On my index.php file, where I want the page title, I have this code:
if(isset($code_nav_title)){
echo $code_nav_title;
}elseif(isset($pagetitle)){
echo $pagetitle;
}else{
echo "Default Page Title Here";
}
the $code_nav_title lets me set the page title from form submissions if I want it to say "success" or "failed" :) the "default page title here" lets you set something to automatically show up if everything fails to show (like if you forgot to set the page title)
Hopefully this makes sense! It's saved me sooo many headaches and makes it easy for expansion/changes!

in PHP, Alter the value of a variable from an included file before the value is computed

I currently have a header.php page which stores all the styles and positions of my header and layout. I then have a contactus.php page which includes the header.php page.
-----CONTACTUS.PHP:
<?php
include 'header.php'
$classnamehere='"linkStyleTwo"' //Here is where I want to update the value
?>
The main links at the top of my page are styled in a certain way using:
------HEADER.PHP:
<?php
$classnamehere= '"linkStyleOne"' ?>
<a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......
I want to alter the style of the link that the person has clicked, so that it pops out indicating to the person which page they are on. Currently, when I try altering the value of $classnamehere inside of contactus.php (by simply assigning it a new value) to change the class printed inside the <a href> tag, nothing happens. It executes the include command, and outputs the value that was stated inside header.php, ignoring my attempts to change the value on the new page.
Is there any way to change the value within contactus.php only, while still keeping the initial value inside header.php so that all the other pages can still use the 'default' style?
Edit: Inside contactus.php, can I change the value of a variable obtained (included) from header.php without actually 'changing' the global variable?
Looking at your code:
-----CONTACTUS.PHP:
<?php
include 'header.php'
$classnamehere='"linkStyleTwo"' //Here is where I want to update the value
?>
The main links at the top of my page are styled in a certain way using:
------HEADER.PHP:
<?php
$classnamehere= '"linkStyleOne"' ?>
<a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......
your code does the following:
contactus.php is executed.
header.php is included, setting the $classnamehere to
'"linkStyleOne"' and creating the actual link with classname
linkStyleOne.
After that $classnamehere is set to linkStyleTwo
This means that you have to assign classname BEFORE including the header.php.
Instead of including it in contactus.php you could do the logic within header.php:
<?php
if ($currentPage) == 'contact') {
//Set this class when user is on a specific page
$classnamehere='"linkStyleTwo"';
}
else {
$classnamehere= '"linkStyleOne"';
}
?>
and just do this in contactus.php
<?php
$currentPage = 'contact';
include 'header.php'
?>
change this
class= <?php echo '"$classnamehere"' ; ?>.
to
class= "<?php echo $classnamehere ; ?>"
Or use it this way
<?php echo '"', $classnamehere ,'"' ; ?>
Or this way(im not sure this works)
<?php echo '"{$classnamehere}"' ; ?>
If you include code, all variables within included code are global and therefore all chnages are reflected in all parts.
in header.php
$a = 10;
in contactus.php
include "header.php"
$a = 0;
And there is no way of changing $a only inside contactus. All code after $a change will be using the new value.
You can create temporary variable $a_copy inside contactus and then change this variable:
$a_copy = $a;
....class= <?php echo '"$a_copy"'...
You should create a PHP class and include it in your header, or make it your header. Invoke it on the page then assign the value for the class variable which you can then echo a separate class function which uses the variable values you require. I can provide an example if you're unfamiliar with the process.
Header.php simple example:
<?php
class Template
{
private $active_nav;
private __construct()
{
}
public function set_active($page = '') // set page test value
{
$this->active_nav = $page;
}
public function get_active() // return page test value
{
return $this->active_nav;
}
public function nav_links() // this could be changed to return an entire header or part or whatever -- change the scope accordingly
{
// active link CSS is linSktyleOne
$current_page = $this->get_active();
$nav_links = '<ul>';
$nav_links .= '<li>Index</li>';
$nav_links .= '<li>Contact</li>';
$nav_links .= '</ul>';
return $nav_links;
}
}
contact.php simple example:
<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('contact');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well
index.php simple example:
<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('index');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well
Does all that help make more sense? You only add 1 line of code to each page, and if you really wanted to get fancy you could remove the set function invocation on each page by using a substring of the$_SERVER['REQUEST_URI'] value for a conditional test and put the condition to set the value in the constructor of the class.

Dynamic pages: include specific aside for specific content

I have the following code to include pages dynamically:
<div id="content">
<div id="aside">
...
</div>
<div id="main">
<?php
$page = (isset($_GET['page'])) ? sanitize($_GET['page']) : 'home';
if (!include 'pages/'.$page.'.php') require 'pages/404.php';
?>
</div>
</div>
As you can see, the #aside has static content.
I want to include a specific content for the #aside depending on the page selected. For example, if the user goes to 'Home' and 'About', I want the 'default' aside. But if the user goes to 'Documents' I want a 'Sections' aside.
I know I can just include each aside from every page, but that's not effective. I also don't want the user to be hable to set the aside as the main content, so they have to be in different folders or something.
I'd like to know an effective and not so complicated way to do this.
Thanks for taking your time to read this.
You want to keep which sidebar goes on which page in a database, and then query that database for the correct sidebar to include.
A table structure may look like this:
Table sidebars: ID | path | name | more info on sidebar...
Table pages: ID | path | name | more info on page...
Table sidebars-to-pages: page_ID | sidebar_ID
This approach even allows you to place multiple sidebars on a specific page.
What if you did this?
<?php
ob_start();
$page = (isset($_GET['page'])) ? sanitize($_GET['page']) : 'home';
if (!include 'pages/'.$page.'.php') require 'pages/404.php';
$contents = ob_get_clean();
?>
<div id="content">
<div id="aside">
<?php include($aside); ?>
</div>
<div id="main">
<?php echo $contents; ?>
</div>
</div>
and $page.php would look like:
<?php $aside = "sidebars/default.php"; ?>
<p>HTML for #main<br />
goes here</p>
There are a few different ways to do this that are all more-or-less equal. I almost always use a config.php file for sites to hold whatever global information I want every page to have. At the top of every page, you just call
<?php
require_once('config.php');
?>
In that config.php file, you could have an array listing your page names and the file you want included for each page, as well as a function that returns the content, like so:
// this lets you call includes relative to the site root
set_include_path($_SERVER['DOCUMENT_ROOT']);
$defaultAsideContent = 'includes/default.php';
$asideContent = array(
'index.php' => 'includes/include-1.php',
'document.php' => 'includes/include-2.php'
);
function getAsideContent() {
global $asideContent;
global $defaultAsideContent;
$content = $defaultAsideContent;
// get the requested page
$pageFull = $_SERVER['REQUEST_URI'];
// strip URL variables
$pageParts = explode('?', $pageFull);
$page = $pageParts[0];
// loop throught the array and see if there is specific aside content for the page
foreach($asideContent as $key=>$value) {
if ($page == $key) {
$content = $asideContent[$key]);
}
}
include($content);
}
Lastly, wherever you want your aside content to show up, just do
<?php getAsideContent(); ?>
When you create a new page, if you want specific aside content, just edit your config file. Just FYI, didn't test this at all, probably has bugs, but you get the jist.
Thank you all for your answers and collaboration. Although none of the answers did exactly what I was looking for, they showed me other ways to approach this issue and guided me to decide what method to use.
I came up with what I think is the simpliest way to do this:
I set my folder structure as: pages/aside and pages/main
I set up an array($asides) with the aside files as the keys and the main content files as the values.
Then I check if the requested file exists in the main folder.
If it doesn't exist, I redirect the user to the 404 page. If it does exist, I loop through $asides to see which aside is asigned to that main content page.
If it doesn't belong to any of the establisged asides, then I include the default aside.
$asides = array(
'aside1' => array('page1', 'page2', 'page3', 'page4'),
'aside2' => array('page5', 'page6')
);
$page = (!empty($_GET['p'])) ? sanitize($_GET['p']) : 'page1';
if (file_exists("pages/main/{$page}.php")) {
foreach ($asides as $key => $value) {
if (in_array($page, $asides[$key])) {
$aside = $key;
break;
}
}
if (!isset($aside)) $aside = 'default';
?>
<div id="aside"><?php require "pages/aside/{$aside}.php"; ?></div>
<div id="main"><?php require "pages/main/{$page}.php"; ?></div>
<?php
} else {
header('Location: ?p=404');
}
The bounty goes to Madara Uchiha because in my opinion, his answer is simple an effective. Thanks again to all of you who helped me with this issue.

Drupal - print current URL as link, but remove the final argument

I'm trying to add a 'back' link within page.tpl.php (replacing a proper breadcrumb for a certain content type...).
My goal is to create a link that pulls in the current URL, but removes the final argument. So a page mysite/x/y would have a link mysite/x/ (or mysite/x).
Is this possible? (The URLs are aliased).
<?php
$path = ???
$my_link = l('Back', $path);
?>
<?php if (($node->type == 'marketplace_item')): ?>
<div id="breadcrumb" class="nav"><?php print $my_link; ?></div>
<?php endif; ?>
Cheers,
James
if this the case always you can build the URL manually
$my_link = $base_url . arg(0);
or count the args then remove the last one

Categories