Web Analytics Made Easy -
StatCounter Why isn't this adding to the array? - CodingForum

Announcement

Collapse
No announcement yet.

Why isn't this adding to the array?

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

  • Why isn't this adding to the array?

    I'm trying to write to an array & include my own indices with it but it doesn't seem to be working as when I try & use the foreach() function on it it doesn't work because it isn't an array..

    PHP Code:
    $ba_fill $key $selected2;

    $ba[$ba_fill] = $string
    Any ideas?

  • #2
    post the rest of the relevant code

    Comment


    • #3
      Code is below.... btw; I changed over from using foreach() to using list() with each() but get the same problem..

      PHP Code:
      function getFolderChoices($selected_id='')
      {
      global 
      $uid;
      global 
      $in_folder;
      global 
      $folder;

      if(
      $in_folder=='')
      {
        
      $folder[0]='';
        
      $in_folder[0]=0;
        
      $query="SELECT id,name,folder_id FROM `files` WHERE user_id='$uid' and type='folder' ORDER BY id ASC;";
        
      $res=mysql_query($query);
        while(list(
      $id,$name,$folder_id)=mysql_fetch_array($res))
        {
           
      $folder[$id]=$name;
           
      $in_folder[$id]=$folder_id;
        }
      }

        foreach(
      $folder as $key => $value)
        {
          
      $string_old='1';
          
      $infolder=$key;
          
      $string='/'.$folder[$key];
          while(
      $string_old!=$string)
          {
            
      $infolder=$in_folder[$infolder];
            
      $string_old=$string;
            
      $string=getDownwardFolders($folder,$infolder,$string);
         }

          
      $selected=($selected_id==$key) ? ' selected' '';
          
      $selected2 str_replace(' '''$selected);

          
      $ba_fill $key $selected2;

          
      $ba[$ba_fill] = $string;

          
      //$buffer.="<option value='$key'$selected>$string</option>";
        
      }

      $ba asort($ba);

        
      reset($ba);

        while (list(
      $ikey$ivalue) = each($ba))
        {

            if (
      strpos($ikey'selected') != false)
            {
                
      $buffer.="<option value='$ikey' selected>$ivalue</option>";
            } else {
              
      $buffer.="<option value='$ikey'>$ivalue</option>";
            }

        }

        return 
      $buffer;
      }

      // getDownwardFolders function

      function getDownwardFolders($folder,$in_folder,$string)
      {
        if(
      $in_folder==0)
           
      $return=$string;
        else
           
      $return="/".$folder[$in_folder].$string;

           return 
      $return;

      Comment


      • #4
        quote the variable when adding to the array ...

        PHP Code:
        <?php
        $key 
        ='blah';
        $selected2='ug';
        $ba_fill $key $selected2;

        $ba[$ba_fill] = 'doh';
        $ba["$ba_fill"] = 'whoot';


        print_r($ba);
        ?>
        resistance is...

        MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

        Comment


        • #5
          There is no sense in quoting the variable, because its already a string. And converting a string to a string is just stupid.

          This will do fine
          PHP Code:
          $key ='blah'
          $selected2='ug'
          $ba_fill $key $selected2

          $ba[$ba_fill] = 'doh';
          print_r($ba
          dumpfi
          "Failure is not an option. It comes bundled with the software."
          ....../)/)..(\__/).(\(\................../)_/)......
          .....(-.-).(='.'=).(-.-)................(o.O)...../<)
          ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
          ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
          Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

          Comment


          • #6
            Hmmmmm.....not sure if that'll work. I was just doing some more testing & it seemed to work when I commented this lines..

            PHP Code:
            $ba asort($ba); 
            Why would that be affecting it?

            Cheers!

            Comment


            • #7
              Ok... tried it out with more simpler code.....

              PHP Code:
              $folder = array('Zero','One','Two','Three','Four');

                foreach(
              $folder as $key => $value)
                {

                  
              $string '/' $value '/';

                  
              $selected=($key==2) ? ' selected' '';
                  
              $selected2 str_replace(' '''$selected);

                  
              $ba_fill $key $selected2;

                  
              $ba[$key] = $string;
                  
              $si[$key] = $selected2;

                  
              //$buffer.="<option value='$key'$selected>$string</option>";
                
              }

              $ba asort($ba);

                
              reset($ba);

              //  if (!is_array($ba)) {
              //  print_r($ba);
              //  }

                
              while (list($ikey$ivalue) = each($ba))
                {

                  
              $ckey $si[$ikey];

                    if (
              strpos($ckey'selected') != false)
                    {
                        
              $buffer.="<option value='$ikey' selected>$ivalue</option>\n";
                    } else {
                      
              $buffer.="<option value='$ikey'>$ivalue</option>\n";
                    }

                }

              // Output

              //echo "<form>\n";
              //echo "<select>\n";
              //echo ($buffer);
              //echo "</select>\n";
              //echo "</form>\n";

              print_r($ba); 
              In this case the output from print_r() is..

              PHP Code:

              When I comment out the..

              PHP Code:
              $ba asort($ba); 
              .. the print_r() function outputs..

              PHP Code:
              Array ( [0] => /Zero/ [1] => /One/ [2] => /Two/ [3] => /Three/ [4] => /Four/ ) 

              Comment


              • #8
                Because asort() works on the given array in place and doesn't return anything.

                Basically you sort the array and set it then to nothing (ie. the return value of asort).

                Do this instead:
                PHP Code:
                asort($ba); 
                dumpfi
                "Failure is not an option. It comes bundled with the software."
                ....../)/)..(\__/).(\(\................../)_/)......
                .....(-.-).(='.'=).(-.-)................(o.O)...../<)
                ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
                ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
                Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

                Comment


                • #9
                  Thanks mate..... that worked a charm!

                  Comment

                  Working...
                  X