【问题标题】:display multiple items with different statuses显示具有不同状态的多个项目
【发布时间】:2012-11-23 16:18:54
【问题描述】:

我不知道如何在网上搜索答案,我不知道如何解释这个问题,所以如果不清楚或者之前有人问过,我很抱歉。

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

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

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

【问题讨论】:

    标签: php


    【解决方案1】:

    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 都是查询的单个结果行,但它是一个对象,这比愚蠢的数组要好得多。

    【讨论】:

    • 抱歉回复晚了,我真的很忙。你能解释一下吗?我在互联网上搜索了 QuestionBean,但并没有真正变得更明智。我已经从 assembla.com/code/... 添加了 QuestionBean 和 OracleBean 类,并将 QuestionBean.php 包含在函数中,但我收到一条通知:“注意:未定义的属性:第 82 行的 QuestionBean::$bericht_id (= foreach 的 href 行)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2021-07-30
    相关资源
    最近更新 更多