如何从magento

时间:2016-03-15 10:02:44

标签: php magento foreach

我有一个magento模块,允许用户提问和回答问题..

问题有自动递增和唯一的question_id。 答案也有相同的配置。

但是,如果用户提出3个问题,则问题会以下面的方式发布在前端...

<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>

当foreach循环返回所有值时,会打印每个问题..

但是,如果用户输入第二个问题的答案,那么如何仅针对该特定问题显示答案呢?

我尝试了以下代码,但答案出现在每个问题中。

<?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 个答案:

答案 0 :(得分:2)

在问题foreach中添加答案,如此

<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>
                    <?php 
$questionid = $data['question_id'];    
$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>  
            </div>