CodeIgniter Forums
counting for a reply - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: counting for a reply (/showthread.php?tid=24034)

Pages: 1 2


counting for a reply - El Forum - 10-28-2009

[eluser]marjune[/eluser]
eh pipzz, in have a sample forum using ci, in my controller i joined the topic and account
table in getting the topic, views, date posted, and the author for the topic which is ok, but my problem is "counting the the reply in every topic"

my controller

Code:
function furom(){
        $this->load->helper('form');
    $this->load->helper('url');
        $furomcontent['data'] = $this->furom->furomcontent();
    $this->load->vars($furomcontent);
    $this->load->view('viewfurom');

}

my model

Code:
function furomcontent(){
    $sql = "SELECT a.fname, a.lname, topic_id, topic, views, lpost
        FROM account a JOIN forum_topic ON a.acc_id = forum_topic.acc_id
        ORDER BY topic_id DESC";
    $query = $this->db->query($sql);
    return $query->result();
                  
}

my views

Code:
<?php
            foreach($data as $field){
    ?>
                    
    <td width="6%" class="furom_body2"></td>
    <td class="furom_body"><a class="topicans">
    &lt;?php
           echo $field->topic."</a><br>Author: ".$field->fname." ".$field->lname;
         ?&gt;</td>
    <td width="10%" class="furom_body2" align="center">
        &lt;?php echo $field->views;?&gt;</td>
    <td width="10%" class="furom_body" align="center">
        &lt;?php echo "how many reply here";?&gt;</td>
    <td width="15%" class="furom_body2">&lt;?php echo $field->lpost;?&gt;</td>
                
    &lt;?php }?&gt;



counting for a reply - El Forum - 10-29-2009

[eluser]InsiteFX[/eluser]
If you can count the replies in your foreach loop then just keep a counter of them, then after your foreach loop exits echo out the counter value.

Enjoy
InsiteFX


counting for a reply - El Forum - 10-29-2009

[eluser]marjune[/eluser]
what i have done before is loading the model inside the foreach loop by passing the id then in the model i count the reply in the reply table but it didn't work


counting for a reply - El Forum - 10-29-2009

[eluser]InsiteFX[/eluser]
Do you have a reply field in your database table?

How are you defining your replies?

Enjoy
InsiteFX


counting for a reply - El Forum - 10-29-2009

[eluser]marjune[/eluser]
i dont have a reply field in my table, i just count the id's in the reply table then those id's is a primary key from the topic table!!


counting for a reply - El Forum - 10-29-2009

[eluser]InsiteFX[/eluser]
If your counting them then put it in a variable end then echo it out.

Enjoy
InsiteFX


counting for a reply - El Forum - 10-29-2009

[eluser]marjune[/eluser]
thats my problem if how can i count inside the foreach loop passing the id to the other model function


counting for a reply - El Forum - 10-29-2009

[eluser]InsiteFX[/eluser]
Try this:

Code:
&lt;?php $count = 0;
            foreach($data as $field){
            $count++;
    ?&gt;
                    
    <td width="6%" class="furom_body2"></td>
    <td class="furom_body"><a class="topicans">
    &lt;?php
           echo $field->topic."</a><br>Author: ".$field->fname." ".$field->lname;
         ?&gt;</td>
    <td width="10%" class="furom_body2" align="center">
        &lt;?php echo $field->views;?&gt;</td>
    <td width="10%" class="furom_body" align="center">
        &lt;?php echo "how many reply here ".$count-1;?&gt;</td>
    <td width="15%" class="furom_body2">&lt;?php echo $field->lpost;?&gt;</td>
                
    &lt;?php }?&gt;

You may need to take off the $count-1 and just use $count.

Enjoy
InsiteFX


counting for a reply - El Forum - 10-29-2009

[eluser]marjune[/eluser]
can i load a model function inside the foreach loop where i can count the reply from the data base table?,
its not just $count


counting for a reply - El Forum - 10-29-2009

[eluser]InsiteFX[/eluser]
I would load your model in your controller then get your count and pass the information to your view.

Example:

Code:
class Welcome extends Controller {

        var $data = array();

    function __construct()
    {
        parent::Controller();
              
                $this->load->model('your-model-name');    
    }
    
    function index()
    {
                // do your model calculations here
                
                $data['replies'] = 'assign you count here';

                // by passing the $data as the second parameter
                // you will have access to the $data array

        $this->load->view('welcome_message', $data);
    }
}

// in your view all yopu need to do is this

&lt;?php echo $replies;?&gt;

Enjoy
InsiteFX