Announcement

Collapse
No announcement yet.

PHP question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP question

    Hello,

    As has been mentioned before, I'm working on a website for my brother.
    I use this

    to allow the user to add fields.

    This yields variables of the form field1, field2, ...

    However, it is possible to remove fields, so I basically have a number of field variables, but the numbering is not necessarily consequtive.

    What is the best way to read these in PHP? (I'm a PHP newbie)

    On could make a loop that goes from 1 to 1000 (highly unlikely the user will add that much fields ), and then check to see which fields exist.
    But can it be done more efficiently?


    Jörg
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

  • #2
    Make the field names an array, name="title[]"

    On submission you would end up with a title[] array containing all the cd titles, you can then either loop through the values or access them individually. title[0]->title[n-1]

    Comment


    • #3
      Also, if you use a decent forms class you could just let php add/remove any fields, rather then relying on any clientside scripting (javascript). With a forms class you would have 'sticky' fields so on submitting the page (to add/remove fields) you wouldn't loose any data.

      I created something similar for a Purchase Order app I created about a year and a half ago.

      Comment


      • #4
        Thanks!
        Do you have tutorials on those form class thingies?

        For now, I'll just keep the script as it is (first version of the website), but it is interesting to consider better alternatives when reworking/bugfixing the site.


        Jörg
        pixar
        Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

        Comment


        • #5
          Here's a tutorial on html_quickform, I believe it is one of the better group of classes, never used it myself but I have seen it mentioned countless times.
          http://www.onlamp.com/pub/a/php/2004...quickform.html

          Comment


          • #6
            Great, thanks!

            Edit: just remembered there is a counter variable that holds the largest number. So if a pass this to the PHP scritp, I know the end point for my loop.


            Jörg
            Last edited by VJ; 30 March 2006, 00:44.
            pixar
            Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

            Comment


            • #7
              You don't need to know the size of the array by passing in a counter.
              There's the foreach syntax:
              Code:
              foreach ($colors as &$color) {
                 $color = strtoupper($color);
              }
              The & here is for passing by reference to allow modification of the elements and also references use less memory then copying the elements. Although if you want to be sure nothing inadvertently changes the elements then don't pass by reference.

              or you can even get the number of items in the array:
              Code:
              $count = count($array);
              Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
              Motion Computing M1400 -- Tablet PC, Ubuntu Linux

              "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

              Comment


              • #8
                cool....


                Jörg
                pixar
                Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

                Comment

                Working...
                X