Dossy Posted January 7, 2009 Share Posted January 7, 2009 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 More sharing options...
jonsjava Posted January 7, 2009 Share Posted January 7, 2009 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 More sharing options...
premiso Posted January 7, 2009 Share Posted January 7, 2009 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 More sharing options...
Dossy Posted January 7, 2009 Author Share Posted January 7, 2009 @jonsjava: I had done that before but most of my pages are fairly dynamic and you can't run functions through arrays... can you? @premiso Oh, OK... How would I go about doing a switch? Thanks for the quick replies! Link to comment https://forums.phpfreaks.com/topic/139888-alternate-code/#findComment-731888 Share on other sites More sharing options...
premiso Posted January 7, 2009 Share Posted January 7, 2009 <?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 More sharing options...
Dossy Posted January 7, 2009 Author Share Posted January 7, 2009 Thanks very much Link to comment https://forums.phpfreaks.com/topic/139888-alternate-code/#findComment-731899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.