Forums

foreach cycle don't work

pierpari 05 Mar, 2014
Hi,
i have this custom PHP code for saving data into table in a cycle foreach()
<?php
$bb=$form->data['SERVIZI'];
foreach ($bb as $a)
{
$db =& JFactory::getDBO();
$query = "INSERT INTO `PeF_chronoengine_chronoforms_datatable_Listino1`(`id`, `uniq_id`, `user_id`, `created`, `modified`, `servizio_id`, `prezzo`) VALUES ('','','','','','$a['id']','3')";
$db->setQuery($query);
$db->query();  
}
?>

but don't work.

If i write this

<?php
$bb=$form->data['SERVIZI'];
foreach ($bb as $a)
{
echo $a['id'];
}
?>

it's ok.

If i write this
<?php
$db =& JFactory::getDBO();
$query = "INSERT INTO `PeF_chronoengine_chronoforms_datatable_Listino1`(`id`, `uniq_id`, `user_id`, `created`, `modified`, `servizio_id`, `prezzo`) VALUES ('','','','','','2','3')";
$db->setQuery($query);
$db->query();  
?>

it's ok too.

But if i add "foreach cycle" and "INSERT query" it don't work.
Why?😟(((((

P.S.: Sorry for my english
GreyHead 05 Mar, 2014
Hi pierpari,

I think ti is a problem with quotes. Please try
VALUES ('','','','','','{$a['id']}','3')

Bob
pierpari 05 Mar, 2014
Hi Bob,
thank you for answer but the problem it isn't this.
I tried also this and don't work too
VALUES ('','','','','','3','3')
GreyHead 05 Mar, 2014
Hi pierpari,

I would add a line of Debug code to output the query that is being created then test that in PHPMyAdmin to see what the error is.

Bob
pierpari 05 Mar, 2014
Debugger write only the array
Array
(
    [option] => com_chronoforms5
    [view] => form
    [Itemid] => 127
    [chronoform] => Listino_insert
    [event] => submit
    [prezzo] => 
    [button2] => Submit
    [SERVIZI] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [uniq_id] => 
                    [user_id] => 0
                    [created] => 2014-02-18 14:25:03
                    [modified] => 
                    [servizio] => shampoo
                )
……
pierpari 05 Mar, 2014
$db =& JFactory::getDBO();
$servizio_id = '27';
$query = "INSERT INTO `PeF_chronoengine_chronoforms_datatable_Listino1`(`id`, `uniq_id`, `user_id`, `created`, `modified`, `servizio_id`, `prezzo`) VALUES ('','','','','','$servizio_id','3')";
$db->setQuery($query);
$db->query();  


this work….

if i add foreach cycle it don't work

<?php
$bb=$form->data['SERVIZI'];
foreach ($bb ad $a)
{
$db =& JFactory::getDBO();
$servizio_id = '27';
$query = "INSERT INTO `PeF_chronoengine_chronoforms_datatable_Listino1`(`id`, `uniq_id`, `user_id`, `created`, `modified`, `servizio_id`, `prezzo`) VALUES ('','','','','','$servizio_id','3')";
$db->setQuery($query);
$db->query();  
}
?>
pierpari 05 Mar, 2014
But this foreach cycle work

<?php
$bb=$form->data['SERVIZI'];
foreach ($bb as $a)
{
echo $a['id'];
}
?>
pierpari 05 Mar, 2014
Answer
I don't know how but now it's right
<?php
$bb=$form->data['SERVIZI'];
$db =& JFactory::getDBO();
foreach ($bb as $a)
{
$servizio_id = $a['id'];
$query = "INSERT INTO `PeF_chronoengine_chronoforms_datatable_Listino1`(`id`, `uniq_id`, `user_id`, `created`, `modified`, `servizio_id`, `prezzo`) VALUES ('','','','','','$servizio_id','3')";
$db->setQuery($query);
$db->query();  
}
?>


Thanks Bob for your time
GreyHead 05 Mar, 2014
Hi pierpari,

Good to hear that you got it working - well done :-)

Bob
This topic is locked and no more replies can be posted.