Forums

Can't seem to get data into the form->data array

chriso0258 19 Feb, 2016
Hello,

I have a column in a table that has an IP address (ipaddress) and I'm converting the ip address to a long integer. The conversion is going fine, however, I'm trying to move the data to another column called ip and can't seem to get the form-data array to take it. Here is my code:
<?php
foreach($form->data[vicondb] as $video_net_key) 
{
	if ($video_net_key['ipaddress'] != null)
	{
		$iplongvideonet = ip2long(trim($video_net_key['ipaddress']));
		$form->data[vicondb]['ip'] = $iplongvideonet;
	}
	
} // End foreach($form->data[vicondb] as $video_net_key)

?>
and the debugger results. Notice that the ip column does not reflect the new value (I have ensured that the change to the long integer is happening and not giving me a 0).
            [263] => Array
                (
                    [video_id] => 294
                    [staff] => 
                    [manufacturer] => Vicon Industries Inc. 
                    [product_code] => 
                    [model] => 
                    [serial_no] => 
                    [unit_name] => 
                    [device_name] => 172.26.2.251 
                    [location] => 
                    [mac] => 00:06:68:02:6D:17
                    [password] => 
                    [ip] => 0
                    [ipaddress] => 172.26.2.251 
                    [subnet] => 
                    [gateway] => 
                    [assigned_nvr] => 
                )

            [264] => Array
                (
                    [video_id] => 375
                    [staff] => 
                    [manufacturer] => Vicon Industries Inc. 
                    [product_code] => 
                    [model] => 
                    [serial_no] => 
                    [unit_name] => 
                    [device_name] => 172.26.2.252 
                    [location] => 
                    [mac] => 00:06:68:02:6E:DA
                    [password] => 
                    [ip] => 0
                    [ipaddress] => 172.26.2.252 
                    [subnet] => 
                    [gateway] => 
                    [assigned_nvr] => 

I'm sure I'm missing something (probably staring me right in the face but I can't see it).

Thanks for your assistance.
Chris
GreyHead 19 Feb, 2016
Hi chriso0258,

You are over-writing the same value each time. Please try this which adds $k as an index:
<?php
foreach($form->data['vicondb'] as $k => $video_net_key) 
{
 if ( !empty($video_net_key['ipaddress']) )
 {
  $iplongvideonet = ip2long(trim($video_net_key['ipaddress']));
  $form->data['vicondb'][$k]['ip'] = $iplongvideonet;
 }
 
} // End foreach($form->data['vicondb'] as $video_net_key)
?>

Bob
chriso0258 19 Feb, 2016
Thank you Bob! That worked.
chriso0258 19 Feb, 2016
One more question Bob,

When I try to save the data to the db I get the following fatal error,

Fatal error: Cannot redeclare class GCore\Models\vicondb in /var/www/administrator/components/com_chronoforms5/chronoforms/actions/db_save/db_save.php(73) : eval()'d code on line 4



Here is a picture of how my db Save is set up:
[attachment=0]ip_save.jpg[/attachment]

and I've added a line in the code for the table id
    <?php
    foreach($form->data['vicondb'] as $k => $video_net_key) 
    {
     if ( !empty($video_net_key['ipaddress']) )
     {
      $iplongvideonet = ip2long(trim($video_net_key['ipaddress']));
	  $form->data['vicondb'][$k]['video_id'] = $video_net_key['video_id'];
      $form->data['vicondb'][$k]['ip'] = $iplongvideonet;
     }
     
    } // End foreach($form->data['vicondb'] as $video_net_key)
    ?>
This topic is locked and no more replies can be posted.