我在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="answerswer_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;
}
像
这样在问题中添加答案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="answerswer_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>