0

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

0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

i changed this list($year, $month, $day) = explode('/', "Array", 3);//Line Number: 97 to $date_array = array(1) { [0]=> string(5) "$date_go" } list($year, $month, $day) = array($date_array); it don't work and this $date_array = array(1) { line have error: Parse error: syntax error, unexpected. did i done true?
That will never work. You need an array with three elements (0,1,2) to have the list work. Please add example data for your date fields you outlined at the beginning of your question.
I do not understand what you mean, Please make changes in my code
I have the same problem. I don't understand what you mean. Please make changes to your question. Please specify what you're specifically looking for, what is your concrete input data, which values are entered into the form fields?
0
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.

1 Comment

this change have tow error: Message: Undefined offset: 1, Message: Undefined offset: 2. !?
0

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';
}

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.