Jump to content

How should I separate date strings from sub arrays within the main Array ?


vincej

Recommended Posts

Hi - I am passing $_POST data from an HTML form. The POST array looks like this:

 

       
	Array
(
    [location] => Array
        (
            [0] => Collingwood
            [1] => Varsity
        )

    [4] => 01 Apr 2012
    [1] => 12 Apr 2012
    [2] => 01 May 2012
    [3] => 01 Jun 2014
    [8] => 01 Jul 2012
    [6] => 12 Aug 2012
    [7] => 20 Apr 2013
    [5] => 20 Apr 2014
    [submit] => Save Changes
)

 

I am using a foreach loop to separate the keys from the values.  The $values include dates and locations. I then pass $values to strtotime to convert the dates to a Unix Time stamp. when strtotime sees the Locations - it throws a PHP error "strtotime() expects parameter 1 to be string, array given"

 

so my question is what can I do to separate the Dates from the locations in order to avoid the error ?

 

MANY THANKS to anyone who can give advice on this !

Link to comment
Share on other sites

Since your date fields don't seem to have pre-defined keys in the HTML (and consequently the $_POST array), try something like this:

 

<?php
// Prepare an array to stash values successfully converted to timestamps
$timestamps = array();

// Loop through post values and try convert each non-array value to a timestamp
foreach ($_POST as $key => $value) 
{
if ( ! is_array($value))
{
	$time = strtotime($value);  # returns FALSE on failure
	if ($time) $timestamps[] = $time;		
}
}
?>

Link to comment
Share on other sites

1) Test to see if the value is an array and don't process it

foreach ($_POST as $key => $value) {
if (is_array($value)) continue;
// ...
}

 

Of course, then strtotime will choke on the submit button. I would recommend changing the field names in your HTML:

<INPUT name="dates[$ID]" ...> 
# Where $ID is whatever ID or Key you are using now

# or, if that's not an ID and just a sequential array index 
<INPUT name="dates[]" ...> 
# use empty brackets

 

Then process only the dates

foreach($_POST['dates'] as $id => $value) {
  // Here we get only the dates 
}

 

[edit]

Guess I type too slow

[/edit]

Link to comment
Share on other sites

HI Guys  ! Thank  you for y our feedback !  Just to clarify, my challenge is to process BOTH the locations and the dates, not to filter them out as such. Of course, as per my earlier post, when my code processes the dates strfttime() chokes on the locations. I am a bit of a newb, so perhaps this infor might help your suggestions a bit. Here is an excerpt of the relevant  code for the HTML form and the Data Update Model ( btw - it is produced with Codeigniter, but the code is obvious )

 

HTML Form:

 

<?php
  foreach ($pickupdates as $item) {
	$theDates = explode(',', $item['Dates']);
	$theIDs = explode(',', $item['ID']);
   

   ?>
   <tr align="center"> 
<td width="100" align="center"><?php echo  $item['locationid'];?></td>
  <td width="100" align="center"><?php $data = array('name'=>'location[]', 'size'=>20,'value' => $item['location']); echo form_input($data); ?></td>
  <td width="100"><?php $data = array('name'=> $theIDs[0],'size'=>15,'value' =>(strftime("%d %b %Y",$theDates[0]))); echo form_input($data);?></td>
  <td width="100"><?php $data = array('name'=> $theIDs[1],'size'=>15,'value' =>(strftime("%d %b %Y",$theDates[1]))); echo form_input($data);?></td>
  <td width="100"><?php $data = array('name'=> $theIDs[2],'size'=>15,'value' => (strftime("%d %b %Y",$theDates[2]))); echo form_input($data); ?></td>
  <td width="100"><?php $data = array('name'=> $theIDs[3],'size'=>15,'value' => (strftime("%d %b %Y",$theDates[3]))); echo form_input($data);?></td>
  <td width="150"> <?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?></td>
  
</tr>
  <?php  ;} ?> 
  
  </table>
  
  <?php
  form_input($data);
  echo "<div class=submitted>". form_submit('submit','Save Changes')."</div>";
  echo form_close();
  ?>

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.