How to get current value from foreach loop in magento

Viewed 1525

I have a module in magento which allows user to ask question and answer question too..

The question has auto-incremented and unique question_id. the answer also has same config.

But if a user ask 3 question the question is posted on the front end in the below manner...

<div>
        <?php if(!empty($questionData)): ?>
            <?php foreach ($questionData as $data): ?>
                <table>
                    <tr>
                        <td><h1>Question:</h1> <h3><b><?php echo $data['question']; ?></b></h3></td>
                    </tr>
                    <tr>
                        <td><h1>Asked by:</h1><h3><b><?php echo $data['username']; ?></b></h3></td>
                    </tr>
                    <tr>
                    </tr>
                    </table>
            </div>

As foreach loop returns all the value , each and every question is printed..

But since if a user enters an answer for 2nd question, how is it possible to display the answer only for that particular question?

i have tried the below code but, the answer appears in each and every question.

<?php $questionData = $this->getQuestionId($prod_id); ?>
<?php foreach ($questionData as $data):$questionid = $data['question_id']; endforeach; ?>
<?php $answerData = $this->getAnswer($prod_id,$questionid); ?>

<div id="answer_block">
                <?php if(!empty($answerData)): ?>
                    <?php foreach ($answerData as $answerPosted):?>
                        <h4>********************************************</h4>
                        <h3> Answer given by: <b><?php echo $answerPosted['username']; ?></b></h3>
                        <h3> Answer: <b><?php echo $answerPosted['answer']; ?></b></h3>

                    <?php endforeach; ?>
                <?php endif; ?>
            </div>  


 public function getAnswer($product_id,$question_id)
    {
        $answerModel = Mage::getModel('questionanswer/answer')->getCollection()
                ->addFieldToFilter('status',0)
                ->addFieldToFilter('product_id',$product_id)
                ->addFieldToFilter('question_id',$question_id);
        return $answerModel;
    }
1 Answers
Related