0

我不知道如何在网上搜索答案,我不确定如何解释问题,所以如果不清楚或之前被问过,我很抱歉。

这是交易:我需要显示一些具有不同状态的项目(有“未回答”、“讨论中”和“已回答”)。现在发生的情况是显示未回答的问题,正在讨论的问题显示正确的文本,但已回答问题的文本与“讨论中”相同。当其中一个问题从“未回答”移至“讨论中”时,将显示“已回答”问题的正确文本。当没有“未回答”的问题时,该项目的文本是正确的。但是其他项目得到的文本与“未回答”相同,并且应该在“讨论中”中显示的问题不可见。

有人知道我能做些什么来解决这个问题吗?我会在下面放一些代码。谢谢!!!

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);
    }
4

1 回答 1

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);                    
            }

您将拥有一个 bean 数组 ($rv),每个 bean 都是查询的单个结果行,但它是一个对象,这比一个愚蠢的数组要好得多。

于 2012-11-23T18:12:59.753 回答