Jump to content

Recommended Posts

I've made a website that I have been using for around a month, and for each page it uses the GET for the variable 'content'. This then goes into a huge if statement, which looks something like:

 

<?php
if ($_GET['content'] == "home") {
echo 'This is my Homepage';
}
elseif ($_GET['content'] == "shoutbox") {
$this->permission_check(1);
$this->shoutbox();
}
# Lots more elseifs
else {
header("Location: ?error=1");
}
?>

 

I have been wondering if there is any other way to echo out the pages in a similar way?

 

Also, can you put PHP code inside a database table and use it as PHP instead of just echoing it out?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/139888-alternate-code/
Share on other sites

a fast method is turning your page name/title into array key=>value pairs

<?php
$allowed_pages = array();
$allowed_pages['home'] = "This is my Homepage";
$allowed_pages['shoutbox'] = "This is the shoutbox";
if (array_key_exists($_GET['content'], $allowed_pages)){
echo $allowed_pages[$_GET['content']];
}
else{
header("location:?error=1");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/139888-alternate-code/#findComment-731880
Share on other sites

If each condition has it's own deal, sadly no. You would need an if statement or a Switch/Case statement. The switch might be better suited for this.

 

Either way the, logic has to stay in there.

 

If the code does get put into MySQL (which there is no reason to do it cause you would have the same amount of code, just more strain on your db) you would have to use eval to run that code out of the db.

Link to comment
https://forums.phpfreaks.com/topic/139888-alternate-code/#findComment-731884
Share on other sites

<?php

$action = isset($_GET['content'])?$_GET['content']:'home';

switch($action) {
    default:
    case 'home':
          echo 'You are home!';
    break;
    case 'shoutbox':
           // code for the shoutbox
    break;
    case 'aboutus':
          // code for about us
    break;
}

?>

 

Makes it a bit nicer so you do not have to always do the conditionals/brackets, but yea just about as much work.

Link to comment
https://forums.phpfreaks.com/topic/139888-alternate-code/#findComment-731894
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.