Welcome Guest, Not a member yet? Register   Sign In
Little php help required - Array of Rules
#1

[eluser]Deep Arora[/eluser]
I need little php help to resolve the issue I am facing.

Here's my code:

Code:
foreach ($query->result() as $row)
            {
                $config = array(
                         'field'   => $row->field_name,
                         'label'   => $row->field_label,
                         'rules'   => $row->field_rule
                );
            }
            $rules_config = array($config);
            return $rules_config;

In this, the $config array inside foreach loop ends up having only the last value in the row record. How can I keep adding records to this array so it forms a final array like this, after foreach is through;
Code:
$config = array(
                         'field'   => $row->field_name,
                         'label'   => $row->field_label,
                         'rules'   => $row->field_rule
                          ),
                         array(
                         'field'   => $row->field_name,
                         'label'   => $row->field_label,
                         'rules'   => $row->field_rule
                          );
#2

[eluser]maria clara[/eluser]
can you post error/s if so?? you can try this:

Code:
foreach ($query->result() as $row)
            {
                $config[] = array(
                         'field'   => $row->field_name,
                         'label'   => $row->field_label,
                         'rules'   => $row->field_rule
                );
            }
            
            return $config;
#3

[eluser]ChiefChirpa[/eluser]
You've left out the square brackets by $config:

Code:
foreach ($query->result() as $row)
            {
                $config[] = array(
                         'field'   => $row->field_name,
                         'label'   => $row->field_label,
                         'rules'   => $row->field_rule
                );
            }
            $rules_config = array($config);
            return $rules_config;

Edit: Oh yeah you won't need "$rules_config = array($config);" anymore as Dyllon as posted below.
#4

[eluser]Dyllon[/eluser]
Code:
foreach ($query->result() as $row)
{
    $rules_config[] = array(
                'field'   => $row->field_name,
                'label'   => $row->field_label,
                'rules'   => $row->field_rule
    );
}

return $rules_config;
#5

[eluser]Deep Arora[/eluser]
bummer..yup..it works now...THANKS!!!! Smile




Theme © iAndrew 2016 - Forum software by © MyBB