php 仅刷新页面中的数据

php refreshing only data in page

我是附近的 php。我的表格有问题。我将控制器与 db 连接并提取一个问题和他的三个答案。如果我点击其中一个,系统 return 第二个问题,在第一个问题下面。如果我点击第二个答案之一,系统 return 第三个问题会覆盖第二个问题。 我希望第二个问题(及其答案)覆盖第一个问题(及其答案)。我怎么能? db structure view

1) index.php

<?php 
error_reporting(E_ERROR | E_PARSE);
require_once 'controllers/controller.php';
$controllers = new Controller();
$controllers->cercaDomanda(1);
$controllers->cercaRisposta(1);
$controllers->cercaNuovoId($id);
?>

2) Controller.php

<?php
require_once('models/Domanda.php');
require_once('models/Risposta.php');
require_once ('models/Model.php');

class Controller {
    public $domanda;
    public $risposta;  
    public $model;

    public function __construct(){

        $this->domande = new Domanda(); //istanziamo la classe domanda
        $this->risposte = new Risposta(); //istanziamo la classe risposta
        $this->model = new Model(); //istanziamo la classe risposta

    }

    public function cercaDomanda($id){                   

        $reslt = $this->domande->getDomanda($id);                         
        include 'views/basicView.php';  
        }   

     public function cercaRisposta($id){
         $reslt2 = $this->risposte->getRisposta($id);
         include 'views/basicView.php';  
        }

        public function cercaNuovoId($id){
            include 'views/basicView.php';  
            $reslt3 = $this->model->getIdNext();
            $reslt4 = $this->cercaDomanda($reslt3);
            $reslt5 = $this->cercaRisposta($reslt3);         
         }   
} 
?>

3)Domanda.php

<?php
require_once 'models/Domanda.php';

class Domanda {

    public function getDomanda($id){
        $dbconnection = pg_connect("host=***port=***dbname=***user=***password=***");
        $query = 'SELECT domanda FROM public."Domande" WHERE "id"='.$id;
        $result = pg_query($query); 
        return $result;
        pg_close($dbconnection);
        }
}
?>

4)Risposta.php

<?php
require_once 'models/Risposta.php';

class Risposta {

    public function getRisposta($id){
        $dbconnection = pg_connect("host=***port=***dbname=***user=***password=***");
        $query = 'SELECT * FROM public."Risposte" WHERE "id_domanda"='.$id;
        $result = pg_query($query); //or die ('Query fallita: ' .pg_last_error());
        return $result;
        pg_close($dbconnection);
    }    
}
?>

5)Model.php

<?php
require_once 'models/Model.php';

class Model {

    public function getIdNext(){

        if(isset($_POST['btnScelta'])){      
            $result = $_POST['btnScelta'];
            return $result;         
        }            
    }  
}

6)查看

<html>

<head></head>

<body>

    <form action='index.php' name='chatbotyForm' method="POST">

<table>

<?php

 echo "<tr>";  
   $domanda = pg_fetch_array($reslt, null, PGSQL_ASSOC);
   print $domanda['domanda'];
   echo "</tr>";
?>

</table>    

<?php 
       while ($rispost = pg_fetch_object($reslt2)) {
            print "<td><button type='submit' name='btnScelta'    
            value='$rispost->id_domanda_successiva'>$rispost->risposta</button> 
              </td>";
             }

?>

</form>

</body>
</html>

在您看来:

...
<?php 
   while ($rispost = pg_fetch_object($reslt2)) {
        print "<td><button type='submit' name='btnScelta'    
        value='$rispost->id_domanda_successiva'>$rispost->risposta</button> 
          </td>";
         }

?>

您在每个按钮中只设置了一个值。以 $_POST['btnScelta'] 的形式提供,用于呈现您的最后一个问题。

编辑 删除了之前的内容,因为它们与问题无关。

要呈现两个问题,将两个 id 作为按钮值传递。

To remove the first question remove the two lines from your index.php

1) index.php

<?php 
error_reporting(E_ERROR | E_PARSE);
require_once 'controllers/controller.php';
$controllers = new Controller();
/* remove the following two lines the first question wont be rendered.*/
//$controllers->cercaDomanda(1);
//$controllers->cercaRisposta(1);
$controllers->cercaNuovoId(/*$id*/);
?>

现在让controller处理两个问题显示

class Controller {
    public function cercaNuovoId(/*$id*/){
        // include 'views/basicView.php';  
        $reslt3 = $this->model->getIdNext();
        // render previous question only if a value exists.
        if ($result3) {
          list($current, $previous) = explode(',', $result3);
          $this->cercaDomanda($previous);
          $this->cercaRisposta($previous);
        }
        // show the first question on first visit.
        if (empty($current)){
          $current = 1;
        }
        $reslt4 = $this->cercaDomanda($current);
        $reslt5 = $this->cercaRisposta($current);
     }
}

现在在视图中对按钮进行修改,使其具有用逗号分隔的两个值。当前问题 ID 可作为 $id 来自 Controller::cercaRisposta()

<?php 
   while ($rispost = pg_fetch_object($reslt2)) {
        print "<td><button type='submit' name='btnScelta'    
        value='{$rispost->id_domanda_successiva},{id}'>$rispost->risposta</button> 
          </td>";
         }
?>
</form>

您还可以通过使用 index.php?previous=$id.

等查询更改表单的操作 属性 来传递上一个问题 ID