I've used this method on a few website builds with no obvious problems. But I'm wondering if it's an okay/proper way to utilize PHP variables as website navigation/page indicators so that site-wide changes can be easily managed in 1 file (upper.php)? Are there any drawbacks? Maybe too many requests to the server, load times, improper coding or negative S.E.O. ramifications?
I've omitted surrounding code such as < body >, etc. for simplicity and indicated where more code would be with "..."
index.php
<?php $urhere="home"; include ("upper.php"); ?>
<div>This is the Home page content</div>
...
about.php
<?php $urhere="about"; include ("upper.php"); ?>
<div>This is the About page content</div>
...
contact.php
<?php $urhere="contact"; include ("upper.php"); ?>
<div>This is the Contact page content</div>
...
upper.php
...
<meta name="description" content="
<?php
if ($urhere=="home") echo "Home page description";
if ($urhere=="about") echo "About page description";
if ($urhere=="contact") echo "Contact page description";
?>
...
<title>
<?php
if ($urhere=="home") echo "Home page Title";
if ($urhere=="about") echo "About page Title";
if ($urhere=="contact") echo "Contact page Title";
?>
</title>
...
<div id="nav">
<ul>
<li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
<li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
<li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
</ul>
</div>
I see some serious maintenance problems with this approach.
Every time you add a new page, you have to add three (for now) if-statements.
upper.php knows too much about other pages -- their window titles and metadata.
I would suggest that instead of using the $urhere variable to make all the decisions in upper.php, you set variables, like window title and metadata, which can be directly used by upper.php. Here's an example:
upper.php
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
'home' => array(
'url' => '/home',
'linkText' => 'Home'
),
'about' => array(
'url' => '/about',
'linkText' => 'About',
'sublinks' => array(
'who-we-are' => array(
'url' => '/who-we-are',
'linkText' => 'Who We Are'
),
'what-we-do' => array(
'url' => '/what-we-do',
'linkText' => 'What We Do',
'sublinks' => array(
'business' => array(
'url' => '/business',
'linkText' => 'Business'
),
'technology' => array(
'url' => '/technology',
'linkText' => 'Technology'
)
)
)
)
),
'contact' => array(
'url' => '/contact',
'linkText' => 'Contact'
)
);
function getLinkElement($pageType, array $linkData)
{
$class = $urhere == $pageType ? 'urhere' : '';
$anchorElement = "$linkData[linkText]";
if (isset($linkData['sublinks']))
{
$sublinkElements = array();
foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
$sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);
$sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
}
else
$sublinksListElement = '';
return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
<ul>
<?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
</ul>
</div>
index.php
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
about.php
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
EDIT
In response to your comment, here's how you can improve your upper.php while keeping the same original structure:
upper.php
<?php
$titles = array(
'home' => 'Home page Title',
'about' => 'About page Title',
'contact' => 'Contact page Title'
);
$descriptions = array(
'home' => 'Home page description',
'about' => 'About page description',
'contact' => 'Contact page description'
);
$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>
...
<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>
...
<title><?php echo $titles[$urhere] ?></title>
...
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
There you go, if-less code.
There is no right or wrong way to build a webpage, and there are many options. In general, I like to use a templating system of some type, which makes the design of the page easy to seperate from the business logic behind it.
You could try something like Smarty (http://www.smarty.net/crash_course).
Check out some of the awesome MVC PHP Frameworks. There is a good comparison of the MVC PHP frameworks at the site below
http://jonathanmh.com/best-php-mvc-frameworks-of-2013/
Related
Here's my current code within an index.phtml viewscript:
<?= $this->paginationControl($posts,'Sliding','application/partial/paginator', ['route' => 'home','lang'=>'it']); ?>
I'd like to pass the :lang parameter within this paginationControl call so that way the router is notified and the html results show the it lang inside the pagination html ahref code for clickable links.
I'm not quite sure how to correctly do this.
Here's my route:
'home' => [
'type' => Segment::class,
'options' => [
'route' => '/:lang',
'defaults' => [
'controller' => ApplicationIndexController::class,
'action' => 'index',
'lang' => 'en'
],
],
],
The resulting html from this paginator will show:
/pp/public/it?page=2
But it currently shows
/pp/public/en?page=2
even when im on the italian version of the page
Well it depends on how you setup your paginationControl partial or viewpage script.
The paginationControl parameters:
PaginationControl::__invoke(Paginator $myPaginator, $scrollingStyle, $partial, $params);
So within your partial or viewpage script for the pagination you are able to access all the stuff you passed to the $params parameter, like you would with your parameters you pass from your controller to your view pages or the partial viewhelper.
You could pass a parameter to the partial like the route to use and its route and query parameters.
$this->paginationControl(
$posts,
'sliding',
'application/partial/pagination',
[
'route' => 'home',
'routeParams' => ['lang' => 'it'],
'queryParams' => []
]
);
So now within your pagination partial you could use the route, routeParams and queryParams - Template used - Item pagination.
<?php
if (!isset($queryParams)) {
$queryParams = [];
}
if (!isset($routeParams)) {
$routeParams = [];
}
?>
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?= $this->firstItemNumber; ?> - <?= $this->lastItemNumber; ?>
<?= $this->translate('of'); ?> <?= $this->totalItemCount; ?>
<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->first]])
); ?>">
<?= $this->translate('First'); ?>
</a> |
<?php else: ?>
<span class="disabled"><?= $this->translate('First') ?></span> |
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url(
$this->route,
$queryParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->previous]])
); ?>">
< <?= $this->translate('Previous') ?>
</a> |
<?php else: ?>
<span class="disabled">< <?= $this->translate('Previous') ?></span> |
<?php endif; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->next]])
); ?>">
<?= $this->translate('Next') ?> >
</a> |
<?php else: ?>
<span class="disabled"><?= $this->translate('Next') ?> ></span> |
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->last]])
); ?>">
<?= $this->translate('Last') ?>
</a>
<?php else: ?>
<span class="disabled"><?= $this->translate('Last') ?></span>
<?php endif; ?>
I am new to drupal.
I need to customize a registration form by adding fields like id,mobile etc.
Can I accomplish this by creating a custom module?
If yes could anyone please help me with a brief idea on creating and overriding the default user registration submit function. I have to insert these details to another table and also have to pass the data as a service request.
Ive created a custom module with function
module_form_alter(&$form,&$form_state,$form_id){
$form['#submit'] = 'module_form_submit';
if($form_id == 'user_register_form'){
//print_r($form_id);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('id'),
'#default_value' => '',
'#size' => 60,
'#maxlength' => 15,
'#required' => TRUE,
);
}
}
function module_form_submit($form, &$form_state){
echo "test";
exit();
}
module_form_alter is being called and I can see the new field on the registration screen but the submit function is still not called. I need to override the default drupal register submit.
I already have the following function in my theme template.php
function templatename_theme() {
$items = array();
$items['user_login'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'portal') . '/templates',
'template' => 'user-login',
'preprocess functions' => array(
'portal_preprocess_user_login'
),
);
$items['user_register_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'portal') . '/templates',
'template' => 'user-register-form',
'preprocess functions' => array(
'portal_preprocess_user_register_form'
),
);
$items['user_pass'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'portal') . '/templates',
'template' => 'user-pass',
'preprocess functions' => array(
'portal_preprocess_user_pass'
),
);
return $items;
}
user-register-form.tpl.php
<div class="form-group">
<?php print drupal_render_children($form); ?>
</div>
And page--user--register.tpl.php with the html
<div id="login-page">
<div class="container">
<div class="form-login" >
<h2 class="form-login-heading"><img src="<?php echo drupal_get_path('theme', 'portal') . '/images/logo.png'; ?>" width="100"><?php echo $createaccount; ?></h2>
<div class="login-wrap">
<?php
$elements = drupal_get_form("user_register_form");
$form = drupal_render($elements);
echo $form ?>
<?php if ($messages):?>
<div id="messages-console" class="clearfix">
<div class="grid_12">
<div class="mt-grid-fix">
<?php print $messages; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
Is this approach fine or should I change this approach inorder to make my custom module functional?
You can go to:
admin/config/people/accounts/fields
and add the fields that you need.
In the fieldset "User settings" don't forget check "Display on user registration form."
I'm trying to make a php site read from a text file by using this:
<html>
<head>
<title>This is a test!</title>
</head>
<body>
<?php
$f = fopen("testfile.txt", "r");
// Read line by line until end of file
while(!feof($f)) {
echo fgets($f) . "<br />";
}
fclose($f);
?>
</body>
</html>
-and it works!
Next step is to make the php site read text with links inside.
This is the text in the text file within a link, please press this link www.stackoverflow.com and more text to come after the link. Linktext like "THIS IS A LINK" is also needed.
Hope you understand what I want:)
How do I do something like this?
From reading your: "I want seperate text files for the different text sections on the site."
Have you considered using a separate php file "setting.php" and inside making a multidimensional array with all the things you need?
<?php
$settings = array(
'header_text' => array(
'text' => 'This is my header text and it containts a link click here',
'color' => '#000000',
'font-size' => '12px'
),
'footer_text' => array(
'text' => 'Copyright © Terms and Conditions',
'color' => '#000000',
'font-size' => '12px'
),
);
?>
and if you need to go a step deeper and do it PER page:
<?php
$settings = array(
'index.php' => array(
'header_text' => array(
'text' => 'This is my header text and it containts a link click here',
'color' => '#000000',
'font-size' => '12px'
),
'footer_text' => array(
'text' => 'Copyright © Terms and Conditions',
'color' => '#000000',
'font-size' => '12px'
)
),
'about.php' => array(
'header_text' => array(
'text' => 'This is my header text and it\'s only for the about page and it containts a link click here',
'color' => '#000000',
'font-size' => '12px'
),
'footer_text' => array(
'text' => 'Copyright © Terms and Conditions and go to the main index page',
'color' => '#000000',
'font-size' => '12px'
)
),
);
?>
you can then include this file in your main script
include('settings.php');
and then call upon the array and such, all you do it edit the php file, and any changes would update.
This way you can manually edit any details you want!
Example:
<?php include('settings.php'); $curr_page = basename($_SERVER['PHP_SELF']); ?>
<div style="color: <?php echo $settings['header_text']['color']?>"><?php echo $settings[$curr_page]['header_text']['text']?></div>
<div style="color: <?php echo $settings[$curr_page]['header_text']['color']?>"><?php echo $settings[$curr_page]['header_text']['text']?></div>
Thanks for your great answer - it was easy to understand.
I need to make my php site read from a text file because it is my boss who needs to change text on the site, and I don't want him to delete any of the coding.
So, is there any way to make the php site read from a text file?
This is a text 1 (must be read from document 1)
This is another text (must be read from document 2)
This is another text section on the php and THESE WORDS shuld be a hyperlink in the middle of the sentence.
etc...
Thanks again!
I want to pass a php variable to modal window , what i am doing is opening a modal window using this link , but i want to pass a variable to this link and get same variable in modal window , i try to to do this to append a text in some div but it return html that i am unable to get in query
echo CHtml::link(
'Set Recipe', '', array(
'class' => 'testclass',
'id' => $finalDate,
'data-toggle' => 'modal',
'data-target' => '#myModal',
'fahadVar' => $finalDate
));
and when i click this button i got modal window how to get variable set in button
Here is simple modal code of yiibooster
<div class="modal-body">
<p>One fine body...</p>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'type' => 'primary',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
thanks in advance
You should create a Widget.
Note: I copied below from another post. It works like charm.
First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
Then create a view for this widget. The file name is category.php. Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>
How can i generate html tags below that represent sidemenu in webpage by "for loop",or "foreach blade", or if any one can help me and generate it by" for php statement" just ?
don't worry about text between tags i want just generate html tags ?
thanks
<ul class="sidebar">
<li>tb
<ul>
<li>er
<ul>
<li>cc</li>
<li>zz</li>
<li>xx</li>
<li>mm</li>
<li>xx</li>
<li>ll</li>
</ul>
</li>
</ul>
</li>
<li>BB
<ul>
<li>AA</li>
<li>FF
<ul>
<li>DD</li>
<li>TT</li>
</ul>
</li>
</ul>
</li>
</ul>
One of the coolest and most underused features of laravel are the Collection objects.
Below is a pretty detailed answer, however I have gone over it in more detail in my clean menu management in laravel 4 blog post.
This allows you to define your menu as such:
use Illuminate\Support\Collection;
//define the top level menu
$menu = new Collection;
//each sub menu is it's own collection
$tb = new Collection;
//even sub sub menus
$er = new Collection;
//each link get's pushed onto the relevant menu
$er->push((object)['title' => 'cc', 'link' => URL::route('cc'), 'type' => 'link']);
$er->push((object)['title' => 'zz', 'link' => URL::route('zz'), 'type' => 'link']);
$er->push((object)['title' => 'xx', 'link' => URL::route('xx'), 'type' => 'link']);
$er->push((object)['title' => 'mm', 'link' => URL::route('mm'), 'type' => 'link']);
$er->push((object)['title' => 'xx', 'link' => URL::route('xx'), 'type' => 'link']);
$er->push((object)['title' => 'll', 'link' => URL::route('ll'), 'type' => 'link']);
//sub sub menu's get pushed onto the parent menu
$tb->push((object)['title' => 'er', 'menu' => $er, 'type' => 'menu']);
//sub menu's get pushed onto the main menu object
$menu->push((object)['title' => 'tb', 'menu' => $tb, 'type' => 'menu']);
//define and push the menu's in the order you want them to appear
$BB = new Collection;
$BB->push((object)['title' => 'AA', 'link' => URL::route('AA'), 'type' => 'link']);
$FF = new Collection;
$FF->push((object)['title' => 'DD', 'link' => URL::route('DD'), 'type' => 'link']);
$FF->push((object)['title' => 'TT', 'link' => URL::route('TT'), 'type' => 'link']);
$BB->push((object)['title' => 'FF', 'menu' => $FF, 'type' => 'menu']);
$menu->push((object)['title' => 'BB', 'menu' => $BB, 'type' => 'menu']);
I would suggest that the above code be put in a view composer.
This gives a great separation of your menu's logic from the actual structure, this can of course all be automated from a database or similar if your menu is not hard coded. But now onto displaying the actual menu! Mote that the below code will generate a valid menu for twitter bootstrap 3. Also there are a couple of extra options such as dividers and disabled menu entries which should give you an idea of how to extend this.
You could probably get away with dropping the 'type' => 'link' and switching it out in the view below for #if(!isset($item->type)) but I personally like to be a bit explicit.
Also note that the below code only supports 2 levels, so for your purpose would need to be extended a little bit, possibly even refactored into a recursive function, but I'm not a fan of > 2 levels of menu (and twbs3 doesn't support them anyway) so I don't do that.
<nav class="col-xs-6 col-sm-10">
<div class="row nav">
<ul class="nav nav-pills">
#foreach($menu->all() as $item)
#if($item->type === 'link')
<li class="#if(isset($item->disabled))disabled#endif">
{{ $item->title }}
</li>
#elseif($item->type === 'menu')
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
{{ $item->title }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
#foreach($item->menu->all() as $subItem)
#if($subItem->type === 'link' && isset($subItem->disabled))
<li class="disabled">
{{ $subItem->title }}
</li>
#elseif($subItem->type === 'link')
<li>
{{ $subItem->title }}
</li>
#elseif($subItem->type === 'divider')
<li class="divider"></li>
#endif
#endforeach
</ul>
</li>
#endif
#endforeach
</ul>
</div>
</nav>
<ul class="sidebar">
<?php foreach ($whatever as $whatevr){ ?>
<li><?=$whatevr?></li>
<?php } ?>
</ul>
that's the basic format. I'm not sure what you're going for exactly, the formatting of your code makes it tough to read.
you can do it using blade
<ul>
#foreach($items as $item)
<li>{{$item}}</li>
#if (count($item->children)>0))
<?php $children = $item->children?>
<ul>
#foreach($children as $child)
<li>{{$child}}</li>
#endforeach
</ul>
#endif
#endforeach
</ul>
Or, in jQuery you can achieve this without PHP with some cool effects of all sorts. Example with fade in:
DEMO
var list = $('#theList li:last-child'),
limit = 20,
current = 0;
function rand() {
return Math.random().toString(36).substr(2); // Just for some random contnt
}
$('#trigger').click(function() { // We're using a static button to start the population of the list
var end = setInterval(function() {
if ( current == limit ) {
current = 0;
clearInterval(end);
}
var elm = $('<li style="display:none;color:green;">' + rand() + '</li>');
list = elm.insertAfter(list);
elm.fadeIn();
var colorEnd = setInterval(function() {
elm.css('color', 'black');
clearInterval(colorEnd);
}, 350);
current = current + 1;
}, 300);
});
Here we establish the last <li> in our container with $('#theList li:last-child'), and then start a loop, appending after our new <li> as well as random content. We display it hidden in order to then re-establish the last <li> with $('#theList li:last-child') again in a new variable, and use .fadeIn() function for a nice effect.