Jump to content

Recommended Posts

Hello everyone,

 

Basically, I would need 2 'switch case' scripts for navigation through HTML pages, like this:

 

1st:

index.php?1

index.php?2
index.php?3...

2nd (this one is with subpages):
index.php?1&2

Therefore, an example, with the 2nd one: I have a few books that I want to put online and index.php is the main catalog, then index.php?1 is the first book in the catalog, and the index.php?1&1 is the first book and the first chapter in it...

Is this achievable?

Link to comment
https://forums.phpfreaks.com/topic/297845-switching-case-script/
Share on other sites

someone else recently asked about using a switch/case to just test/map values to determine what content to include/produce on a page. you wouldn't use a switch/case statement just to do this since that would require that you first create the switch/case statement with all your values, then find and edit the correct place in your switch/case logic every time you add or delete content. see my reply in this thread - http://forums.phpfreaks.com/topic/297827-switchcase-for-about-800-pages/

 

for what you are asking, you would use either one or two get parameters in the url - index.php?book_id=1 or index.php?book_id=1&chapter_no=1

 

the php code would receive these as $_GET['book_id'] and $_GET['chapter_no']. you would test if the first or both of these are present, validate or cast their value so that they are safe to use, then use the value(s) to determine what content to retrieve/include into the page.

 

in short, you would use the computer to do the work for you rather than you writing out several 10's or several 100's of lines of code that only differs in the value they are testing/mapping.

we generally don't write code for people, but since you and whoever that is in the other thread are stuck on needing to use switch/case statements to do this assignment, here's an example of how you would do this if your book/chapter content is stored in files and folders/files -

// condition/cast inputs
$book_id = isset($_GET['book_id']) ? (int)$_GET['book_id'] : 0;
$chapter_no = isset($_GET['chapter_no']) ? (int)$_GET['chapter_no'] : 0;

$file = '';
if($book_id && !$chapter_no){
    // only the book_id was given
    $file = "book{$book_id}.php"; // change this to whatever maps the book_id to the book file
}

if($book_id && $chapter_no){
    // both were given
    $file = "book{$book_id}/chapter{$chapter_no}.php"; // change this to whatever maps the book_id/chapter_no to the chapter file
}

if(!$file){
    // nothing or nothing using valid numbers was requested
    echo "no book or book/chapter was requested";
} else {
    // a book or a book/chapter was requested, test if what was requested exists
    if(!file_exists($file)){
        // the book or book/chapter doesn't exist
        echo "the book or book/chapter requested doesn't exist.";
    } else {
        // the book or book/chapter exists, include it
        include $file;
    }
}

there are no switch/case statements needed, and this will work for any number of books and any number of chapters in any book.

 

using some glob() statements and a little bit of program logic would let you dynamically produce the navigation menu/links without hard-coding them too.

 

if you are taking a class and the instructor has suggested you use a switch/case statement for this task, he should have assigned a task that actually would use a switch/case statement. when the processing is the same for each input value, you would not use a switch/case statement. some examples where you could use a swtich/case statement would be - the CUD (create, update, delete) part of CRUD (create, read, update, delete) data coding, the different operations you can perform on a shopping cart (which is actually just CRUD coding).

Edited by mac_gyver
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.