0

I need to insert array value into table I have the forms which has array value.Now return only first character of an array 's' need to print 'sample1'

<input type="text"  placeholder="First Name" name="first_name[]"/>
<input type="text"  placeholder="Last Name" name="last_name[]">
<input type="text"  placeholder="Age" name="age[]">

codeigniter code

   $fname=$this->input->post('first_name');
   foreach($fname as $name){
       $n=$name['first_name']; //return only first character
     }
  print_r($fname) return an array
    Array ( [0] => sample1 [1] => sample2 )
2
  • 1
    Your form names are arrays, if you want only a singular value, remove the [] from them, and don't foreach the post value. Commented Feb 24, 2016 at 10:55
  • What is your desired result ? Commented Feb 24, 2016 at 10:56

3 Answers 3

3

Just use like this :

foreach($fname as $name){
       $n=$name;
     }

Edit :

$fname=$this->input->post('first_name');
for($i=0;$i<count($fname);$i++) {
    echo $fname[$i];
}
Sign up to request clarification or add additional context in comments.

8 Comments

I need to get 2nd value from the array i.e) sample2
Then simple use $fname[1]. No need for foreach.
can you please give example
$fname=$this->input->post('first_name'); echo$fname[1];
for($i=0;$i<($a-1);$i++) { echo $i; $person = array( 'firstName' => $fname[$i], 'lastName' => $lname[$i] ); }
|
3

change foreach like this

   foreach($fname as $name){
   $n=$name; //return only first character
 }

this working fine

OR You store all data (first_name) single cell try this

example: sample1,sample2

 $first_names= implode(",",$fname); 

Comments

0

You can use it like this:

foreach($this->input->post("first_name") as $name){
    $n=$name;
}

No need to assign post data to a variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.