LunchMarble
LunchMarble

Reputation: 5151

php appending array

OK so I have changed my code to look more like what you have all suggested, but the actual append still does not happen. It simply changes the single value in the array to be the new value being passed in instead of an actual append.

if(isset($_POST['addtag']) && isset($_POST['tagname']))
{
  if(isset($_POST['tags']))
  {
    $_POST['tags'][] = $_POST['tagname'];
  }
  else
  {
    $_POST['tags'] = array($_POST['tagname']);
  }
}

The reason I check to see if the 'tags' variable has been set is because there's no guarantee that it is an array yet so I don't take the chance of using it like one until I initialize it with an array value.

Here's what happens:
1. I visit the page for the first time.
2. I add a tag ('tag1')
3. The page refreshes and reflects that the 'tags' variable has a size of 1 and displays 'tag1'
4. I add another tag ('tag2')
5. The page refreshes and reflects that the 'tags' variable has a size of 1 and displays 'tag2'

I say "refreshes" because this is the only term I know that specifies that the action of the form points to the page it is located on.

Upvotes: 2

Views: 3397

Answers (9)

Felix Kling
Felix Kling

Reputation: 817238

Regarding your update:

$_POST is not persistent between request. It will only contain values the HTML pages sends to the server. So as long as you don't send a list of every created tag in each request, $_POST['tags'] will always be empty.

I think what you want is a session.

You also should read about form handling in PHP.


The only thing you have to do is:

$appendedTags = $_POST['tags'];
$appendedTags[] = $_POST['tagname'];

or if you really want to use $_POST['tags'] in further processing (which you shouldn't, I think you have a false concept of $_POST in your head (because you write in a comment post out.... what does this mean?)):

$_POST['tags'][] = $_POST['tagname'];

Some remarks on your code:

  • In PHP, you don't have to initialise the length of an array. You can add as many elements as you want. You can think of it as a list or table.

  • Your for loop just copies all values from one array to another. You can achieve the same by just assigning the array contained in $_POST['tags'] to a new variable.

  • If you don't specify an index, the next highest index will be chosen for a new value.

You really should read about arrays in PHP.

Upvotes: 4

LunchMarble
LunchMarble

Reputation: 5151

As per Felix Kling's suggestion, I changed the $_POST to $_SESSION and the values now survive across multiple page requests. Thank you much!

Upvotes: 2

Jonah
Jonah

Reputation: 10091

All unnecessary. This will do exactly what you're trying to achieve.

if (isset($_POST['addtag'], $_POST['tagname'])) {

    // put the tagname at the end of the tags array
    $_POST['tags'][] = $_POST['tagname'];
}

Arrays in PHP are dynamic. You don't/can't predefine a size.

http://php.net/array

Upvotes: 2

shmeeps
shmeeps

Reputation: 7853

PHP doesn't include array size initialization, as all PHP array are dynamically resizable, meaning that if you have an array that has indexes from 0-4, and try to access index 5, you won't end up with a buffer overflow and will receive just a null value. This is also true for variables, if you try to read a variable that doesn't exist, PHP will quickly create it and set it to null, and you will receive a null value.

With that being said, $appendedTags[$count + 1] will actually try to access the value at $appendedTags[$count + 1]. PHP will realize that nothing exists there, create the $appendedTags array with a null value, and create indexes 0 - ($count + 1), all with null values.

The proper way to initialize the array is with $appendedTags = array(), although that itself is not required as long as $appendedTags is null. Then just assign to the array as needed, PHP will generate the rest as you use it.

Upvotes: 0

David Gillen
David Gillen

Reputation: 1172

If you have an array you can add a new item to the end of it simply by doing

$array[] = $newitem;

or in your case

$appendedTags[] = $_POST['tagname'];

Upvotes: 3

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

Consider just using PHP's array_merge instead.

Upvotes: 0

user479911
user479911

Reputation:

Use the array append operator

<?php
$array = array('orange');
$array[] = 'apple';
$array[] = 'pear';

print_r($array); //array('orange', 'apple', 'pear')
?>

Upvotes: 2

user229044
user229044

Reputation: 239571

This is not how you create an array with a given length:

$appendedTags[$count + 1];

Instead, create an empty array and append to it:

$appendedTags = array();

for($i = 0; $i < $count; $i++)
{
  $appendedTags[] = $curTags[$i];
}

//add the new tag to the end of the appended tags array
$appendedTags[] = $_POST['tagname'];

Since your initial intent seems to be to append a tag directly to $_POST['tags'], you can do all this in a single step, assuming $_POST['tags'] is an array:

$_POST['tags'][] = $_POST['tagname'];

Upvotes: 3

Jacob Relkin
Jacob Relkin

Reputation: 163318

In PHP, arrays are not of fixed size, they auto-expand, so you do not (and cannot) specify its initial length.

$appendedTags[$count + 1];

Should be:

$appendedTags = array();

Upvotes: 3

Related Questions