algorithm help....

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

    algorithm help....

    Hello there....

    I hope there's someone who can help with one algorithm...I'v e been trying
    the whole day....

    I'm building my own FORM class and have a

    function select(&$option s, $name = "", $onchange = "")

    there's also a var $selected to be called before ->select() because
    someitmes I need some stuff to be selected by default....

    I'm having a problem with algorithm after checking if ->selected is array or
    not...

    "not" part is done but "it is" part is giving me headaches....

    i also have a var $selcted_what before ->selected to define what will be
    selected ($keys or $values)...
    So the pseudo code should be..

    $options=Aray(" test1","test2", "test3","test4" ,"test5");
    $object->selected_what= "values";
    $object->selected=Array ("test1","test2 ");
    $object->select($option s);

    So the output should be select_box with test1 and test2 selected.....

    or when

    $object->selected_what= "keys";
    $object->selected=Array (1,2);
    $object->select($option s,"testing");

    now it should have the "test2" and "test3" selected....

    thanx for taking time to help....

    respect....


  • Janwillem Borleffs

    #2
    Re: algorithm help....


    "point" <point@caanNOSP AMproduction.co m> schreef in bericht
    news:bjo3dm02fc a@enews2.newsgu y.com...[color=blue]
    >
    > I'm building my own FORM class and have a
    >
    > function select(&$option s, $name = "", $onchange = "")
    >[/color]

    This is exactly how I started of my form class, before I realized it wasn't
    very user friendly and it wasn't able to cope with new properties unless I
    changed it (per example, what if you would need to define a style or a class
    attribute in the <select /> tag?)

    I rewrote the class methods to accept calls like:

    $f->select(array(' name'=>'selecti onmenu','style' =>'font-weight:bold',
    'values'=>array ('option 1'=>'1st option','option 2'=>'2nd option'),
    'default'=>arra y('2nd option')));

    which is a bit like the Perl CGI module works.
    [color=blue]
    >
    > "not" part is done but "it is" part is giving me headaches....
    >
    > i also have a var $selcted_what before ->selected to define what will be
    > selected ($keys or $values)...
    > So the pseudo code should be..
    >[/color]

    What you should do is create a class array variable which either contains
    the keys and values of the $_POST or $_GET arrays, depending on the value of
    the form's method attribute. When the name of the select menu exists in the
    class array variable and its value matches one of the options, the default
    selected option can be overwritten.


    JW




    Comment

    • point

      #3
      Re: algorithm help....

      "point" <point@caanNOSP AMproduction.co m> schreef in bericht
      news:bjo3dm02fc a@enews2.newsgu y.com...[color=blue]
      >
      > I'm building my own FORM class and have a
      >
      > function select(&$option s, $name = "", $onchange = "")
      >[/color]

      This is exactly how I started of my form class, before I realized it wasn't
      very user friendly and it wasn't able to cope with new properties unless I
      changed it (per example, what if you would need to define a style or a class
      attribute in the <select /> tag?)

      I rewrote the class methods to accept calls like:

      $f->select(array(' name'=>'selecti onmenu','style' =>'font-weight:bold',
      'values'=>array ('option 1'=>'1st option','option 2'=>'2nd option'),
      'default'=>arra y('2nd option')));
      ------------------
      Well at a start I also did put the class tag init but as a coder I saw
      that those 3 elements are all I need.....
      Also happened with some other methods....I don't have a radiobutton tags
      and some other because I didn't need them in my work by now(weird I know)..

      So what seemd like to develope to messy stuff I killed at the start and
      try not to write "the universal" class..but more what I need to help me at
      my work....
      ------------------
      which is a bit like the Perl CGI module works.
      [color=blue]
      >
      > "not" part is done but "it is" part is giving me headaches....
      >
      > i also have a var $selcted_what before ->selected to define what will be
      > selected ($keys or $values)...
      > So the pseudo code should be..
      >[/color]

      What you should do is create a class array variable which either contains
      the keys and values of the $_POST or $_GET arrays, depending on the value
      of
      the form's method attribute. When the name of the select menu exists in
      the
      class array variable and its value matches one of the options, the default
      selected option can be overwritten.
      ------------------
      That what I usually do...i use like file.php?select =5
      and in script before rendering with ->select I set the
      $object->selected=$_G ET['select]...

      That works also fine but the problem is(I can belive I forgott to mention
      that) is with MULTIPLE options....
      ------------------


      Comment

      • Janwillem Borleffs

        #4
        Re: algorithm help....


        "point" <point@caanNOSP AMproduction.co m> schreef in bericht
        news:bjp62g08ra @enews3.newsguy .com...[color=blue]
        >
        > That works also fine but the problem is(I can belive I forgott to[/color]
        mention[color=blue]
        > that) is with MULTIPLE options....[/color]

        Then you will have to check whether the passed value is an array or not:

        if (is_array($sele cted_options)) {
        foreach ($selected_opti ons as $option) {
        // When $option equals one of the predefined options
        // add 'selected' to the option
        }
        } else {
        // Business as usual
        }

        BTW, your reply was hard to read because it wasn't quoted properly with the
        '>' characters in front of the quoted lines. Please fix this.

        JW



        Comment

        • point

          #5
          Re: algorithm help....

          Hm... i tried a lots of variations but no luck....

          Last version was like this:

          if(is_array($th is->selected))
          {
          foreach($this->selected as $_selectedKey=> $_selectedValue )
          {
          foreach($option s as $value=>$output )
          {
          if($_selectedVa lue==$output)
          print("\t\t<OPT ION value=\"{$value }\"
          SELECTED>{$outp ut}</OPTION>\n");
          else
          print("\t\t<OPT ION value=\"{$value }\">{$output} </OPTION>\n");
          }
          }
          }
          else { I got this part }

          $options array has names of 150 people and as the $key there's
          person_id(from database)....

          $this->selected=Array (20,40)...

          That means that only persons with id (option value) =20 and =40 should be
          selected...

          Hope you can help.....

          Respect

          "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
          news:3f60541e$0 $28895$1b62eedf @news.euronet.n l...[color=blue]
          >
          > "point" <point@caanNOSP AMproduction.co m> schreef in bericht
          > news:bjp62g08ra @enews3.newsguy .com...[color=green]
          > >
          > > That works also fine but the problem is(I can belive I forgott to[/color]
          > mention[color=green]
          > > that) is with MULTIPLE options....[/color]
          >
          > Then you will have to check whether the passed value is an array or not:
          >
          > if (is_array($sele cted_options)) {
          > foreach ($selected_opti ons as $option) {
          > // When $option equals one of the predefined options
          > // add 'selected' to the option
          > }
          > } else {
          > // Business as usual
          > }
          >
          > BTW, your reply was hard to read because it wasn't quoted properly with[/color]
          the[color=blue]
          > '>' characters in front of the quoted lines. Please fix this.
          >
          > JW
          >
          >
          >[/color]


          Comment

          • Janwillem Borleffs

            #6
            Re: algorithm help....


            "point" <point@caanNOSP AMproduction.co m> schreef in bericht
            news:bjpo3l02to 4@enews4.newsgu y.com...[color=blue]
            > Hm... i tried a lots of variations but no luck....
            >[/color]
            ....[color=blue]
            >
            > $options array has names of 150 people and as the $key there's
            > person_id(from database)....
            >
            > $this->selected=Array (20,40)...
            >[/color]

            Since $this->selected is an 1-dimensional array, you can simplify your code
            by using the in_array function:

            foreach ($options as $value=>$output ) {
            if (is_array($this->selected)) {
            if (in_array($valu e, $this->selected)) {
            echo print("\t\t<OPT ION value=\"$value\ " SELECTED>$outpu t</OPTION>\n");
            } else {
            print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
            }
            } else {
            if ($value == $this->selected) {
            echo print("\t\t<OPT ION value=\"$value\ " SELECTED>$outpu t</OPTION>\n");
            } else {
            print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
            }
            }
            }


            JW



            Comment

            • Janwillem Borleffs

              #7
              Re: algorithm help....


              "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> schreef in bericht
              news:3f6074ea$0 $28912$1b62eedf @news.euronet.n l...[color=blue][color=green]
              > >
              > > $options array has names of 150 people and as the $key there's
              > > person_id(from database)....
              > >[/color][/color]
              ....[color=blue]
              > echo print("\t\t<OPT ION value=\"$value\ "[/color]
              SELECTED>$outpu t</OPTION>\n");

              Typo here, remove 'echo' in this and simulair lines:

              foreach ($options as $value=>$output ) {
              if (is_array($this->selected)) {
              if (in_array($valu e, $this->selected)) {
              print("\t\t<OPT ION value=\"$value\ " SELECTED>$outpu t</OPTION>\n");
              } else {
              print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
              }
              } else {
              if ($value == $this->selected) {
              print("\t\t<OPT ION value=\"$value\ " SELECTED>$outpu t</OPTION>\n");
              } else {
              print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
              }
              }
              }


              JW



              Comment

              • point

                #8
                Re: algorithm help....

                Believe it or not when I was reading your answer my php_manual was on
                is_array() function.....

                I often used that function but how did sliped my mind in this case will
                remain a mistery... :))

                So everything is working :)

                respect to you and thanx for your time to help....

                You're one of few people I see here helping others on regular basis.....

                Once again thanx....


                "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
                news:3f6074ea$0 $28912$1b62eedf @news.euronet.n l...[color=blue]
                >
                > "point" <point@caanNOSP AMproduction.co m> schreef in bericht
                > news:bjpo3l02to 4@enews4.newsgu y.com...[color=green]
                > > Hm... i tried a lots of variations but no luck....
                > >[/color]
                > ...[color=green]
                > >
                > > $options array has names of 150 people and as the $key there's
                > > person_id(from database)....
                > >
                > > $this->selected=Array (20,40)...
                > >[/color]
                >
                > Since $this->selected is an 1-dimensional array, you can simplify your[/color]
                code[color=blue]
                > by using the in_array function:
                >
                > foreach ($options as $value=>$output ) {
                > if (is_array($this->selected)) {
                > if (in_array($valu e, $this->selected)) {
                > echo print("\t\t<OPT ION value=\"$value\ "[/color]
                SELECTED>$outpu t</OPTION>\n");[color=blue]
                > } else {
                > print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
                > }
                > } else {
                > if ($value == $this->selected) {
                > echo print("\t\t<OPT ION value=\"$value\ "[/color]
                SELECTED>$outpu t</OPTION>\n");[color=blue]
                > } else {
                > print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
                > }
                > }
                > }
                >
                >
                > JW
                >
                >
                >[/color]


                Comment

                • point

                  #9
                  Re: algorithm help....

                  Yeah I noticed the echoes when I was reading the answer....I figured that
                  was a typo.....

                  :))


                  "point" <point@caanNOSP AMproduction.co m> wrote in message
                  news:bjpta001kr @enews4.newsguy .com...[color=blue]
                  > Believe it or not when I was reading your answer my php_manual was on
                  > is_array() function.....
                  >
                  > I often used that function but how did sliped my mind in this case will
                  > remain a mistery... :))
                  >
                  > So everything is working :)
                  >
                  > respect to you and thanx for your time to help....
                  >
                  > You're one of few people I see here helping others on regular basis.....
                  >
                  > Once again thanx....
                  >
                  >
                  > "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> wrote in message
                  > news:3f6074ea$0 $28912$1b62eedf @news.euronet.n l...[color=green]
                  > >
                  > > "point" <point@caanNOSP AMproduction.co m> schreef in bericht
                  > > news:bjpo3l02to 4@enews4.newsgu y.com...[color=darkred]
                  > > > Hm... i tried a lots of variations but no luck....
                  > > >[/color]
                  > > ...[color=darkred]
                  > > >
                  > > > $options array has names of 150 people and as the $key there's
                  > > > person_id(from database)....
                  > > >
                  > > > $this->selected=Array (20,40)...
                  > > >[/color]
                  > >
                  > > Since $this->selected is an 1-dimensional array, you can simplify your[/color]
                  > code[color=green]
                  > > by using the in_array function:
                  > >
                  > > foreach ($options as $value=>$output ) {
                  > > if (is_array($this->selected)) {
                  > > if (in_array($valu e, $this->selected)) {
                  > > echo print("\t\t<OPT ION value=\"$value\ "[/color]
                  > SELECTED>$outpu t</OPTION>\n");[color=green]
                  > > } else {
                  > > print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
                  > > }
                  > > } else {
                  > > if ($value == $this->selected) {
                  > > echo print("\t\t<OPT ION value=\"$value\ "[/color]
                  > SELECTED>$outpu t</OPTION>\n");[color=green]
                  > > } else {
                  > > print("\t\t<OPT ION value=\"$value\ ">$output</OPTION>\n");
                  > > }
                  > > }
                  > > }
                  > >
                  > >
                  > > JW
                  > >
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...