SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loop for inserting into array

    Im sending some variables from Flash to a php page. The code below is what I use to get the variables into an array and then I display them with a for loop.

    Instead of listing the whole darn thing out like this I was hoping it would be possible to use a loop or something to get this down to a small chunck of code. I can not send from Flash as an array. The items names could be 1,2,3 and so on that I could identify later.


    if ($item1 == "1") {
    $items[] = "Cases of Envelopes" ;
    $itemno[] = "001" ;
    $qty[] = $q1 ;
    $cost[] = $uc1 ;
    $tcost[] = $tc1 ;
    }
    if ($item2 == "1") {
    $items[] = "Metal Cages" ;
    $itemno[] = "002" ;
    $qty[] = $q2 ;
    $cost[] = $uc2 ;
    $tcost[] = $tc2 ;
    }
    if ($item3 == "1") {
    $items[] = "Plastic Trays" ;
    $itemno[] = "003" ;
    $qty[] = $q3 ;
    $cost[] = $uc3 ;
    $tcost[] = $tc3 ;
    }

    and so on

  2. #2
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you know the max. number of items?
    And is $items and $itemno hardcoded all the time?

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes on max number of items. 10

    $items do not have to be hardcoded. They could be incremented integers but they would always have to correspond to the if statement- $item1, $item2 should always result in $items being equal to 1 and 2

  4. #4
    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)
    I never tried this, but I think you could use a 'variable variable'
    http://www.php.net/manual/en/languag...s.variable.php
    Maybe something like this:
    PHP Code:
    <?php// first create an items lookup array
    $items_lu = array('1' => 'Cases of Envelopes''2' => 'Metal Cages'/*etcetera*/
    // initialize arrays
    $items[] = array();
    $itemno[] = array();
    $qty[] = array();
    $cost[] = array();
    $tcost[] = array();

    for (
    $i 1$i <= 10$i++)
        {
        
    $item_varname 'item' $i;
        
    $q_varname 'q' $i;
        
    $uc_varname 'uc' $i ;
        
    $tc_varname 'tc' $i ;

        if (isset($
    $item_varname) && $$item_varname == '1' )
            {
            
    $items[] = $item_lu[$i];
            
    $itemno[] = function_to_get_leading_zeros($i); // make it : )
            
    $qty[] = $$q_varname;
            
    $cost[] = $$uc_varname ;
            
    $tcost[] = $$tc_varname ;
            }
        }
     
    ?>
    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  5. #5
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't seem to get it to work but I will look into this further

    Thanks

  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)
    You're welcome, what's the problem?

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

  7. #7
    SitePoint Addict
    Join Date
    Apr 2002
    Location
    Miami
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's acting as I have typo somewhere. I worked on it till about 2 am this morning then gave up.

    Over coffee this morning I had the possibly stupid idea that it would be easier to concatenate all the variables in Flash and send them as a single variable with some defining characters so I can reformat the string in php.

    For my situation this works since I want to display what was sent and also store the data in a single row column of the database. I have no need for the data except to refer

    Actually works pretty well. Sure makes for a real clean php page.

    I have another question though if you dont mind. The qty value is the only one that the user has the option to fill out. All other values are selected with buttons. Is there any security reason in allowing this field to have html characters? If not I could include the table formatting as well and leave it at that.

    BTW this database does not hold anything of importance but still dont want people messing around in there

  8. #8
    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)
    The security reason: <script>/*nasty code*/</script>

    You could use 'strip_tags' to strip out any html/php tags.

    Or 'htmlspecialchars' if you would like to dispay the html code.

    Or better, validate the input. Us 'is_numeric' for example, or regular expressions.

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


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
  •