I'am a newbie also my language maybe bad, and looking for solution for my learning php code
let say i have many page i.e. etc page1.php ... page1001.php each page maybe:
inside of page1.php:
$color = "red";
$pages = "two";
$theme = "ocean";
$lang = "eng";
$charset = "ISO-8859-1";
etc (more..)
inside of page2.php :
$color = "blue";
$pages = "two";
$theme = "ocean";
$lang = "it";
$charset = "UTF-8";
etc (more..)
now i need to put the variable of each pages to one page, n just put a simple code in each pages to setting them so next time easily to edit, note I using plain text (flat file)
anybody help me? i give appreciate and say thank you
You can try to use "include"
or you could use:
require('page1.php');
This will work similarly, but cause an error if the page cannot be located.
see: here
oh sorry maybe i give wrong explained:
I mean all content in page1.php up to page1001.php above i will move to one page call it as parameter.php
so parameter.php become:
<?
//the value of page1.php
$color = "red";
$pages = "two";
$theme = "ocean";
$lang = "eng";
$charset = "ISO-8859-1";
etc (more..)
//the value of page2.php :
$color = "blue";
$pages = "two";
$theme = "ocean";
$lang = "it";
$charset = "UTF-8";
//etc (more..)
//the value of page3.php :
$bla-bla = "bla-bla";
?>
now how to call the value of page1.php also page2.php in parameter.php above? if i using include "parameter.php"; in each page (page1.php up to page1001.php) sometimes not suitable to each of page...
Judging by your data, you could put your values for a certain page into an associative array such as this:
//Page1.php
$page1Information('color' => 'red', 'pages' => 'two', 'theme = 'ocean');
//Page2.php
$page2Information('color' => 'blue', 'pages' => 'five', 'theme = 'forest');
Then you can include all the pages in your parameter.php file, and call upon the data in any one file by going:
echo $page1Information['color'];
//Prints out "red"
Related
I realy tried to find solution but maby i ask wrong question. I have website in php without cms and my goal is to have url specific Meta Title and Description in Google for URLs on this page.
Preferable all meta data would be in file like metadata.php, for now i have code in header.php, and it works partial
I have something like this for now and it always displays text3 on every page:
if($_SERVER['SCRIPT_NAME']=='index.php'){
$pm_title = "tex1";
$pm_desc = "";
}elseif($_SERVER['SCRIPT_NAME']=='contact.php'){
$pm_title = "text2";
$pm_desc = "";
}else{
$pm_title = "text3";
$pm_desc = "";
}
$_SERVER['SCRIPT_NAME'] contains the whole path of your file, not just the filename. So if your file is located in /var/www/site/index.php, $_SERVER['SCRIPT_NAME'] will contain everything above, not just index.php.
So, in order to achieve what you're trying you can use pathinfo to get the filename, then switch among the results.
Here's a quick code
$filename = pathinfo($_SERVER['SCRIPT_NAME'])['basename'];
// Filename now contains only the file.php part
switch ($filename) {
case 'index.php':
$pm_title = "tex1";
$pm_desc = "";
break;
case 'contact.php':
$pm_title = "tex2";
$pm_desc = "";
break;
default:
$pm_title = "tex3";
$pm_desc = "";
}
basically I have a navigation menu, currently if I go to a page on the site it adds 'current' class to the navigation menu item so I can change it's style. Like so:
jQuery(document).ready(function($){
var path = window.location;
$('#nav a[href="'+path+'"]').addClass('current');
});
I want to extend this to include any page under it. I have come across a few posts explaining how you do it but non seem to work for me. The URLs are quite long, and the site heavily relays on parameters in the url so a type url might be example.com/path1/path2/?id=9238293&name=test.
Not sure if jquery is the best way to do this? Open to doing it in PHP also if possible.
YOu can have this in PHP:
<?php
//$url = 'http://example.com/path1/path2/?id=9238293&name=test';
$url = $_POST['path'];
//REMOVE THE LAST PARAMETER IN URL ?id=9238293&name=test';
$paths = explode('?',$url);
$paths = count($paths) ? $paths[0] : $paths; //if have parameter or not
$paths = explode('/', $paths); //SEPARATE THE URL
if (!$paths[count($paths)-1]){
unset($paths[count($paths)-1]); //VERIFY IF THE URL ENDS WITH '/'
}
$count = count($paths);
//THEN YOU HAVE
//YOUR CURRENT PAGE
//print "Current page: ".$paths[$count-1];
//print "Mother page: ".$paths[$count-2];
//THEN YOU CAN COMPARE THE MOTHER PAGE WITH ONE EXISTING MENU PAGE
//IN YOUR EXAMPLE YOU CAN GO:
$return = array();
$site = "http://example.com/";
$current = $paths[$count-1];
$mother = $paths[$count-2];
$return['current'] = $site.$mother.'/'.$current; //THAT WAY YOU STRIP THE PARAMETERS TO GET THIS ON YOUR JAVASCRIPT
$return['mother'] = $site.$mother;
print json_encode($return);exit;
?>
Then in your client-side do:
<script>
jQuery(document).ready(function($){
var path = window.location;
//very important!
path = path.toString();
$.post('YOUR PHP SCRIPT URL',{path:path},function(data){
$('#nav a[href="'+data.current+'"]').addClass('current');
$('#nav a[href="'+data.mother+'"]').addClass('current');
},'json');
});
</script>
I am trying to pull area of a page with AJAX.
In JS I have on click I pass href to PHP;
in PHP(located in tools):
<?php defined('C5_EXECUTE') or die("Access Denied.");
$path = ($_POST['path']);
$page = Page::getByPath($path);
$a = new Area('Main');
$ret = $a->display($page);
echo json_encode($ret);
?>
If I make:
echo json_encode($page);
I receive the page so everything working, But when I try to receive an Area I get this error:
concrete\elements\block_area_header_view.php on line 5
In this File I found this
$c = Page::getCurrentPage();
$areaStyle = $c->getAreaCustomStyleRule($a);
So as I understand $c is null that why I have this error how can I fix this??
This line of code:
$ret = $a->display($page);
...does not do what you think it does. The "display" function does not return the content -- instead it outputs it to the browser. So your json_encode($ret) is just encoding and echo'ing an empty variable.
To capture the displayed content and put it into a variable, you can use php's output buffering feature, like so:
ob_start();
$a->display($page);
$ret = ob_end_clean();
I'm assigning a value to smarty template from PHP file. My code snippet from PHP file is as follows :
<?php
require_once("includes/public-application-header.php");
ob_start();
prepare_request();
$request = empty( $_GET ) ? $_POST : $_GET ;
$op = $request['op'];
$objTeacherDetails = new TeacherDetails();
$teacher_id = $_GET['teacher_id'];
$teacher_classes = $objTeacherDetails->GetAllClassesByTeacherId($teacher_id);
$smarty->assign('teacher_classes', $teacher_classes);
$file_to_show = "teacher-details.tpl";
switch( $op ) {
case "get_assigned_subject_list":
$objClassSubjects = new ClassSubjects();
$objSubjects = new Subjects();
$class_id = $request['class_id'];
$all_subjects = $objSubjects->GetAllSubjects();
$subject_details = $objClassSubjects-> GetClassSubjectDetailsById($class_id);
$smarty->assign('all_subjects', $all_subjects);
$smarty->assign('subject_details', $subject_details);
$smarty->assign('teacher_id', $teacher_id);
$smarty->display("assigned-subject-list.tpl");
die();
break;
?>
My issue is I can't access the value of $teacher_id in smarty template. My code of smarty template is as follows:
<input type="text" value="{$teacher_id}" name="tid">
But I'm getting the text field blank. Can anyone help me out to resolve this minor issue? Thanks in Advance
Have you assigned $smarty somewhere?
$smarty = new Smarty();
Edit:
The problem was the $_GET['teacher_id'] was not being passed from one page call to the next.
I was wondering how you guys set your page titles while you use global headers. I would like to be able to change my page title from page to page... for example, "Site Name : News Archives". Would the best way be to use JavaScript? If I did this with JS, would the new changes take effect in search engine results? Just wanted to get some input on this thought.
<?php
include('header.php');
switch($_GET['p']){
case "news":
include('news.php');
break;
default:
include('indexBody.php');
}
include('footer.php');
?>
You can define a variable BEFORE including the header.
This variable can be used then in header.php.
<?php
$pagetitle = "Site Name : News Archives";
include('header.php');
...
?>
If your looking to go down this road, perhaps try something like the following:
Store your variables in a data array and then pass them to your header/footer/content views before echo'ing out.
$data['title'] will be seen by your header and footer ect as $title
<?php
$page = (!empty($_GET['p'])?$_GET['p']:'index');
$data = array();
switch($page){
case "news":
$view = 'news.php';
$data['title'] = 'Site Name : News Archives';
break;
default:
$view = 'indexBody.php';
$data['title'] = 'Site Name : Home';
break;
}
echo load_view('header.php',$data);
echo load_view($view,$data);
echo load_view('footer.php',$data);
function load_view($path,$data) {
if (file_exists($path) === false){
return 'View not found: '.$path;
}
extract($data);
ob_start();
require($path);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
?>