我不知道如何在网上搜索答案,我不知道如何解释这个问题,所以如果不清楚或者之前是否有问题,我很抱歉。
这是交易:我需要展示一些具有不同状态的项目(有“未答复”,“在讨论中”和“已回答”)。现在正在发生的是,未显示的问题正在显示,正在讨论的问题正在显示正确的文本,但回答的问题的文本与“讨论中”相同。当其中一个问题从“未答复”移到“讨论中”时,正在显示“已回答”问题的正确文本。如果没有“未答复”的问题,则该项目的文本正确无误。但其他项目与“未答复”的文本相同,而且“在讨论中”应显示的问题不可见。
有人知道我能做些什么来解决这个问题吗?我会在下面放一些代码。感谢!!!
overzicht.php
<?php
session_start();
if(!isset($_SESSION['views']))
{
header('Location: index.php');
}
else
{
$feedback = "";
try
{
include_once('classes/question.class.php');
$oQuestion = new Question();
$oQuestionsUnanswered = $oQuestion->getQuestionsUnanswered();
$oQuestionsInDiscussion = $oQuestion->getQuestionsInDiscussion();
$oQuestionsAnswered = $oQuestion->getQuestionsAnswered();
}
catch(Exception $e)
{
$feedback = $e->getMessage();
}
}
?>
显示不同的项目(对于其他2个状态重复两次,其他变量,例如$ oQuestionsAnswered):
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(isset($oQuestionsUnanswered))
{
$unanswered_details = "";
while($arr_unanswered = mysqli_fetch_array($oQuestionsUnanswered))
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$arr_unanswered['bericht_id']."'>".$arr_unanswered['bericht_titel']."</a></div>";
$unanswered_details .= "<div class='datum'>" . $arr_unanswered['bericht_datum'] . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
question.class.php(对其他2也重复这个)
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
if($result->num_rows!=0)
{
return $result;
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
}
答案 0 :(得分:0)
IMO你犯了一个大错误,你将查询时刻与获取时刻分开,这样你就可以创建一个问题对象数组,而不是在页面内部使用它。
以下是您的代码的草稿:
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
$_rv =array()
if($result->num_rows!=0)
{
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
return $_rv
}
然后
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(count($oQuestionsUnanswered))
{
$unanswered_details = "";
foreach( $oQuestionsUnanswered as $bean)
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$bean->bericht_id."'>".$bean->bericht_titel."</a></div>";
$unanswered_details .= "<div class='datum'>" . $bean->bericht_datum . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
当然,您可以使用1类对所有类型的问题进行优化,使用旁观者选择1种函数来选择您需要的问题。
问题类型将是QuestionBean的参数。
QuestionBean是一个Bean:)
作为bean我指的是一个“简单”的数据对象,其中每个属性都有一个getter和setter或者是公共的。
我使用__constructor作为初始化程序,使用名为populate的函数:
class simpleBean{
public $id;
public function __construct($params){
// some logic as need
}
// simple populate
public function populate($params){
$valid = get_class_vars ( self );
foreach($params as $k => $v){
if(!isset($valid[$k]){
// if this key has no attrib matchig I skip it
continue;
}
$this->$k = $v;
}
}
}
class QuestionBean extend simpleBean{
public $attr1;
public function __construct($params){
// may be I've some common logic in parent
parent::__construc($params);
//
$this->populate($params);
}
}
现在
之后 while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
你将拥有一个数组($ rv)的bean,每个bean都是你查询的一个结果行,但它是一个对象,它比一个愚蠢的数组要好得多。