Variable scoping across <?php ...?> blocks with HTML interspersed - php

I am very new to webscripting and technologies. So please pardon me if this is a very basic query. Further I have been researching this all day and I haven't been able to figure out why this is not working.
I am essentially trying to improve a website for my wife. The design dictates that we have a common header.php file that can be included into the various static pages that the website contains.
Now as a new requirement, they want to target a few static pages targeting different keywords, and for this the company wants to setup a different "Heading Message". So I want to dynamically pass a different "$page_heading" before I include my header.php.
My header.php has a bunch of HTML code with interspersed in between , so the following doesn't seem to be working:
In <mypage.php>:
...
<body>
<div class="container">
<?php
//global page_header;
$page_header = 'A Wildlife Resort of South India';
include('header.php');
?>
...
In <header.php>:
<header class="row-fluid">
<div class="header-main">
<img src="http://www.innthewild.com/img/itw-logo.png" alt="Inn The Wild" style="margin-top:8px;"/>
<div class="pull-right" style="height: 20px;">
<nav>
...
<div class="heading pull-right">
<?php
if(!isset($page_heading)) {
$page_heading = 'A Wilderness Retreat — Masinagudi Jungle Resort, India';
}
echo $page_heading;
?>
</div>
...
The above seems like it should work straight up, but the variable seems to be losing its scope across a new instance. How do I make this work?
Here are the example pages: http://www.innthewild.com and http://innthewild.com/resorts-places-around-bangalore.php

You're calling it $page_header in mypage.php and $page_heading in header.php.
Change one or the the other so that the variable name matches across them

u don't need to pass it
$some_var = "text";
include "some.php";
the script you just included can access the $some_var

Related

Cakephp, adding a view to a sidebar

I'm struggling trying to understand cakephp's views, blocks and layouts.
I need everypage to show a left and right sidebar which content might change. At this moment I have the right sidebar defined in /pages/home.ctp but I'm guessing it would be better to extend that sidebar since it has to appear in everypage. Correct me if that thought is wrong.
Then, I have this view add.ctp for the 'usuarios' table, it practically shows the fields login and password. I want to show this view in the sidebar, but I'm really lost as how to do that.
Thanks in advance.
Lets make this thing easy. Like #patrick said, there is a lots of way.
Start with layout file. Rearrange your default.ctp layout like-
default.ctp layout
<div id="container">
<div id="header">
<?php echo $this->element('header');?>
</div>
<div id="left-sidebar">
<?php echo $this->element('left-sidebar');?>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
</div>
<div id="right-sidebar">
<?php echo $this->element('right-sidebar');?>
</div>
<div id="footer">
<?php echo $this->element('footer');?>
</div>
</div>
Now create elements ctp files as header.ctp, left-sidebar.ctp, right-sidebar.ctp and so on and place them to app/View/Elements.
Your left-sidebar.ctp file may looks like this...
left-sidebar.ctp
// to show login form //
if you just need to show on view.ctp place few logic here for login form.
//end login form//
show other sidebar contents
There are a couple ways to do it, depending on your Cake version. If you're using >=2.1 (which I assume you are since you asked about blocks), then you should try those to see if they work for your setup. The way I usually do things is that if all views for a controller need common markup then those view files would extend a base view within the Controller directory, e.g.
#/View/Posts/index.ctp
<?php
$this->extend('_skel'); //arbitrary filename, I use '_skel' since that makes sense
echo $this->Html->para(null, 'Hello');
#/View/Posts/_skel.ctp
<?php
echo $this->Html->div('sidebar', 'Sidebar for posts...');
echo $this->fetch('content'); // This gets all output from the Posts/index.ctp view
Then all your Posts views which extend _skel will have the sidebar automatically.
Your login module might make sense as an element - something that could be used anywhere in your views.

PHP Variables URL news page

