Welcome Guest, Not a member yet? Register   Sign In
Array parse problem
#1

[eluser]iniweb[/eluser]
I have this code:

Code:
function Index()
{
    $content = '';

    $this->db->select('id, gal_name, description');

    $this->db->orderby('id', 'asc');

    $gallery = $this->db->get('galleries');

    if($gallery->num_rows != '0')
    {
        foreach($gallery->result() as $gal)
        {
            $num = $this->db->query("SELECT * FROM ci_images WHERE gall_id = $gal->id");

            $data = array(
                'galleries_list' => $gallery->result_array(),
                'count_images'   => "$num->num_rows",
            );
        }

        $content .= $this->parser->parse('Gallerys.tpl', $data);
    }

    $data = array(
        'content'        => $content,
        'user_info'      => $this->piclib->user_info(),
        'user_enter'     => $this->piclib->user_enter_form(),
    );

    $this->parser->parse('Main.tpl', $data);
}

All work, but count_images return latest value for all galleries.

Gallerys.tpl:

Quote:{galleries_list}
<tr>
<td align="center">
<a class="link555" href="/Gallery/{id}.html"><b>{gal_name}</b></a> <font class="text33"><b>(Images: {count_images}) </b></font>
</td>
</tr>
{/galleries_list}
#2

[eluser]tonanbarbarian[/eluser]
yes $data['count_images'] will always be the last value of $num->num_rows because the array is recreate in each pass through the loop

try doing this instead

Code:
if($gallery->num_rows != '0')
    {
        $data = array();
        foreach($gallery->result() as $gal)
        {
            $num = $this->db->query("SELECT * FROM ci_images WHERE gall_id = $gal->id");

            $data[] = array(
                'galleries_list' => $gallery->result_array(),
                'count_images'   => $num->num_rows(),
            );
        }

        $content .= $this->parser->parse('Gallerys.tpl', $data);
    }

but personally I would try to use a single query to get all of the data and then build the array structure from that.
because for each "galleries" record you are running a seperate query - if one day you have 100 or even 1000 galleries you will be running 101 or 1001 queries when you can do it all in 1 query.
#3

[eluser]iniweb[/eluser]
Erorr:

Quote:Severity: Warning

Message: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 3

Filename: libraries/Parser.php

Line Number: 161
#4

[eluser]tonanbarbarian[/eluser]
Cant help you with that one. What is the Parser library? It is not a core library.




Theme © iAndrew 2016 - Forum software by © MyBB