Welcome Guest, Not a member yet? Register   Sign In
How to: While in loop, pass the ID of each iteration element
#1

[eluser]developer10[/eluser]
View file:

Code:
foreach($rSet as $row):
    echo '<li>';
    echo '<a>' . $row->kat_naziv . '</a> <br />';
                    
         foreach($rSetPod as $row):
             echo '<a class="podkat">podkat_seo.'">' . $row->podkat_naziv . '</a>, ';
         endforeach;
                    
         echo '<span class="viseSpan">... (<a class="podkat" href="#">više</a>)</span>';
         echo '</li>';
endforeach;

Model (both main and inside foreach loops):

Code:
function listKategorije()
    {
        $query = $this->db->query(" SELECT * FROM job_kategorije ORDER BY kat_id ASC ");
        
        if($query->num_rows() > 0)
        {
            $result = $query->result();
        }
        
        return $result;
    }
    
    function listPodKategorije($katid)
    {
        $query = $this->db->query(" SELECT * FROM job_podkategorije LEFT JOIN job_kategorije ON job_podkategorije.kat_id = job_kategorije.kat_id WHERE job_podkategorije.kat_id = " . $katid . " LIMIT 0,5 ");
        
        if($query->num_rows() > 0)
        {
            $result = $query->result();
        }
        
        return $result;
    }

Controller (note that number 3 for rSetPod):

Code:
function index()
    {
        $data['rSet'] = $this->Kategorije_model->listKategorije();
        $data['rSetPod'] = $this->Kategorije_model->listPodKategorije(3);
        
        $this->template->write('naslov', 'Početna');
        $this->template->write_view('lijevo', 'lijevoView', $data);
        $this->template->write_view('sadrzaj', 'homeView', $data);
        $this->template->write_view('goredole', 'goredoleView', $data);
        $this->template->render();

    }

The question:
Is there any way to tell the controller to "read" the id of each iteration element while doing the foreach loop?
#2

[eluser]developer10[/eluser]
Any suggestions?
#3

[eluser]developer10[/eluser]
Maybe this needs some clarification. I have a feeling that this can be done.

In my DB i have table job_categories and job_subcategories. Those records in the latter table contain subctg_id and ctg_id (id of their parent).

Now, in my model, i query all categories and show them in my view, using foreach loop.

In the middle of that loop, i'm want another loop to be placed (this one shows subcategories of respective category).

Query for subcategories is also in that model file. But the problem is that i only can pass one parameter to the model, so i always get stuck with the very same subcategories below each category (those with ctg_id = 1, or 2, or 3, etc.).

I've managed to solve my problem by pure PHP while loop and sql query in the view file, but that is not satisfying enough for me. I'd like to have all queries in model files.

So, does anyone have any idea how to solve this?
#4

[eluser]Vega[/eluser]
I think I also came across a similar problem a few days ago where I did not want queries in my view. Here is a (somewhat incomplete) method that creates a multidimensional array of forums in categories that I came up with:

Code:
/**
     * Returns a multidimensional array of categories and forums
     *
     * @access    public
     * @return    array    (multidimentional array)
     */
    public function forums_in_categories()
    {
        $data = array();
        
        $this->db->order_by('category_position', 'asc');
        $query = $this->db->get('categories');
        
        if ($query->num_rows > 0)
        {
            $categories = $query->result_array();
            
            foreach ($categories as $row)
            {
                $forums = array();
                
                $this->db->order_by('forum_title', 'asc');
                $query = $this->db->get_where('forums', array('forum_category_id' => $row['category_id']));
                
                if ($query->num_rows > 0)
                {
                    $forums = $query->result_array();
                    $data[$row['category_title']] = $forums;
                }
            }
        }
        
        return $data;
    }

This method will output something like this:

Code:
$data = array(
            'category title' => array(
                array('forum_id' => '1', 'forum_title' => 'first'),
                array('forum_id' => '2', 'forum_title' => 'another forum'),
                array('forum_id' => '3', 'forum_title' => 'and another')
            ),
            'another category' => array(
                array('forum_id' => '4', 'forum_title' => 'first'),
                array('forum_id' => '5', 'forum_title' => 'another forum'),
                array('forum_id' => '6', 'forum_title' => 'and another')
            )
        );
#5

[eluser]developer10[/eluser]
Why is it incomplete? Do you think this would solve my problem? Since im not well experienced in CI and PHP, how can i use this $data in my view, using foreach loop?
Show me just one line, it will be enough. Thanks
#6

[eluser]Vega[/eluser]
It is incomplete because I need to test it a bit more, but it does produce the correct output.

The view file looks like this:

Code:
<table>
    <tr>
        <td>Forum Title</td>
        <td>Last Post</td>
        <td>Topics</td>
        <td>Posts</td>
    </tr>
    &lt;?php foreach ($results as $key => $value) { ?&gt;
    <tr>
        <th colspan="4">&lt;?php echo $key; // category title ?&gt;</th>
    </tr>
    &lt;?php foreach ($value as $row) { ?&gt;
    <tr>
        <td class="forum_title"><strong>&lt;?php echo anchor(site_url('forums/'.$row['forum_id'].'-'.$row['forum_uri']), $row['forum_title']); ?&gt;</strong><br />&lt;?php echo $row['forum_description']; ?&gt;</td>
        <td class="last_post"><strong>&lt;?php echo anchor(site_url('forums/'.$row['forum_id'].'-'.$row['forum_uri']), 'Post Title'); ?&gt;</strong><br />by &lt;?php echo anchor($row['forum_last_poster_id'], $row['forum_last_poster']); ?&gt;</td>
        <td class="num_topics">&lt;?php echo $row['forum_num_topics']; ?&gt;</td>
        <td class="num_posts">&lt;?php echo $row['forum_num_topics'] + $row['forum_num_posts']; ?&gt;</td>
    </tr>
    &lt;?php } // end inner foreach ?&gt;
    &lt;?php } // end outer foreach ?&gt;
</table>
#7

[eluser]developer10[/eluser]
Problem solved, this really works. THANKS

If you come up with an even better code or solution, please post.
#8

[eluser]developer10[/eluser]
Well, i came across similar problem again.

Your code worked well for that problem i had.
Let present it like this:
- category
--- subcategory 1
--- subcategory 2
--- subcategory 3

- category 2
--- subcategory 1
--- subcategory 2

- category 3
--- subcategory 1
--- subcategory 2
--- subcategory 3



But this time instead of just one key (category) i want to have regular query with all field values pulled, but only one (counted) value to be retrieved from some other table.

To make it more clear:

I want to loop through projects, and for each row i want the number of bids for that projects to be counted and displayed.

Example:

Project ID: [value]
Project name: [value]
Bids made: [value]
Project ends: [value]

I'd really appreciate if you have any suggestions?




Theme © iAndrew 2016 - Forum software by © MyBB