To get it out of the way, I am learning php as I go and I am new to it. I am really trying to get my hands dirty in it on this project to learn and to make my life easier!
I have a scenario, and I would love for someone to help me out through it and point me in the right direction.
My scenario is:
I have a latest news page on my 'static' website.
I am using php includes on the header, navbar and footer for every page.
I am using includes to grab the latest news information I have in assets/news/form/news1.php. This is the following:
<?php
if (!defined("ACCESS"))
{
die ("Don't waste your time trying to access this file!! Please head to the families relief home page and donate!!");
}
?>
<?php
$header = 'Article Header';
$date = 'June 1st 2013';
$url = '/news/articleName';
$body = 'Body in HTML';
?>
I then have another file in assets/news/news1.php containing:
<div id="appeal3" class="row-fluid">
<div class="well span12">
<div class="span3">
<div class="thumbnail">
<img src="/img/news-blank.jpg">
</div>
</div><!-- span3 -->
<div class="span9 appeals">
<p class="app-date"><?php echo $date?></p>
<h3><?php echo $header ?></h3>
<p><?php echo substr($body, 0, 200) ?><span class="readmore"> ...Read More ></span></p>
</div><!-- span9 -->
</div><!-- well span12 -->
</div><!-- appeal# -->
I then use the following code on my news overview page at /news/index.php to display the html with the variables entered:
<?php include("$_SERVER[DOCUMENT_ROOT]/assets/news/news1.php"); ?>
So what this gives me is an overview page, with the news article displayed around a wrapper in overview view.
I then have another page when the overview 'read more' is clicked.
Within this page, I have a full view news article page with a wrapper where I can echo the variables in.
Wha La! That is my news article structure.. Long winded isn't it!
Now, what I want!! I want to be able to include the variables from an article into my overview page and include the 2nd block of code above as the template. I then want the articles to fill out the variables. However, I have 5 articles on this page with identical variables. What is the best and most efficient way to do this??
Secondly, I have a full-view article page. How can I use this page as a template, and change the php it gets it's variables from. But, I don't want to create a new php page every time I have a new article.
What I had in mind, is something like the following:
domain.com/news/article/index.php?news1
I have been reading up on passing variables in a URL. I want to expand my knowledge on this. Could this be the solution to my problem. My issue is that I have NO idea how to go about this? Can someone please lecture me to solve this and hopefully teach me a little in between!
Thanks!

Reusable user interface in PHP?

For most of my projects I make an administration interface, which has the same design for every project. The design of the header, the footer, the topbar, the leftmenu, the css, etc. are always the same. It is a pity to create the views every time; so I was thinking: maybe there would be a nice way to put the admin interface in my MVC library, as it is reused by every project?
But for the moment, in every single view I got code like the following:
<?php $this->_include('/includes/doctype.php'); ?>
<head>
<?php $this->_include('/includes/head.php'); ?>
<title>Some title</title>
</head>
<body>
<?php $this->_include('/includes/topbar.php'); ?>
<div id="page">
<?php $this->_include('/includes/header.php'); ?>
<?php $this->_include('/includes/leftmenu.php'); ?>
<div id="content" role="main">
<h1>Some title</h1>
<p>Blah blah blah.</p>
</div><!-- /#content -->
<?php $this->_include('/includes/footer.php'); ?>
</div><!-- /#page -->
</body>
</html>
Would it be a good idea to extract the custom content from the structure of the interface, and put that structure in my library somehow to make it reusable?
After that how will it be possible to customize the title and the actual menus?
I do this all the time. I have a custom header and footer file that are called at the start and end of every page.
<?PHP
Require("includes/header.php");
...
Require("includes/footer.php");
?>
The header provides a database handle, a datetime string and handles logon, priveleges, logging of pageviews etc.
The footer provides a standard HTML page but includes some systematised variables. It also generates the menu dynamically from the driving database then closes the database connection.
This way when I write code, I don't get mixed up in the HTML and any bugs are easy to find.
I like variables akin to:
$display_scripts - adds extra data in the head section.
$display_onload_scripts - adds onload scripts to body section.
$display_style_sheets - option to include link to additional stylesheets
$display_above_menu - will appear above the menubar. NOT recommended.
$display_below_menu - will appear immediately below the menubar.
$display_one_column - page contents when only one column is to be used
$display_left_column - page contents when two columns used. Left pane.
$display_right_column - page contents when two columns used. Right pane.
$display_footer - appears in footer division.
My main code then just has to generate the appropriate variable. Fundamentally, what you need to do is examine the source of a good age you have produced then replace the stuff you want to change with variables.
Here is a schematised version of the file I use (pseudocode) to give you an idea of how I do it.
// Code here generates the menu from database
// Code here genereates popup alert messages from other users
//permanent links to external style sheets go here.
//You can also select skins here.
<?PHP
echo $display_style_sheets;
echo "<title>".$display_page_title."</title>";
?>
<script type="text/javascript" src="JAVASCRIPT GOES HERE.js"></script>
</head>
<body <?PHP echo $display_onload_scripts;?> >
<div id="page_area" >
<div id="banner">
</div>
<?php
echo $display_above_menu;
if(!$hide_menu){echo $display_menu;} //Insert the menu variable here.
echo $display_below_menu;
?>
<div id="content_area">
<div id="inner_content">
<?PHP
if($display_number_of_columns==1)
{
echo "<div id='onecolumn'>".$display_one_column."</div>"; //I only use this one
}
if($display_number_of_columns==2)
{
echo "<div id='leftcolumn'>".$display_left_column."</div>"; //these are left for legacy support from before I got better at CSS.
echo "<div id='rightcolumn'>".$display_right_column."</div>";
}
echo "<div id='footer'>".$display_footer."</div>"; //just in case - I hardly use it.
echo $display_pop_box; //for user alert messages to other users
?>
</div>
</div>
</div>
<div id="logbox"> Automatic Logout statement</div> //this is called by JS to activate timeouts.
</body>
</html>
<?PHP
$mysqlidb->close();
?>
Sorry it's such a lot of code. The layout allows easy adaptation and makes it simple to find the offending variable if things are not going as expected. There are more elegant solutions but this works well for me and is very fast.

