dianagaby2002 Posted August 17, 2015 Share Posted August 17, 2015 I want to use a switch.....case like for example: <a href="?link=1">Page 1</a> <a href="?link=2">Page 2</a> <a href="?link=3">Page 3</a> <?php $link = $_GET['link']; switch($link){ case 1: $page = "page1.php"; break; case 2: $page = "page2.php"; break; case 3: $page = "page3/page3.php"; break; } @include "$page"; ?> will it be hard to process about 800 pages with switch...case? thank you in advance;) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 17, 2015 Share Posted August 17, 2015 (edited) you would not use a switch/case statement to do this. switch/case statements are used when you have to select between different processing logic in each case. if all you are doing is checking if a value is one out of a permissible set, you would define the set of permissible values in a data structure somewhere (database table, array), test the input value against that data structure (db query, in_array()), then produce the output value based on the input value and use it. if you have more than a few pages on a web site you should also be dynamically serving those pages using a content management system, where the content that's different between the pages is stored in a database, and the navigation menus and the logical pages are dynamically produced by simple php code on one physical page that uses the information stored in the database. the PHPFreaks.com Questions, Comments, & Suggestions forum section where you posted this is not for asking programming questions, it's for asking questions or making comments/suggestions about this site. Edited August 17, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
bsmither Posted August 18, 2015 Share Posted August 18, 2015 "will it be hard to process" Technically, no. It will be onerous to setup and probably tedious to maintain. But well within the computational power (memory and execution time limits) of PHP. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 18, 2015 Share Posted August 18, 2015 "will it be hard to process" Technically, no. It will be onerous to setup and probably tedious to maintain. But well within the computational power (memory and execution time limits) of PHP. If you don't mind PHP evaluating up to 800 conditions to get the value of a single variable, sure. Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 18, 2015 Share Posted August 18, 2015 So do you actually have 800 physical "page" files? If so, why?! Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 18, 2015 Share Posted August 18, 2015 PHP arrays are most undoubtably a better answer: $page = array(); $pages[1] = 'page1.php'; $pages[2] = 'page2.php'; $pages[3] = 'page3/page3.php'; $link = (int)$_GET['link']; if (isset($pages[$link])) { @require_once(realpath($pages[$link])); } else { // hacking url param or page not found } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 19, 2015 Share Posted August 19, 2015 i think this is more of a theoretical problem, rather than a real situation, given that there's another current similar thread asking about two different switch/case statements, one for a book selection and a second one about chapters for each book. Quote Link to comment Share on other sites More sharing options...
Maq Posted August 19, 2015 Share Posted August 19, 2015 OP, what exactly are you trying to achieve? Your question wreaks that what you're trying to do is the wrong approach. Without more information I can't help much, but it looks like using "slugs" might help you. Quote Link to comment Share on other sites More sharing options...
dianagaby2002 Posted August 20, 2015 Author Share Posted August 20, 2015 So do you actually have 800 physical "page" files? If so, why?! Yes...My colleague made the site and he wrote the menu in each page Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 20, 2015 Share Posted August 20, 2015 the whole point of having a server-side scripting language is so that you don't have to write out and maintain 100's or even 10's of pages for a web site, like you would have had to do 20 years ago with static html pages. instead, you store just the information that's different (category, title, content, ...) between the pages in a database or an array (if you don't have that much different information), then you use a simple server-side program in one physical file to dynamically produce the navigation menu and dynamically produce each logical page from that stored information. if you or someone you know produced 10's or 100's of actual .php files that contain whole html documents, with navigation menus, and different content, you are 20 years out of date. web sites are not made this way in the year 2015. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.