SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can an Array reduce these code lines?

    I've snipped down the actual file code lines here for illustration, and am wondering if some kind of an 'Array' might eliminate a lot of lines *and* provide more flexibility.

    My basic $var assignment config file:
    PHP Code:
    <?php
    // config_var.php
    // $var config #1
    $varnum1 "10";
    $vartext1 "Text #1";
    // $var config #2
    $varnum2 "20";
    $vartext2 "Text #2";
    // $var config #3
    $varnum3 "30";
    $vartext3 "Text #3";
    // <snipped the rest of the numbered $varnum & $vartext lines>
    //
    ?>
    Here is the file which reads in config_var.php to populate a drop-down list:
    PHP Code:
    <?php
    // dropdowntest.php
    include("config_var.php");
    echo 
    "Please Select One<br>";
    echo 
    "<SELECT NAME=\"var\" size=\"1\">";
    echo 
    "<OPTION VALUE=\" SELECTED\">" "--Choose Option--" "</OPTION>";
    if ((
    $varnum1 != "") && ($vartext1 != "")) {
    echo 
    "<OPTION VALUE=\"" $varnum1 "\">" $vartext1 "</OPTION>";
    }
    if ((
    $varnum2 != "") && ($vartext2 != "")) {
    echo 
    "<OPTION VALUE=\""  $varnum2 "\">" $vartext2 "</OPTION>";
    }
    if ((
    $varnum3 != "") && ($vartext3 != "")) {
    echo 
    "<OPTION VALUE=\""  $varnum3 "\">" $vartext3 "</OPTION>";
    }
    // <snipped the rest of the numbered sequence 'if' loops>
    //
    echo "</SELECT>";
    ?>
    In some cases, only 3 options will be needed, but in other cases, perhaps 10. So at present, I have a bunch of ""; (blank) assignments in the config file, but 10 'if' loops in the main file.

    Could some kind of 'Array' simply look for any $varnumN & $vartextN (N=Number) and spit everything out from just a couple of code lines in the main file (like having almost an unlimited number of $var assignments in the config file which would all get read & used if needed, but if only 2 or 3, then only those?

    If so, this would be very cool and usable for text fields, checkboxes & radio buttons too I would think.

    Or, is this all some kind of wishful thinking?

    Thanks much.

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you should definitely use an array for that.
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not wishful thinking. Its actually quite an easy thing to accomplish if you have a fair amount of experience with arrays.

    PHP Code:
    <?php

    // first generate two happy little arrays...

    $varnum = array (
              
    => "10",
              
    => "20",
              
    => "30"
    );

    $vartext = array (
              
    => "Text #1",
              
    => "Text #2",
              
    => "Text #3"
    );

       
    // next, use your two arrays to generate a drop down list
       
    $display  '<select name="var" size="1">';
       
    $display .= '<option value="" selected>--Choose Option--"</option>';

       
    //count the length of your array (assuming both arrays are of equal length)
       
    for($x=0;$x<count($varnum);$x++) { // x is cooler that i
          
    $display .= '<option value="' $varnum[$x] . '">' $vartext '</option>';
       }
          
    $display .= '</select>';
      
          echo 
    $display;
    ?>
    All you would really need from here is something to create your arrays for you on the fly...
    Mark
    www.MySpaceWatcher.com
    Tracking & Statistics For MySpace.com Users

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Deo:

    Well, you've just gotten me EXCITED about the possibilities here.

    However...I am not understanding how I can create the two arrays from data contained in a 'config' file BY some automatic means (maybe that's not possible).

    RE:
    All you would really need from here is something to create your arrays for you on the fly...
    Ohhh...I think that's what you mean, but I have absolutely no clue as to what that might be. Maybe it is impossible?

    Tnx for the example, but in testing it I got the following errors:

    1. Notice: Undefined offset: 0 <snip> on line 23

    2. All 3 items in the drop-down list said "Array", and not the items.

    BTW, what exactly does the .= do???

    Thanks!

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change for($x=0;$x<count($varnum);$x++) into for($x=1;$x<count($varnum);$x++)

    change $display .= '<option value="' . $varnum[$x] . '">' . $vartext . '</option>';
    into
    $display .= '<option value="' . $varnum[$x] . '">' . $vartext[$x] . '</option>';

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just looking again at php.net and saw this:
    PHP Code:
    // Use of character sequences introduced in 4.1.0
    // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
    foreach(range('a''i') as $letter) {
    echo 
    $letter;

    This produces: abcdefghi

    Umh, so if in a config $var file I had:
    PHP Code:
    $var1 "blah1";
    $var2 "blah2";
    $var3 "blah3"
    ...I'm wondering how *anything* that started with $var could get read INTO an array? I guess a preg_matchs for only the $var part would be a first step?

    But the additional real question is, can this be done *without* knowing exactly how many $var1, $var2, $var3 .... etc. exist??? (Because this can change).

    Mind boggling

    (EDIT Hmmm...I'm wondering if:
    [QUOTE]
    array_push -- Push one or more elements onto the end of array [/PHP]
    ...is part of the way to make this work?

  7. #7
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oop...i forgot to add in something...

    PHP Code:
    <?php

    .
    .
    .
    $display .= '<option value="' $varnum[$x] . '">' $vartext[$x] . '</option>'

    ?>
    $vartext should actually be $vartext[$x]

    That should also take care of the error on line 23. Guess I should test my code before I post it hehe.
    Mark
    www.MySpaceWatcher.com
    Tracking & Statistics For MySpace.com Users

  8. #8
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Deo's code is nice, but you can still reduce it.
    PHP Code:
    <?php 
    // first generate an associative array... 
    $var = array ( 
    "10" => "Text #1"
    "20" => "Text #2"
    "30" => "Text #3" 
    ); 

    // next, use your associative array to generate a drop down list 
    $display '<select name="var" size="1">'
    $display .= '<option value="" selected>--Choose Option--</option>'
    //don't need to worry about the length of the array(S) 
    foreach($var AS $key=>$value)
    {
    $display .= '<option value="' $key '">' $value '</option>'

    $display .= '</select>'

    echo 
    $display
    ?>
    Nasir
    nasir.us

  9. #9
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like biggulp beat me to it. GJ man..
    Mark
    www.MySpaceWatcher.com
    Tracking & Statistics For MySpace.com Users

  10. #10
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nasir, you can do that with the array, however its much easier to generate two arrays rather than one array with two values. So in this case, two arrays are best if he is going to end up generating the arrays on the fly.

    I would personally use a for statement with this because it allows you to
    index and iterate through the arrays as you wish. That way you can order how you like (asc, desc, etc). foreach wont allow you that flexibilty and generally is a bit slower (not like it matters much on a drop down list).

    However, for simplified code, nasirs way is one of the better ways to go. I guess I just like things messy
    Mark
    www.MySpaceWatcher.com
    Tracking & Statistics For MySpace.com Users

  11. #11
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey...you folks are ALL super...thanks for the examples.

    FYI, instead of preg_match I'm now thinking about 'strstr' maybe is a key somehow.

    Then, some kind of 'ifexists' (such a function would be nice!)...etc., etc. and something like:

    1. Determine if a $varnum1 and $vartext1 both exist, and if so, read them into arrays. Then...

    2. Determine if any additional $varnumN++ and $vartextN++ exist and do the same thing until no $varnumN and $vartextN remain.

    Then, of course, if there are only text fields, there would actually be TWO $vartext[blah]'s I think... One for the Label & the other for what value.

    Oh gosh...maybe I've started a monster here

  12. #12
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Deo
    You are quite right. I used foreach since the flexibility of iteration was not a stated requirement.

    As for 'on-the-fly' arrays, associative arrays are just as easy as 0-based integer indexed arrays. That is once you get the hang of them. [img]images/smilies/smile.gif[/img]

    RocketMan
    There is in_array( ) to check if a value is in the array, and there is array_key_exists( ) to check the keys.
    Nasir
    nasir.us

  13. #13
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the function I am looking for is something like:
    PHP Code:
    If $varnum(1through $varnum(unknown number) && $vartext(1through $vartext(unknown numberboth existthen read ALL of the sequentially numbered $varnum's & $vartext's into two arrays (or an associative array). 

    The only other thing I can think of is to put some type of cap or limit of maybe 50 or 100 and have some kind of loop which cycles through until it doesn't find any more of $varnum(sequential numbers) and $vartext(sequential numbers)...with a counter++ involved.

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI, I also tried biggulp's mod to Deo's script but Text #3 fails to show.

    This appears to have some promise:
    PHP Code:
    if ( !is_array($myArray) ) $myArray= array();
    array_push($myArray$myElement); 
    But I dunno how to get the sequentially number $varnum & $vartext stuff to read correctly into:
    PHP Code:
    if ( !is_array($varnum) ) $varnum= array();
    array_push($varnum, $???); 
    if ( !
    is_array($vartext) ) $vartext= array();
    array_push($vartext, $???); 
    Any ideas are welcome!

  15. #15
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm not sure what I'm doing here but this is a final attempt before I drop into bed at almost 1:30AM local
    PHP Code:
    <?php 
    $varnum 
    "";
    $vartext "";
    include(
    "config_var.php" ); 
    // How to get $varnum1 to increment to $varnum2, $varnum3, etc. ???
    if ((!is_null($varnum1)) && (!is_null($vartext1))) {

    if ( !
    is_array($varnum) ) {
    $varnum= array();
    array_push($varnum$varnum1);
    } else {
    array_push($varnum$varnum1);
    }
    if ( !
    is_array($vartext) ) {
    $vartext= array();
    array_push($vartext$vartext1);
    } else {
    array_push($vartext$vartext1);
    }
    // $varnum++; // doesn't work ;-(
    // $vartext++; // doesn't work ;-(
    }
    // next, use your two arrays to generate a drop down list 
    $display '<select name="var" size="1">'
    $display .= "<OPTION VALUE=\" SELECTED\">" "--Choose Option--" "</OPTION>"
    //count the length of your array (assuming both arrays are of equal length) 
    for($x=1;$x<count($varnum);$x++) { // x is cooler that i 
    $display .= '<option value="' $varnum[$x] . '">' $vartext[$x] . '</option>'

    $display .= '</select>'

    echo 
    $display
    ?>
    I can't figure out to to increment the $varnum & $vartext without affecting the basic array $varnum & $vartext names or the string content within the $vars themselves.

    FYI, I tried a 'while' loop at the top but it continually produced an error dialog box saying 'php.exe' generated an error or something like that & crashed.

    ZZZZZZZZZZZzzzzzzzzzzzzzzzzzzz...
    Last edited by RocketMan; Oct 13, 2003 at 00:38.

  16. #16
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I think I've come up with a working solution.
    Quote Originally Posted by RocketMan
    Then, of course, if there are only text fields, there would actually be TWO $vartext[blah]'s I think... One for the Label & the other for what value.
    Based on this quote I'm not sure if I fully have solved the problem though. I don't understand what you're meaning here actually.

    config_var.php
    PHP Code:
    $varnum1 "10";
    $vartext1 "Text #1";
    $varnum2 "20";
    $vartext2 "Text #2";
    $varnum3 "30";
    $vartext3 "Text #3";
    $varnum5 "50";
    $vartext3 "Text #5"
    As you see the code will also work even if your $varnum# series isn't continuous.
    PHP Code:
    $formSelect "<select name=\"var\" size=\"1\">\n"
    $formSelect .= "<option value=\"\" selected>--Choose Option--</option>\n";
    include 
    'config_var.php';
    $vars get_defined_vars();
    foreach(
    $vars AS $key=>$value) {
        if(
    substr(strtolower($key), 06) == 'varnum' && strlen($key) == 7) {
            
    $i = (int)substr($key, -1);
            if(
    array_key_exists("varnum$i"$vars) && !empty($vars["vartext$i"]))
                
    $formSelect .= '<option value=' $vars["varnum$i"] . '">' $vars["vartext$i"] . "</option>\n";
        }
    }
    $formSelect .= "</select>\n"

    echo 
    $formSelect
    If anything is unclear, I'll try to explain further.

    -Helge

  17. #17
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again, Helge!!!

    WOW

    Methinks you are a GENIUS!!!!!!!!!!!!!

    This is sooooo "cool". And if there are no $vars, I don't get an error message

    It would be very helpful to my php education & understanding if you could explain the following lines/logic, so that I can also modify it to use with several different $var (named) situations:
    PHP Code:
    $vars get_defined_vars(); 
    I am surprised this doesn't produce an error message if a $var is NOT defined!!!
    PHP Code:
        if(substr(strtolower($key), 06) == 'varnum' && strlen($key) == 7) { 
    So if I have a different $var like maybe: $varnewone3, then would I change the 0, 6 & == 7 above to: 0, 9 & == 10 ??? I can't tell how you differentiate between $varnum1 and $vartext1 when these have different lengths!
    PHP Code:
    $i = (int)substr($key, -1); 
    Can you please explain this?

    Now, for the drop down, if I want to have a text label display above the drop-down list, then I guess I can just use $varlabel = "Something"; ... and if it exists, display it. And, perhaps that can be like an on/off 'toggle' as to whether to execute the rest of the code?

    If I want to use this for checkboxes & radio buttons, I think the same code will work as you posted. But for a 'text field', then would I just use (per your code), like $varnum1 = The label name & $vartext1 = the value name?

    This is so A-W-E-S-O-M-E. Perhaps others can make use of this in their own scripts as well.

    Thank you ALL very much again. PHP is incredible

  18. #18
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm...

    FYI, I just observed that
    PHP Code:
    // $var config #4
    $varnum10 "40";
    $vartext10 "Text #4"
    will not work. Presumably, because the length of $varnum is 8?

    So, it appears this is limited to 1 through 9 options. How could I have up to 99 options???

    I tried this:
    PHP Code:
    if(((substr(strtolower($key), 06) == 'varnum') || (substr(strtolower($key), 07) == 'varnum')) && (strlen($key) == 7) || (strlen($key) == 8)) { 
    But that doesn't work & duplicates all items in the list.

    Ooops...I think this is more appropriate:
    PHP Code:
        if((substr(strtolower($key), 06) == 'varnum' && strlen($key) == 7)  || (substr(strtolower($key), 07) == 'varnum' && strlen($key) == 8)) { 
    However this does not work either.

    Thanks.
    Last edited by RocketMan; Oct 13, 2003 at 17:21.

  19. #19
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So now I have tried this:
    PHP Code:
    <?php
    $formSelect 
    "<select name=\"var\" size=\"1\">\n"
    $formSelect .= "<option value=\"\" selected>--Choose Option--</option>\n"
    include 
    'config_var.php'
    $vars get_defined_vars(); 
    foreach(
    $vars AS $key=>$value) { 
        if(
    substr(strtolower($key), 06) == 'varnum' && strlen($key) == 7) {
            
    $i = (int)substr($key, -1);
     } elseif 
         (
    substr(strtolower($key), 06) == 'varnum' && strlen($key) == 8) { 
            
    $i = (int)substr($key, -2);
     }
          if(
    array_key_exists("varnum$i"$vars) && !empty($vars["vartext$i"])) {
                
    $formSelect .= '<option value=' $vars["varnum$i"] . '">' $vars["vartext$i"] . "</option>\n"
        } 

    $formSelect .= "</select>\n"
    echo 
    $formSelect
    ?>
    This time, it does read in $varnum10 & $vartext11, but still 'duplicates' of each entry.

    And, FOURTEEN of these:
    PHP Code:
    [b]Notice[/b]: Undefined variable<snipon line [b]13[/b]
    [
    b][/b

    Ugh!

  20. #20
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi RocketMan
    Quote Originally Posted by RocketMan
    Methinks you are a GENIUS!!!!!!!!!!!!!
    Thanks. I don't think that myself though

    Quote Originally Posted by RocketMan
    I am surprised this doesn't produce an error message if a $var is NOT defined!!!
    The variable $vars will always (I think) contain something because the get_defined_vars() always will return something.
    See get_defined_vars(). Also try:
    PHP Code:
    echo '<pre>';
    print_r(get_defined_vars());
    echo 
    '</pre>'

    Quote Originally Posted by RocketMan
    PHP Code:
        if(substr(strtolower($key), 06) == 'varnum' && strlen($key) == 7) { 
    So if I have a different $var like maybe: $varnewone3, then would I change the 0, 6 & == 7 above to: 0, 9 & == 10 ???
    Yes you can extract whatever you want from the key and compare it to something.
    Quote Originally Posted by RocketMan
    I can't tell how you differentiate between $varnum1 and $vartext1 when these have different lengths!
    I don't. I kind of assume that $vartext# and $varnum# are coupled together when I find the variables. I only use $vartext# when I creatr the field. Not well explained, but I hope you get it
    Quote Originally Posted by RocketMan
    PHP Code:
    $i = (int)substr($key, -1); 
    Can you please explain this?
    Yes. First the substr() evaluates the $key variable. The start parameter -1 means that I actually want the last char in the word. The (int) is just to make sure that the result from the substr() is of type integer. See Type Casting

    Quote Originally Posted by RocketMan
    Now, for the drop down, if I want to have a text label display above the drop-down list, then I guess I can just use $varlabel = "Something"; ... and if it exists, display it. And, perhaps that can be like an on/off 'toggle' as to whether to execute the rest of the code?

    If I want to use this for checkboxes & radio buttons, I think the same code will work as you posted. But for a 'text field', then would I just use (per your code), like $varnum1 = The label name & $vartext1 = the value name?
    I don't get this quite. But it might makes me think that you should store the form in an array and then take it from there. I don't know how many variables you will have and how the naming conventions will be.

    Quote Originally Posted by RocketMan
    PHP is incredible
    Indeed

    -Helge

  21. #21
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    As I said in my previous post I might think you should store the config in a array instead of different variables.
    So I made a suggestion on how you can do this using an array. I think the this methos is better to customise.

    I'm sorry this post is a bit long, but I put in some features just to show you that this is a quite flexible way
    PHP Code:
    error_reporting(E_ALL);
    $formConfig = array(=> array('name'     => 'name',
                                   
    'type'     => 'text',
                                   
    'label'    => 'You name'
                                  
    ),

                        
    => array('name'     => 'website',
                                   
    'type'     => 'select',
                                   
    'label'    => 'Select your favorite website',
                                   
    'size'     => 3,
                                   
    'selected' => array(1,3),
                                   
    'value'    => array(=> 'sitepointforums.com',
                                                       
    => 'cnn.com',
                                                       
    => 'bbc.co.uk',
                                                       
    => 'yahoo.com',
                                                       
    => 'gogle.com'
                                                      
    )
                                  ),

                        
    => array('name'     => 'occupation',
                                   
    'type'     => 'radio',
                                   
    'label'    => 'Select your occupation',
                                   
    'selected' => 2,
                                   
    'value'    => array(=> 'Employed',
                                                       
    => 'Student',
                                                       
    => 'Retired'
                                                      
    )
                                  ),
                        
    => array('name'     => 'submit',
                                   
    'type'     => 'submit',
                                   
    'value'    => 'Submit'
                                  
    ),

                        
    => array('name'     => 'reset',
                                   
    'type'     => 'reset',
                                   
    'value'    => 'Reset'
                                  
    ),

                        
    => array('name'     => 'age',
                                   
    'type'     => 'select',
                                   
    'label'    => 'Your age',
                                   
    'size'     => 1,
                                   
    'selected' => 2,
                                   
    'value'    => array(=> 'Your age',
                                                       
    => '0 - 10',
                                                       
    => '11 - 20',
                                                       
    => '21 - 30',
                                                       
    => '31 - 40'
                                                      
    )
                                  )
                  );

    ksort($formConfig);
    $form "<p>\n<form name=\"\" method=\"post\" action=\"" $_SERVER['PHP_SELF'] . "\">\n\n";
    foreach(
    $formConfig as $order => $fields) {
        
    $name     $fields['name'];
        
    $type     $fields['type'];
        
    $label    = isset($fields['label'])    ? $fields['label']    : '';
        
    $size     = isset($fields['size'])     ? $fields['size']     : '';
        
    $selected = isset($fields['selected']) ? $fields['selected'] : '';
        
    $value    = isset($fields['value'])    ? $fields['value']    : '';


        if(
    strtolower($type) == 'text') {
            
    $form .= "<label for=\"$name\">$label:</label>\n";
            
    $form .= "<input type=\"text\" name=\"$name\" id=\"$name\" value=\"$value\" /><br />\n\n";
            
        } elseif(
    strtolower($type) == 'select') {
            
    $form .= "<label for=\"$name\">$label:</label>\n";
            
    $multiple $size 'multiple="multiple" ' '';
            
            
    $form .= "<select name=\"$name\" id=\"$name\" size=\"$size\" $multiple>\n";
            
    ksort($value);
            foreach(
    $value as $k=>$v) {
                
    $select is_array($selected) ? in_array((int)$k$selected) ? 'selected="selected"' '' : (int)$selected == (int)$k 'selected="selected"' '' ;
                
    $form .= "<option value=\"$k\" $select>$v</option>\n";
            }
            
    $form .= "</select><br />\n\n";
        
        } elseif(
    strtolower($type) == 'radio') {
            
    $form .= "<label for=\"$name\">$label:</label><br />\n";
            
    $name trim($name).'[]';
            
    ksort($value);
            foreach(
    $value as $k=>$v) {
                
    $select = (int)$selected == (int)$k 'checked="checked" ' '';
                
    $form .= "<input type=\"radio\" name=\"$name\" id=\"$name\" value=\"$v\" $select/>$v<br />\n";
            }
        
        } elseif(
    strtolower($type) == 'submit' || strtolower($type) == 'reset' || strtolower($type) == 'button') {
            
    $form .= "<button type=\"$type\" name=\"$name\">$value</button>\n";

        } 
    }
    $form .= "<form>\n</p>";

    echo 
    "<p><a href=\"" $_SERVER['PHP_SELF'] . "\">Reload</a></p>\n";
    if(
    count($_POST) > 0) {
        echo 
    '<pre>';
        
    print_r($_POST);
        echo 
    '</pre>';
    }
    echo 
    $form;
    ?> 
    The drawback with this is that you need to make a bit complex array structure as config.

    I've not styled the form using CSS so the layout isn't to good. I've put some <br /> in there just to make it readable. Most of tem should be removed when you use CSS to style it.

    If any question, you know what to do

    -Helge

  22. #22
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Helge: (*)

    Well, you've given me another great 'homework' assignment

    I will study this in detail.

    Shall I assume that 'error_reporting(E_ALL);' is just for testing, or is this something which should be used in place of the (2039) error line?

    Thanks. Will check back in.

    BTW, are you using Unix there at the University or Linux?

    Regards,

    (*) Methinks I shall perhaps call you "Professor Helge"

  23. #23
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RocketMan
    Well, you've given me another great 'homework' assignment

    I will study this in detail.
    I din't mean to actually. I just think that your first attempt using only "simple" variables will be just as confusing when things start to "grow", like if you want pre selected options.
    Quote Originally Posted by RocketMan
    Shall I assume that
    PHP Code:
    error_reporting(E_ALL); 
    is just for testing, or is this something which should be used in place of the (2039) error line?
    I always use (E_ALL) or (2047) when developing things. That's because I like to view all errors and try to get rid of them. Using (2039) or (E_ALL ~ NOTICE) is fine when your finished developnig and have tested your script.

    Quote Originally Posted by RocketMan
    BTW, are you using Unix there at the University or Linux?
    Linux, I think.

    Quote Originally Posted by RocketMan
    (*) Methinks I shall perhaps call you "Professor Helge"
    Methings not. But thanks.

    -Helge


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
  •