Dynamic data in header in a symfony project

I have a Symfony 1.4 project. As you know the Template layout is defined independently, in the apps' templates' folder and then it is universally applied to all other templates. My layout is very simple, something like this:
<div id = "header">
</div>
<div id = "content">
<?php echo $sf_content ; ?>
</div>
<div id = "footer">
</div>
$sf_content, as most symfonians would know, essentially spits out the template for whatever web page is being viewed at the moment. If I needed some specific data for my header, such as logout, logo etc, I would simply include it within my header. THis works great because it is static in nature. The challenge I am facing is how I can include data that is dynamic in nature and specific to a page within the header tag because the UI demands that I include it there.
For instance, one of my webpages requires user specific data to be loaded in a dropdown/select menu. This is dynamic and could range from 0 to 100 and is specific to each user. To create this dropdown menu is not an issue, and I already have that part done. The challenge is, how do I load it in the header, given that my data becomes part of $sf_content and that is spit out in my content div.
Is there a way for me to move a specific part of my $sf_content into the header div ?
In your actions.php:
$this->getResponse()->setSlot('someData', 'and its value');
In layout.php:
<div id="header">
<?php echo get_slot('someData'); ?>
</div>
<div id="content">
<?php echo $sf_content ; ?>
</div>
<div id="footer">
</div>
Slots work for this. They can either be set in the action as in the first answer above or you can define them in the templates themselves. This is what I've done where I have dynamic data to define for the layout.
In your example:
<div id="header">
<?php include_slot('some slot name')?>
</div>
<div id="content">
<?php echo $sf_content() ?>
</div>
<div id="footer">
</div>
In the templates you would define the following:
<?php slot('some slot name')?>
//your code goes here
<?php end_slot() ?>
When the layout is then rendered Symfony will place the code between the slot() and end_slot() into the point at which you defined by using include_slot().
For ease I created a global partial that is included in all templates that defines the various common slots used through out the application. There is more info on slots and their usage here

Get Text and post to an included file

What I'm trying to do:
I have two files: One is the header.php which is included (by php include) into my second file content.php
I am going to be use my header.php with many different 'content' files, just so you know
Now, I have a div that is going to display a title text provided by content.php and any other pages that includes the header.php: <div class="title"> </div> This div is only in the header.php.
My problem is that I do not know how to include a text into the header.php div area <div class="title"> </div> from my content.php which again includes the header.php.
Header.php (contains the <div class="title"></div>)
Content.php (needs to have the title text and be included into the header.php <div class="title"> <div> area)
My question is: How in heck can I do this with php?
And I am not very knowledgeable about php or any type of coding for that matter. I have just been winging it, now I'm rather stuck. So If anyone could possibly explain how to do this in a simplified way, I would really appreciate it.
A very basic simple system would work like this:
header.php:
<div class="title"><?php if (isset($title)) { echo $title; } ?></div>
content.php:
<?php
$title = 'This is my content page';
?>
blah blah blah html here.
<?php include('header.php'); ?>
blah blah more html her e
header.php would check for the presence of a "title" variable, and output it if need be. If there's no title varaible, you get an empty div.
The content page is responsible for setting the title variable. And when the header page is included, it'll be in the same "scope" as the content.php code, pick up the variable, and run with it.

Categories