php appending array -
ok have changed code more have suggested, actual append still not happen. changes single value in array new value being passed in instead of 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 check see if 'tags' variable has been set because there's no guarantee array yet don't take chance of using 1 until initialize array value.
here's happens:
1. visit page first time.
2. add tag ('tag1')
3. page refreshes , reflects 'tags' variable has size of 1 , displays 'tag1'
4. add tag ('tag2')
5. page refreshes , reflects 'tags' variable has size of 1 , displays 'tag2'
"refreshes" because term know specifies action of form points page located on.
regarding update:
$_post
not persistent between request. contain values html pages sends server. long don't send list of every created tag in each request, $_post['tags']
empty.
i think want session.
you should read form handling in php.
the thing have is:
$appendedtags = $_post['tags']; $appendedtags[] = $_post['tagname'];
or if want use $_post['tags']
in further processing (which shouldn't, think have false concept of $_post
in head (because write in comment post out.... mean?)):
$_post['tags'][] = $_post['tagname'];
some remarks on code:
in php, don't have initialise length of array. can add many elements want. can think of list or table.
your
for
loop copies values 1 array another. can achieve same assigning array contained in$_post['tags']
new variable.if don't specify index, next highest index chosen new value.
you should read arrays in php.
Comments
Post a Comment