Jump to content

adding element to array with array_push


Recommended Posts

I want to add elements by the browser into an array with the name $map

 

I wrote the next piece of code for this:

   <?php     $var = $_POST["var"];     function addIntoArray($var)                              {                               global $map;                               if (($var != ".") || ($var != ".."))                                               {                                              array_push($map, $var);                                              echo $var . " succesfully added to the array!";                                               }                                return;                              }     ?> 

The problem though, is not having an error code, but the screen just does not give an output at all while I did make an echo line.

 

Whats going wrong here?

 

Link to post
Share on other sites

1. Make sure you check for the existence of the input field with

<?php   if (isset($_POST['var']) {       $var = $_POST['var'];                function addIntoArray($var) {       global $map;          if (!empty($var)) {           $map[] = $var;           echo $var." succesfully added to the array!";       }           return;       }   }?>
Edited by Techneut
Link to post
Share on other sites

I have it running. But I had to call the function

 

addIntoArray($var)

 

Now I want to check if the variable is added to the array.

 

I already had an array, with elements and I want to print the entire array with its content on the screen.

 

It is not that I cant find the codes.

 

My problem when programming is that I dont know much about the principles, such as type signature, declaration, definition. Are there any resources online that explain more about these logic that is behind a programming language like specifically PHP?

Edited by WesleyA
Link to post
Share on other sites

The PHP documentation is probably a good place to start, go through the Language Reference section.http://php.net/manual/en/If you're looking for something that is more instructional, then maybe a book like Programming PHP. Check the PHP section at oreilly.com to see a list.

Link to post
Share on other sites

Ok thanks I was there already. The problem too is that since last year programmers work with the mysqli extension instead of mysql, so the php book (in my own language which is NOT present @ php.net) I bought became partly useless.

 

But I quited this array_push matter meanwhile.

 

I switchted to MYSQL giving me more chance to find scripts etc and more structure in a more complex built database.

Link to post
Share on other sites

are you saying you have a book that uses the mysqli extension and you still decided to use mysql instead? I would highly discourage that, mysql is deprecated, at minimum you should be using mysqli, or even better, PDO.

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...