Jump to content

query db and insert in array


masgas

Recommended Posts

Hi all!

 

I'm trying to do something and I don't get it right...

 

The idea is to have a first array with 4 different values, and each time the user clicks a form button: connect with the db, select one new value, delete the first from the existing array and insert the new value in the array...

 

I've tried this code but no good result :(

 

//connect with DB

//pull new value results in => $tipoAvo = $row['tipoAvo'];

 

 

/*Pre declared array

$avos[0]="A320";

$avos[1]="B739";

$avos[2]="A319";

$avos[3]="B737";

Pre declared array */

 

//code

$sum=sizeof($avos);

if($sum<=4){

array_push($avos, $tipoAvo);

reset($avos);

$firstinarray = current($avos);

$avos=array_diff($avos, array($firstinarray));

while (list($index, $values)= each ($avos)){

echo "<br>".$index." - ".$values;}

 

}

 

but it prints the same array, only inserting in the last line the new value: $tipoAvo. I guess that having the array predeclared is producing this result... but I don't get any ideas of how to avoid this...

 

I would like to add it and delete the first, and so on each time the user cliks the form... so I have a 4 valued array that changes constantly...

 

 

Thank you for your help, and ideas...

 

Link to comment
Share on other sites

Which value do you want removed?

Using array_splice($array, offset [, length] [, replacement])

you can easily pull off an entry in an array and replace with some other value.

Are you trying to remove the [0] and then put in a new value at the end of the array?

Link to comment
Share on other sites

Hi thank you for your reply!

 

I think that's exactly what I need... I will read about the array_splice function and try to get the solution... the idea is having an array of four values all the time, insert one in the end and delete the first one... alway pulling the values from a db...

 

:)

Link to comment
Share on other sites

If it is always first and last,

Then it is even simpler by doing:

$array = ('one', 'two', 'three', 'four');
$firstvalue = array_shift($array); // Remove first entry in the array and return it, reindex array
$array[] = 'newvalue';
/*
The array would now look like:
Array
  {
     [0] => two
     [1] => three
     [2] => four
     [3] => newvalue
  }
*/

Link to comment
Share on other sites

great!

now the question is: wouldn't the array be again= $array = ('one', 'two', 'three', 'four'); next time I load the page?

 

how could I keep it:

 

Array

  {

    [0] => two

    [1] => three

    [2] => four

    [3] => newvalue

  }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.