-1

i'm a php nearby. I have a problem with my form. I connect the controller with db and extract a question and his three answers. If i click on one of this, the system return the second question, under the first question. If i click on one of the seconds answers, the system return the thirth question overwrite the second question. I would like that second question (and its answers) overwrite the first (and its answers). How i can? 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)View

<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>
Kurtbain
  • 3
  • 3
  • You want to look at MySQL's `ORDER BY` clause – Martin Nov 20 '18 at 16:11
  • Possible duplicate of [How can I reorder rows in sql database](https://stackoverflow.com/questions/812630/how-can-i-reorder-rows-in-sql-database) – Martin Nov 20 '18 at 16:11
  • No, i have postgresql and my problem isn't in db but in view. I call the questions and relative answers with id, there is no order. – Kurtbain Nov 21 '18 at 09:41
  • SQL is (partially) standard, `ORDER BY` is the same in MySQL and in PostgreSQL. "There is no order" - implement one! – Daniel W. Nov 21 '18 at 11:29
  • yes, but i don't have db problems. i have a problem of viewing. I would like the first question and it's relative answers disappear as the other. – Kurtbain Nov 21 '18 at 14:53

1 Answers1

0

In your view:

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

?>

you are setting only one value in each button. which is available as $_POST['btnScelta'] and that is used to render your last question.

Edit previous contents removed because they are not relevant to the question.

To render two questions pass two ids as the button value.

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*/);
?>

Now make controller process two question display.

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

Now in the view make modification for button to have two values seperated by comma. current question id is available as $id from 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>

you can also pass the previous question id by changing form's action property with a query like index.php?previous=$id.

  • Thanks for all! But this non change my problem. The first question and it's relative questions do not disappear. I leave the db structure in post. – Kurtbain Nov 21 '18 at 14:52
  • reading through the comments in question, I realize my understanding of your question was not correct. Let me know when you click `liceo scientifico` whether you want to show the third question `Di dove sei?` and its answers(relative questions?) only OR both second and third question with its answers and not show first question. –  Nov 21 '18 at 16:29
  • I would like that second question (and its answers) overwrite the first (and its answers). Sorry for delay – Kurtbain Nov 22 '18 at 09:09
  • Alright, The answer is updated for that. Please verify. –  Nov 22 '18 at 09:35
  • A typo in `cercaNuovoId` instead of `$reslt3` I wrote `$result3`. –  Nov 22 '18 at 09:45
  • Okay! It works! there is a error in controller. I have to parse ($str = "$reslt3";) and change the explode ( explode(",", $str); ). Thanks for everything! – Kurtbain Nov 22 '18 at 10:36