I have an array that contains text, indexed by a timestamp (using time()) of when it was posted. For example, the output is something like:

Array ( [1160906743] => testing )

I then want to be able to add more text onto the end, resulting in something like

Array ( [1160906743] => testing
[1160906961] => testing again )

I tried using array_push() which didn't work (probably because this is an asociative array), until someone recomended the code

$time=time();
$new=$_POST['speech'];
$speech_array[$time] = $new;

This works, but instead of adding a new line onto the end, it overwrites the whole array. So instead of

Array ( [1160906743] => testing
[1160906961] => testing again )

I'd just get

Array ( [1160906961] => testing again ) 

Can anybody offer any help?

    That php code should work. perhaps theres an issue elsewhere in your code? Is there an extra $speech_array = array(); (which resets the array) or something like that?

      Not that I can find. My entire input code is

      <?
      include 'db.php';
      if(isset($HTTP_POST_VARS['submit'])){
      
      //get old
      $speech_query=mysql_query("SELECT * FROM map WHERE `x` = '". $object[5]->x ."' AND `y` = '". $object[5]->y ."'") or die(mysql_error());
      
      
      $array_info = mysql_fetch_array($speech_query);
      $speech_array=unserialize($array_info['speech']);
      
      
      //add new
      $time=time();
      $new=$_POST['speech'];
      $speech_array[$time] = $new;
      
      $info=mysql_real_escape_string(serialize($speech_array));
      $update=mysql_query("UPDATE `map` SET `speech`= '$info' WHERE x='2' AND y='2' ") or die(mysql_error()); 
      mysql_free_result($speech_query);
      }else{
      ?>
      <form name="signup" action="<? $_SERVER['PHP_SELF']; ?>" method="POST">
       <tr>
           <td><input type="text" id ="speech" name="speech" value="" maxlength="30"></td>
      <td><input type="submit" id="submit" name="submit" value="submit"></td></form><?}?>

      And my output code is simply

      <?
      
      //start of speaking
      
      $speech_query=mysql_query("SELECT * FROM map WHERE `x` = '". $object[5]->x ."' AND `y` = '". $object[5]->y ."'") or die(mysql_error());
      
      $array_info = mysql_fetch_array($speech_query);
      $speech_array=unserialize($array_info['speech']);
      if (!is_array($speech_array)){die("NOPE 2");}
      
      
      echo "Speech array:";
      
      print_r($speech_array);
      
      
      
      
      
      
      //end of speaking
      mysql_free_result($speech_query);
      
      ?>

      I did have a couple of $speech_array-array() instances, but I got rid of them, and nothing changes.

        The code looks good to me. In the first block of code, do a print_r() on $speech_array before you add the new value, see if that array does already contain what it's meant to.

          I put in print_r($speech_array). There was no output.

            That's your problem. For some reason your array isn't getting populated.

            It's possible that your SELECT query returns 0 or more than 1 result... Try print_r($array_info).

              I tried what you suggested. There was no output from $array_info, so it must be empty. I've done some experimenting, and my code now looks like this:

              <?
              include 'db.php';
              if(isset($HTTP_POST_VARS['submit'])){
              
              //get old
              $speech_query=mysql_query("SELECT 'speech' FROM map WHERE `x` = '2' AND `y` = '2'") or die(mysql_error());
              $rows=mysql_num_rows($speech_query);
              print_r("Number of selected rows: ");
              print_r($rows);
              ?><BR><BR><?
              $array_info=mysql_fetch_array($speech_query);
              $speech_array=unserialize($speech_query);
              
              print_r("Array_info before: ");
              print_r($array_info);
              ?><BR><BR><?
              
              
              print_r("Speech_array before: ");
              print_r($speech_array);
              
              
              ?><BR><BR><?
              //add new
              $time=time();
              $new=$_POST['speech'];
              $speech_array[$time] = $new;
              
              print_r("Speech_array after: ");
              print_r($speech_array);
              
              $info=mysql_real_escape_string(serialize($speech_array));
              $update=mysql_query("UPDATE `map` SET `speech`= '$info' WHERE x='2' AND y='2' ") or die(mysql_error()); 
              mysql_free_result($speech_query);
              }else{
              ?>
              <form name="signup" action="<? $_SERVER['PHP_SELF']; ?>" method="POST">
               <tr>
                   <td><input type="text" id ="speech" name="speech" value="" maxlength="30"></td>
              <td><input type="submit" id="submit" name="submit" value="submit"></td></form><?}?>

              and my output is

              Number of selected rows: 1
              
              Array_info before: Array ( [0] => speech [speech] => speech )
              
              Speech_array before:
              
              Speech_array after: Array ( [1161103925] => whateverIsubmit )  

              One problem that I had before was that I was using values for the mysql query 'x' and 'y' that hadn't been specfied. I've replaced them with placeholder values that correspond to the data I'm testing.

              Apart from that I'm no further on.

                In 'Array_info before: Array ( [0] => speech [speech] => speech )' the [speech] variable is not serialized, so when you try to unserialize you get get an error and $speech_array has nothing in it.

                Put the following at the top of your script and try running it again. Actually, make sure you put this at the top of every script during development!

                error_reporting(E_ALL);

                  I get the output

                  Notice: unserialize() [function.unserialize]: Argument is not a string in /blah/blah/blah/staggy11.byethost3.com/blah/testing.php on line 13

                  just before the array_info is outputted.

                    The data you've previously put into the db isn't a serialized array. So you can't take it out an unserialize it. That's why the array is empty. So you need to serialize what's in the database or think of another way of doing it.

                      I used print_r to have a look at my array once it was serialised. It looked like

                      a:1:{i:1161185868;s:9:"testeing2";}

                      I then used phpMyAdmin to look at the table where it was stored. It looked like

                      a:1:{i:1161185868;s:9:"testeing2";}

                      Exactly the same.

                      Would this work without using serialise()?

                        No, you'll need to serialize it to store an array in the db.

                        $speech_array=unserialize($speech_query);

                        should be

                        $speech_array=unserialize($array_info);

                          It works! I'm not sure how I've got it working. The final code is

                          <?
                          error_reporting(E_ALL);
                          include 'db.php';
                          if(isset($HTTP_POST_VARS['submit'])){
                          
                          //get old
                          $speech_query=mysql_query("SELECT `speech` FROM `map` WHERE x='2' AND y='2' ") or die(mysql_error());
                          $rows=mysql_num_rows($speech_query);
                          print_r("Number of selected rows: ");
                          print_r($rows);
                          ?><BR><BR><?
                          
                          $array_info=mysql_fetch_array($speech_query);
                          $speech_array=unserialize($array_info['speech']); 
                          
                          print_r("Array_info before: ");
                          print_r($array_info);
                          
                          
                          ?><BR><BR><?
                          //add new
                          $time=time();
                          $new=$_POST['speech'];
                          $speech_array[$time] = $new;
                          
                          print_r("Speech_array after: ");
                          print_r($speech_array); ?><BR><BR><?
                          
                          $info=serialize($speech_array);
                          print_r("Serialized array: ");
                          print_r($info);
                          $update=mysql_query("UPDATE `map` SET `speech`= '$info' WHERE x='2' AND y='2' ") or die(mysql_error()); 
                          mysql_free_result($speech_query);
                          }else{
                          ?>
                          <form name="signup" action="<? $_SERVER['PHP_SELF']; ?>" method="POST">
                           <tr>
                               <td><input type="text" id ="speech" name="speech" value="" maxlength="30"></td>
                          <td><input type="submit" id="submit" name="submit" value="submit"></td></form><?}?>

                          I fiddled around with the query, and suddenly everything just...worked.
                          I'm gonna clean this up, save a backup copy, and set about making the output of the array a bit neater.
                          Thanks for your help matt72!

                            Write a Reply...