Welcome Guest, Not a member yet? Register   Sign In
SOLVED: How can i include a foreach loop in the template parser array?
#1

[eluser]Slowcheetah[/eluser]
I can't seem to get the template parser inside a loop. Any solution for this?

Here is my current code:

Code:
// Checkbox loop
if ( isset ($_POST['option']))
{
    $option = $_POST['option'];
    foreach ($option as $option_id)
    {
        $this->db->where('id', $option_id);
        $selected_options = $this->db->get('opties');
    }
};

// Set data
$data = array ('selected_options'=>$selected_options->result_array());

// Parse Template
$this->parser->parse('options_view', $data);
#2

[eluser]TheFuzzy0ne[/eluser]
Selected Options should be an array should it not? I don't understand the loop. You're overwriting the selected_options variable with each iteration of the loop. If I understand the problem correctly, to get a loop in your view you'd need to do something like this:

Code:
{selected_options}
    {option_id}
    {option_title}
{/selected_options}
(You need to change the names to match the names of the database fields you want to display).


By using $this->input->post(), you can save yourself a line of code. I also feel you need a bit more checking going on in your foreach loop. I've rewritten your code below, with my proposed changes.
Code:
$selected_options = array();

// Checkbox loop
if ($option = $this->input->post('option'))
{
    foreach ($option as $option_id)
    {
        $res = $this->db->get_where('opties', array('id', $option_id));

        if ($res->num_rows())
        {
            $selected_options[] = $res->row_array();
        }
    }
};

// Set data
$data['selected_options'] = $selected_options);

// Parse Template
$this->parser->parse('options_view', $data);

I haven't tested it, but I think it does what you want it to.
#3

[eluser]Slowcheetah[/eluser]
[quote author="TheFuzzy0ne" date="1241636508"]I haven't tested it, but I think it does what you want it to.[/quote]

I think this is going to the right direction, thanks for that. However i still get a errormessage.

Code:
Error Number: 1054

Unknown column '0' in 'where clause'

SELECT * FROM (`opties`) WHERE `0` = 'id' AND `1` = '3'

Something wrong with?

Code:
$res = $this->db->get_where('opties', array('id', $option_id));
#4

[eluser]TheFuzzy0ne[/eluser]
Code:
$res = $this->db->get_where('opties', array('id', $option_id));

# Should be...

$res = $this->db->get_where('opties', array('id' => $option_id));

Sorry - I'm always doing that...
#5

[eluser]Slowcheetah[/eluser]
[quote author="TheFuzzy0ne" date="1241638766"]

Sorry - I'm always doing that...[/quote]

Works like a charm, thanks for this Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB