is there a more elegant way to do this in PHP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    is there a more elegant way to do this in PHP?

    Hey, well I'm really pleased with myself for writing a little script today
    that changes variables according to a radio button. The thing is, I have
    this feeling part of my script could be a lot more elegant and compact than
    it is:

    if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;}; //each of these blocks
    is a range each block assigns different prices to items
    if ($radiobutton== "radio1") {$item2 =$item2 * 56 ;};
    if ($radiobutton== "radio1") {$item3 =$item3 * 85 ;};
    if ($radiobutton== "radio1") {$item4 =$item4 * 42 ;};
    if ($radiobutton== "radio1") {$item5 =$item5 * 44 ;};

    if ($radiobutton== "radio2") {$item1 =$item1 * 80 ;};
    if ($radiobutton== "radio2") {$item2 =$item2 * 85 ;};
    if ($radiobutton== "radio2") {$item3 =$item3 * 140 ;};
    if ($radiobutton== "radio2") {$item4 =$item4 * 69 ;};
    if ($radiobutton== "radio2") {$item5 =$item5 * 73 ;};

    if ($radiobutton== "radio3") {$item1 =$item1 * 94 ;};
    if ($radiobutton== "radio3") {$item2 =$item2 * 100 ;};
    if ($radiobutton== "radio3") {$item3 =$item3 * 165 ;};
    if ($radiobutton== "radio3") {$item4 =$item4 * 82 ;};
    if ($radiobutton== "radio3") {$item5 =$item5 * 86 ;};


    when I tried this:

    if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;} {$item2 =$item2 * 56 ;};
    // etc

    which looks more compact to me - no radio button check every line, I got
    some very erratic results indeed! Anyway got a spare minute to show me how
    to tighten things up a little?!

    THANKS!

    Steve


  • Justin Koivisto

    #2
    Re: is there a more elegant way to do this in PHP?

    Steve wrote:[color=blue]
    > Hey, well I'm really pleased with myself for writing a little script today
    > that changes variables according to a radio button. The thing is, I have
    > this feeling part of my script could be a lot more elegant and compact than
    > it is:
    >
    > if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;}; //each of these blocks
    > is a range each block assigns different prices to items
    > if ($radiobutton== "radio1") {$item2 =$item2 * 56 ;};
    > if ($radiobutton== "radio1") {$item3 =$item3 * 85 ;};
    > if ($radiobutton== "radio1") {$item4 =$item4 * 42 ;};
    > if ($radiobutton== "radio1") {$item5 =$item5 * 44 ;};
    >
    > if ($radiobutton== "radio2") {$item1 =$item1 * 80 ;};
    > if ($radiobutton== "radio2") {$item2 =$item2 * 85 ;};
    > if ($radiobutton== "radio2") {$item3 =$item3 * 140 ;};
    > if ($radiobutton== "radio2") {$item4 =$item4 * 69 ;};
    > if ($radiobutton== "radio2") {$item5 =$item5 * 73 ;};
    >
    > if ($radiobutton== "radio3") {$item1 =$item1 * 94 ;};
    > if ($radiobutton== "radio3") {$item2 =$item2 * 100 ;};
    > if ($radiobutton== "radio3") {$item3 =$item3 * 165 ;};
    > if ($radiobutton== "radio3") {$item4 =$item4 * 82 ;};
    > if ($radiobutton== "radio3") {$item5 =$item5 * 86 ;};
    >
    >
    > when I tried this:
    >
    > if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;} {$item2 =$item2 * 56 ;};
    > // etc
    >
    > which looks more compact to me - no radio button check every line, I got
    > some very erratic results indeed! Anyway got a spare minute to show me how
    > to tighten things up a little?![/color]

    switch($radiobu tton){
    case 'radio1':
    $item1=$item1 * 54;
    $item2=$item2 * 56;
    $item3=$item3 * 85;
    $item4=$item4 * 42;
    $item5=$item5 * 44;
    break;
    case 'radio2':
    $item1 =$item1 * 80;
    $item2 =$item2 * 85;
    $item3 =$item3 * 140;
    $item4 =$item4 * 69;
    $item5 =$item5 * 73;
    break;
    case 'radio3':
    $item1 =$item1 * 94;
    $item2 =$item2 * 100;
    $item3 =$item3 * 165;
    $item4 =$item4 * 82;
    $item5 =$item5 * 86;
    break;
    }

    OR

    if ($radiobutton== "radio1") {
    $item1 =$item1 * 54 ;
    $item2 =$item2 * 56 ;
    $item3 =$item3 * 85 ;
    $item4 =$item4 * 42 ;
    $item5 =$item5 * 44 ;
    }else if ($radiobutton== "radio2") {
    $item1 =$item1 * 80 ;
    $item2 =$item2 * 85 ;
    $item3 =$item3 * 140 ;
    $item4 =$item4 * 69 ;
    $item5 =$item5 * 73 ;
    };else if ($radiobutton== "radio3") {
    $item1 =$item1 * 94 ;
    $item2 =$item2 * 100 ;
    $item3 =$item3 * 165 ;
    $item4 =$item4 * 82 ;
    $item5 =$item5 * 86 ;
    }


    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.
    SEO Competition League: http://seo.koivi.com/

    Comment

    • Justin Koivisto

      #3
      Re: is there a more elegant way to do this in PHP?

      Justin Koivisto wrote:
      [color=blue]
      > };else if ($radiobutton== "radio3") {[/color]

      Oops... that should have been:

      }else if ($radiobutton== "radio3") {

      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.
      SEO Competition League: http://seo.koivi.com/

      Comment

      • MrPlow

        #4
        Re: is there a more elegant way to do this in PHP?

        Steve wrote:
        [color=blue]
        > Hey, well I'm really pleased with myself for writing a little script today
        > that changes variables according to a radio button. The thing is, I have
        > this feeling part of my script could be a lot more elegant and compact than
        > it is:
        >
        > if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;}; //each of these blocks
        > is a range each block assigns different prices to items
        > if ($radiobutton== "radio1") {$item2 =$item2 * 56 ;};[/color]
        <snip[color=blue]
        >
        > if ($radiobutton== "radio2") {$item1 =$item1 * 80 ;};
        > if ($radiobutton== "radio2") {$item2 =$item2 * 85 ;};[/color]
        <snip>[color=blue]
        >
        > when I tried this:
        >
        > if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;} {$item2 =$item2 * 56 ;};
        > // etc
        >
        > which looks more compact to me - no radio button check every line, I got
        > some very erratic results indeed! Anyway got a spare minute to show me how
        > to tighten things up a little?!
        >
        > THANKS!
        >
        > Steve
        >[/color]

        if ($radiobutton== "radio1")
        {
        $item1 = $item1 * 54;
        $item2 = $item2 * 56;
        //etc...
        }
        else if ($radiobutton== "radio2")
        {
        //stuff..
        }

        or you could use a switch statement in this case

        switch ($radiobutton) {
        case "radio1":
        $item1 = $item1 * 54;
        $item2 = $item2 * 56;
        break;
        case "radio2":
        //stuff..
        break;
        }

        check out php.net, they have excelent documentation of the entire language
        PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


        Comment

        • Chung Leong

          #5
          Re: is there a more elegant way to do this in PHP?

          Here's a general tip: Whenever you do a comparison against a list of things,
          think hash-table (aka associative array).

          Your code can be implemented much more compactly like this:

          $multipler_tabl e["radio1"] => array(54, 56, 85, 42, 44);
          $multipler_tabl e["radio2"] => array(80, 85, 140, 69, 73);
          $multipler_tabl e["radio3"] => array(94, 100, 165, 82, 86);

          $m = $multipler_tabl e[$radiobutton];

          $item1 *= m[0]; // this is the same as $item1 = $item1 * m[0]; btw
          $item2 *= m[1];
          $item3 *= m[2];
          $item4 *= m[3];
          $item5 *= m[4];

          The code would be more compact still if you use an array to store the
          $item's. Although depending on the nature of these items and how they're
          used, it might be easier to store them as separate variables.

          See http://www.php.net/manual/en/language.types.array.php for more info.

          Uzytkownik "Steve" <luckylucky200@ hotmail.com> napisal w wiadomosci
          news:4050c8c8$0 $34507$c3e8da3@ news.astraweb.c om...[color=blue]
          > Hey, well I'm really pleased with myself for writing a little script today
          > that changes variables according to a radio button. The thing is, I have
          > this feeling part of my script could be a lot more elegant and compact[/color]
          than[color=blue]
          > it is:
          >
          > if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;}; //each of these[/color]
          blocks[color=blue]
          > is a range each block assigns different prices to items
          > if ($radiobutton== "radio1") {$item2 =$item2 * 56 ;};
          > if ($radiobutton== "radio1") {$item3 =$item3 * 85 ;};
          > if ($radiobutton== "radio1") {$item4 =$item4 * 42 ;};
          > if ($radiobutton== "radio1") {$item5 =$item5 * 44 ;};
          >
          > if ($radiobutton== "radio2") {$item1 =$item1 * 80 ;};
          > if ($radiobutton== "radio2") {$item2 =$item2 * 85 ;};
          > if ($radiobutton== "radio2") {$item3 =$item3 * 140 ;};
          > if ($radiobutton== "radio2") {$item4 =$item4 * 69 ;};
          > if ($radiobutton== "radio2") {$item5 =$item5 * 73 ;};
          >
          > if ($radiobutton== "radio3") {$item1 =$item1 * 94 ;};
          > if ($radiobutton== "radio3") {$item2 =$item2 * 100 ;};
          > if ($radiobutton== "radio3") {$item3 =$item3 * 165 ;};
          > if ($radiobutton== "radio3") {$item4 =$item4 * 82 ;};
          > if ($radiobutton== "radio3") {$item5 =$item5 * 86 ;};
          >
          >
          > when I tried this:
          >
          > if ($radiobutton== "radio1") {$item1 =$item1 * 54 ;} {$item2 =$item2 * 56[/color]
          ;};[color=blue]
          > // etc
          >
          > which looks more compact to me - no radio button check every line, I got
          > some very erratic results indeed! Anyway got a spare minute to show me how
          > to tighten things up a little?!
          >
          > THANKS!
          >
          > Steve
          >
          >[/color]



          Comment

          • Steve

            #6
            Re: is there a more elegant way to do this in PHP?

            *Thanks* guys - that looks so much better!

            Steve


            Comment

            Working...