0
votes

I use codeigniter, i send tow input as array to following php code, but following php code don't get it as array, how change php code for get value array?

<input type="text" name="date[]">
<input type="text" name="date[]">

$date = $this -> input -> post('date');

$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);
list($year, $month, $day) = explode('/', $date, 3);//Line Number: 97
if($year>=$year_now && $month<=12 ) {
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
return $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
  return '0';
}

with above code i have this error:

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: admin/model.php

Line Number: 97

-

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 2

Filename: admin/model.php

Line Number: 97

-

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: admin/model.php

Line Number: 97

3

3 Answers

1
votes

You need to read and understand the error messages:

Message: Array to string conversion (Line 97)

Meaning you're converting an array to a string. That normally results in the following string : "Array":

list($year, $month, $day) = explode('/', "Array", 3);//Line Number: 97

Using that explode on a the "Array" string returns the following:

array(1) {
  [0]=>
  string(5) "Array"
}

So an array with one string containing "Array" on index 0. So:

list($year, $month, $day) = array('Array');

Results in:

$year = "Array"; $month = NULL; $day = NULL;

Obviously this is not what you wanted. I don't know what you wanted, maybe this?:

list($year, $month, $day) = $date;

But maybe not, because you wrote you have two date fields, not three. See as well Marc B's answer.

0
votes
list($year, $month, $day) = explode('/', $date, 3);//Line Number: 97

At the point this line runs, $date is an array, not a string. You can't explode an array. If I'm understanding your code correctly, you'd probably want to do:

foreach ($date as $d) {
    list($year, $month, $day) = explode('/', $d, 3);
}

and process each of the "dates" submitted from the form separately.

0
votes

From what I see, $date is already an array.

so, you need 3 input fields.

<input type="text" name="date[]">
<input type="text" name="date[]">
<input type="text" name="date[]">

then $date should be an array already. check var_dump($this->input->post('date'));

$date = $this -> input -> post('date');

$jdate = jgmdate("Y/m/j");
list($year_now, $month_now, $day_now) = explode('/', $jdate, 3);

$year = $date[0];
$month = $date[1];
$day = $date[2];

if($year>=$year_now && $month<=12 ) {
$j2g = $this->convert_date->JalaliToGregorian($year, $month, $day);
return $j2g[0]."/".$j2g[1]."/".$j2g[2];
}else {
  return '0';
}