SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Brasil
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with switch

    I tried to do this with the command if but was so angry i almost broke my computer so i tried with switch and it was almost ok.
    In the script it makes a question to the person if the person really wants to delete information from the database and when the person clicks sim (portuguese for yes) the second message comes and the second does also and i only want the second message to appear. I thought in switch when one of the cases was satisfyied only its command would roll and in this case when you click yes(sim) there is only one case satisfyied but the command of two of then roll.

    the adress is http://www.calesa.ufsc.br/administra...&incluir=links
    the code is:
    PHP Code:
    <?PHP
        $linksid 
    $_GET['linkid'];
        
    $linksend $_GET['linkend'];
        
    $remover $_GET['removelink'];
        
    $dbcnx = @mysql_connect ('localhost''login''pass');
        
    mysql_select_db('calesa');
        switch(
    $remover){
            case 
    nao:
            echo(
    "<font face=\"arial\" size=\"2\">Tem certeza que deseja excluir o link: " $linksend " ?<br>
                  <a href=\"" 
    $PHP_SELF "?linkid=" $linksid "&removelink=sim\">SIM</a> | <a href=\"index2.php?avaliar=1&incluir=links\">NÃO</a>
                  </font>"
    );
                break;
            case 
    sim:
            
    $sqlremove "DELETE FROM Links WHERE ID=" $linksid "";
            
    mysql_query($sqlremove);
            echo(
    "<font face=\"arial\" size=\"2\">O link foi removido com sucesso! <a href=\"index2.php?avaliar=1&incluir=links\">Clique aqui</a> para voltar a página principal.</font>");
            default:
            echo(
    "<font face=\"arial\" size=\"2\">Tem certeza que deseja excluir o link: " $linksend " ?<br>
                  <a href=\"" 
    $PHP_SELF "?linkid=" $linksid "&removelink=sim\">SIM</a> | <a href=\"index2.php?avaliar=1&incluir=links\">NÃO</a>
                  </font>"
    );
        }
    ?>

  2. #2
    SitePoint Zealot oivaf's Avatar
    Join Date
    Apr 2003
    Location
    Mexico
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I didn't understand you pretty well but what about adding a break in the second case like this:

    PHP Code:
        case sim
            
    $sqlremove "DELETE FROM Links WHERE ID=" $linksid ""
            
    mysql_query($sqlremove); 
            echo(
    "<font face=\"arial\" size=\"2\">O link foi removido com sucesso! <a href=\"index2.php?avaliar=1&incluir=links\">Clique aqui</a> para voltar a página principal.</font>");
            break; 
    // I added this break
     
            
    default: 
            echo(
    "<font face=\"arial\" size=\"2\">Tem certeza que deseja excluir o link: " $linksend " ?<br> 
                  <a href=\"" 
    $PHP_SELF "?linkid=" $linksid "&removelink=sim\">SIM</a> | <a href=\"index2.php?avaliar=1&incluir=links\">NÃO</a> 
                  </font>"
    ); 
    And please don't break your computer instead break the case

    HTH

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    jgbranco,

    I've been looking at the switch construct on the PHP.net website and discovered that the SWITCH construct requires curly braces around the set of case: statements AND that EACH case statement, including the default at the end, require a BREAK statement to teminate.

    That should fix your switch problems.

    Have a look at php.net for confirmation and examples.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That last statement is incorrect. When t=he parser encounters a case that evaluates to true it begins executing code until it reaches a break;. That may cover multiple cases. An example where someone might use this to their advantage would be...
    PHP Code:
     case 'A':
     case 
    'a':
        echo 
    "Alpha";
        break; 
    Of course the case can be made for strtoupper() in this case as well... but I digres..
    Aaron Brazell
    Technosailor



  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Sketch,

    Good info -- thanks! I thought that the PHP.net manual seemed overly restrictive in that context.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apart form what's been said already I'm thinking this:
    PHP Code:
    /* ............... */
            
    case nao:
    /*........ */
            
    case sim
    should be like this (notice the quotes):
    PHP Code:
    /* ............... */
            
    case 'nao':
    /*........ */
            
    case 'sim'
    Unless nao and sim are (elsewhere defined) constants.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Zoef,

    Good catch! I missed that one.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    And the lack of quotes might be why you had porblems with the original IF version perhaps...


Bookmarks

Posting Permissions

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