I am currently working on a project and I would like to print different HTML pages depending on the selection of the user.
Since all pages share the same sidebar and navigation bar I thought I could export them into a different file and always read/show them while also reading the different subsites and showing them.
Here is a little example of the code:
$decision = (isset($_GET["site"])?$_GET["site"]:"default");
switch ($decision) {
case "login":
readfile("login.html");
break;
case "register":
if($isAdmin){
readfile("register.html");
}
break;
default: // Prints the dashboard by default
/* #region To be removed (exists for testing only) */
//readfile("register.html");
/* #endregion */
readfile("dashboard.html");
break;
And since I don't know how I would succeed in showing to pages at the same time which are interconnected I am asking you and I was also wondering if I could send the page information by post and not only by get.
Thanks in advance!
The solution to my problem was to split my content into several different files and just arrange it in the correct order (suggested by #Adyson)
include_once("header.html");
switch ($decision) {
case default:
include_once("login.html");
break;
}
include_once("footer.html");
Related
Hii everyone i am new here, and i am also new in coding world too 😁
But i what i have learned so far from php by myself it feels nice using php.
But i have a queation
I have a file name (home.php)
I used
$action = $_GET("action")
In this page and i have multiple actions available fr the page it goes like
home.php?action=main
home.php?action=new
But when someone tries puting new action there that i dont have in the file
Like
home.php?action=boom
Page comes blank
Any one give me any idea to set a action that will come when an actio not found in the file
Thank you very much
I know ifs a lot to ask
But its stack overflow 😍
A very simple solution can be :
$action = $_GET["action"];
switch ($action) {
case "main":
// do your main stuff here
break;
case "new":
// do your new stuff here
break;
default:
// the action is unknown, do what you have to do in that case here
}
But that makes you add each new action manually, probably not the best way to do it.
Still, it is a simple way to answer your question.
You can also try this :
$action = $_GET("action");
if($action=='main'){
// do your main stuff here
}else if($action=='new'){
// do your main stuff here
}else{
// do your main stuff here
}
if action does not match in any condition then by default it will go on else case.
Sorry for a noob question. I have my php script organized like this:
switch(true)
{
case (isset($_POST['login'])):
// ...some code to check a login
break;
case (isset($_POST['register'])):
// ... some script to register a new user
etc.
Access to each section of code is controlled by using the name of the submit buttons in forms. I borrowed a menu system that uses URL's with command parameters instead of submit buttons. An example URL looks like this:
http://MYIPADDRESS/gradebook/login.php?guid=51913a6e37e1b&command=newclass
So, to get the same control of the program flow using URL's I tried adding this at the beginning of the file:
if (isset($_GET['command']))
{
switch ($_GET['command'])
{
case 'newclass':
$_POST['basicsettings']='basicsettings';
$guid=$_GET['guid'];
break;
case 'logout':
$_POST['logout']='logout';
break;
case 'continue':
$guid=$_GET['guid'];
$_POST['continue']='continue';
break;
}
}
This doesn't work because the $_GET global array is only reset if the next operation issues a new URL, and in most cases the user would use a submit button on a form and not select another menu item. You can't use unset on $_GET within a php script because it only affects the local copy. I tried setting up a $_SESSION command variable instead and refreshing the page using header("Location:login.php") to reset the $_GET global array but this also didn't work. Here is the modified code:
session_start();
if (isset($_GET['command'])) // if program flow control came from a URL
{
switch ($_GET['command']) // check which script section to use
{
case 'newclass':
$_POST['basicsettings']='basicsettings';
$guid=$_GET['guid'];
$_SESSION['command']='basicsettings';
$_SESSION['guid']=$guid;
header("Location:login.php");
break;
case 'logout':
$_POST['logout']='logout';
$_SESSION['command']='logout';
header("Location:login.php");
break;
case 'continueteacher':
$guid=$_GET['guid'];
$_POST['continueteacher']='continueteacher';
$_SESSION['command']='continueteacher';
$_SESSION['guid']=$guid;
header("Location:login.php");
break;
}
}
if (isset($_SESSION['command']))
{
$var=$_SESSION['command'];
$_POST[$var]=$_SESSION['command'];
$guid=$_SESSION['guid'];
$_REQUEST['guid']=$_SESSION['guid'];
unset($_SESSION['command']);
}
switch(true)
{ .. ...etc.
I realize that one way to fix this is to change the menu system to use submit buttons instead of URL's, but that has some downsides as well. Is there any way to make this work? Assuming you can see what I'm trying to do and it makes sense?
I wanted some suggestions from someone with experience in php.
I am making a website in php which will have 4 kinds of users :
1. guest(unregistered),
2. registered,
3. registered with special privilages,
4. admins
So the same page will be visible differently to all four of them.
Right now I am doing that by using if conditions.
In every page, I am checking the role of the user and then using many if statements to display the page accordingly.
It makes the code very big and untidy and I have to check conditions again and again in all the pages.
Is there a better way to do this?
How is this done in big professional websites?
Extended Question:
What is the most optimal way to do the same using a MVC framework like kohana 3.1? Does it have anything to do with acl?
It really depends on what you need.
For example if the page has big part that change completely, what I would suggest is to create different templates and include them depending on their "permissions"
$permission = $_SESSION['type_user'];
include '/path/to/file/with/permission/'.$permission.'/tpl.html';
and have something in the page similar to
<?php
//inside include.php you have the line similar to
//$permission = isset($_SESSION['type_user']) && $_SESSION['type_user']!=''?$_SESSION['type_user']:'common';
require_once '/mast/config/include.php';
include '/path/to/file/with/permission/common/header.html';
include '/path/to/file/with/permission/'.$permission.'/tpl_1.html';
include '/path/to/file/with/permission/common/tpl_2.html';
include '/path/to/file/with/permission/'.$permission.'/tpl_3.html';
include '/path/to/file/with/permission/common/footer.html';
?>
if the script is full of small parts like "show this text", or "show this button", you can create a function that will check the permissions for you
<?php
function can_user($action, $what){
switch($action){
case 'write':
return $your_current_if_on_what;
break;
case 'read':
default:
return $your_current_if_on_what;
break;
}
}
?>
and the template will look like:
[my html]
<?=can_user('read','button')?'My Button':''?>
[my html]
As a rule of thumb, if a piece of code is used more than 2 times, it needs to be put in a function/file separately, so if you have many "IFS" you need to create a function
The code below ensures that when a user accesses control panel, they are ran through a quick verification process to validate what their entities are. For instance, if a user is level 1 they are only given access to video feed which means nothing else is available to them.
When I look at the code though, I can see video feed being called when case 1 and 3 are called. I would possibly enjoy an alternative to make the code more efficient.
I was told a possible array could make things a little easier but then again this is faster.
switch ($_SESSION['permission']) {
case 1: // Level 1: Video Feed
include ("include/panels/videofeed.index.php");
break;
case 2: // Level 2: Announcements / Courses / Teachers
include ("include/panels/announcements.index.php");
include ("include/panels/courses.index.php");
include ("include/panels/teachers.index.php");
break;
case 3: // Level 3: Announcements / Video Feed / Courses / Teachers / Accounts / Logs
include ("include/panels/announcements.index.php");
include ("include/panels/videofeed.index.php");
include ("include/panels/courses.index.php");
include ("include/panels/teachers.index.php");
include ("include/panels/accounts.index.php");
include ("include/panels/log.index.php");
break;
case 4: // Level 4: Teachers
include ("include/panels/teachers.index.php");
}
It's fine the way it is. I think you don't mean "efficiency" when you refer to the "repeated" includes. You mean you could compact your code by using the switch fall-through.
While this might make your code smaller, it has no significant impact of efficiency (the time the script takes to run) and it will actually make the code harder to read. Leave it be.
Frist you may run better if you use require_once if its possible.
The second point is to shorten the url it seems that its every include the same.
Maybe try to use it in a function for example:
$permission_id = $_SESSION['permission']
function requireModule($moduleName, $path = 'include/panels/') {
$moduleName .= '.index.php';
require_once($path . $moduleName);
}
// if you want you can add an arry for it:
$permissionModules = array(
array('videofeed'),
array('announcements', 'courses', 'teachers'),
array('announcements', 'courses', 'teachers', 'accounts', 'log', 'videofeed'),
array('teachers')
);
// now we can put it in a more effectiv way
if(array_key_exists($permission_id, $permissionModules)) {
foreach($permissionModules[$permission_id] as $includes) {
requireModule($includes);
}
}
Wrong ? Correct me!
I am developing a site using php and mysql. I want to know... what's a good way to deal with multi-lingual support? I want a user to be able to select from a drop down and select their language. Then everything (content, buttons, links) except the user-written content is in their language.
What's a good way to approach this? Use a cookie? Session?
Something like this works fine:
Langs.php
<?
// check if language switch as been set at url var
if ($_GET["lang_change"]) {
$_SESSION['session_name']["lang"] = $_GET["lang_change"];
}
// set value to lang for verification
$active_lang = $_SESSION['session_name']["lang"];
// verify $lang content and set proper file to be load
switch ($active_lang) {
case 'prt':
$lang_file = 'prt.php';
break;
case 'gbr':
$lang_file = 'gbr.php';
break;
case 'fra' :
$lang_file = 'fra.php';
break;
case 'esp' :
$lang_file = 'esp.php';
break;
case 'deu' :
$lang_file = 'deu.php';
break;
default:
$lang_file = 'gbr.php';
}
// load proper language file for site presentation
include_once ('$lang_file);
?>
LANG GBR FILE (gbr.php)
define("LANG_PAGETITLE_HOMEPAGE", 'Homepage');
define("LANG_BTN_KNOW_MORE", 'know more');
METHOD TO CHANGE LANGUAGE (url sample)
USE ENG
Basically, you have PHP files with constants, each file with a lang.
On click you set a url var (ex: lang_change = lang).
That will force page reload, and the langs.php file include at top of your index.php will load the selected language...
If you need more explanation about this, leave a comment and I'll send you a working sample!
Ps: session variables shown in this code is usefully for interaction with login systems, or just to avoid having the url parameters...
Save all dynamic content flagged with actual language
Make use of gettext() for buttons, etc. This one is much faster than including .php files with arrays
First of all you have to add all values in each language dynamically.
While adding dynamic content to your website, u can add languageId to each field of your tables in database. And then you can show that content at front end on behalf of that languageId.
I think it's a good idea to consider working with a framework that has internationalization support.
Take a look at this example using CakePHP http://bakery.cakephp.org/articles/view/p28n-the-top-to-bottom-persistent-internationalization-tutorial
I think the following will help you get some basic idea of developing it.
In a website, specially a multilingual website should have user interfaces / templates where hardcoded labels should be linked to variables. These variables should be loaded with correct language values. This can be done easily by including the language file containing the values in that specific language. You can have as many language files in a folder.
You will need to write a script in php, as whenever the user selects the language from the drop down, the page can reload with a language session. Another php script to fetch the selected language inside this session data and include the relevant language file inside the template/UI.
The same approach can be used in fetching content data from a table, where in all MySQL queries, you can use an additional lookup for language type from the content table. so that that file will be loaded.
SELECT * FROM posts WHERE lang='en' AND featured = 1
In many cases, the languages require HTML and CSS to be set accordingly to make the language render perfectly inside the browser. This means, you can also define language inside the HTML and in CSS define the fonts and directions (right to left or left to right).
I am recommending you to read the following in order to get more information on how to do it.
http://www.stylusinc.com/website/multilanguage_support.htm