Jump to content

adding element to array with array_push


WesleyA
 Share

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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...