Results 1 to 3 of 3

Thread: Using a switch statement and MySQL database

  1. #1
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts

    Using a switch statement and MySQL database

    Ok the plan is to write a script which displays the correct link (which is extracted from a database) when you click on a certain button.

    The thing is, i'm using a switch but i'm going to end up extracting the data from my database. I don't want to write a query for every single case in the switch statement as that's a long and horrible process. Surely if i define a function where instead of saying "WHERE title news" i'd replace it with something like "WHERE <? $variable = case ?> title $variable"

    Argh, it's hard to explain, i just don't want to write out a query for every single case so i want to use a function which would make it a lot shorter. Basically taking the name of the case and putting it into the query so it'd only display a certain page.

    If anyone understands what i mean feel free to point me in the right direction

  2. #2
    HEXUS.net Webmaster
    Join Date
    Jul 2003
    Location
    UK
    Posts
    3,108
    Thanks
    1
    Thanked
    0 times in 0 posts
    PHP Code:
    switch ($page)
    {
        case 
    'news':
            
    $whereClause="title='news'";
            break;
            
        case 
    'articles':
            
    $whereClause="title='articles'";
            break;
        
        default:
            
    $whereClause="title='other'";
            break;
    }

    if (isset(
    $whereClause))
    {
        
    $sqlQuery "SELECT * FROM table WHERE ".$whereClause;


  3. #3
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    genius! Cheers Az

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •