Search code examples
phphtmlphp-pgsql

How to get as much inputs as line displayed in html?


I want to open a pop up window with a form that contains inputs when I click a button. I don't have problem to do that, the particularity is I want as much inputs (in the form of the pop up) as cells of a row from my orginal window.

Basically I have page1.php that output SQL SELECT queries. For each line I generate a button that opens a pop-up window on onClick() event. I want to use this button to give users possibility to modify a line. img

I generate my table this way :

        $result = Db::query($requete);

        $texte = "<table class='table table-bordered table-sm'><thead>$table_header</thead>";
        $texte .= "<tbody>";

        if (!pg_num_rows($result)){
            $nb_ligne = "Aucune ligne trouvée !";
        }else{
            $nb_ligne ="Nombre de lignes : ".pg_num_rows($result);
        }
        while($row = pg_fetch_row($result)){
            $texte .= "<tr><td><button class=\"btn btn-primary\" type=\"button\" id=\"buttonCreate\" onclick=\"OuvrirPopup('update.php','', 'resizable=no, location=no, width=800, height=1000, menubar=no,status=no, scrollbars=no')\"></button></td>";
            foreach ($row as $value){
                $texte .= "<td style='word-break: keep-all'>$value</td>";
            }
            $texte .= "</tr>";
        }
        $texte .= "</tbody></table>";

        $response = new Response();
        $response->assign('nb_ligne', 'innerHTML', $nb_ligne);
        $response->assign('tableau_resultat', 'innerHTML', $texte);

        return $response;

I don't see how to generate as much inputsas cells in a row

and also

how to set inputs default value with what is already filled with.

I want to learn from this, so, if possible, explain to me what I'm doing wrong here, or if my approach is lacking insight.


Solution

  • In file page1.php you will do something like this:

        $result = Db::query($query); // the query should return column ID for each row
    
        $text = "<table class='table table-bordered table-sm'><thead>$table_header</thead><tbody>";
    
        if (!pg_num_rows($result))
        {
          $no_results = "Nothing was found !";
        }
        else
        {
          $no_results = "Results found: ".pg_num_rows($result);
        }
        while($row = pg_fetch_row($result))
        {
          // the popup will open `page2.php?id=ID-of-the-row`
          $text .= "<tr><td><button class='btn btn-primary' type='button' id='buttonCreate' onclick='showPopup(\"update.php?id=".$row['id']."\")'>EDIT</button></td>";
          foreach ($row as $value)
          {
            $text .= "<td style='word-break: keep-all;'>".htmlspecialchars($value)."</td>";
          }
          $text .= "</tr>";
        }
        $text .= "</tbody></table>";
    
        $response = new Response();
        $response->assign('no_results', 'innerHTML', $no_results);
        $response->assign('table_body', 'innerHTML', $text);
    
        return $response;
    

    Then, in file page2.php you will do something like this:

        $result = Db::query($query); // the query will use $_GET['id'] in order to fetch the specific row
    
        $text = "<table class='table table-bordered table-sm'><thead>$table_header</thead><tbody>";
    
        if (!pg_num_rows($result))
        {
          $no_results = "Nothing was found !";
        }
        else
        {
          $no_results = "Results found: ".pg_num_rows($result);
        }
        // there should be no more than 1 row
        while($row = pg_fetch_assoc($result))
        {
          $text .= "<tr>";
          foreach ($row as $key => $value)
          {
            $text .= "<td style='word-break: keep-all;'><input type=text name='".$key."' value='".htmlspecialchars($value)."'></td>";
          }
          $text .= "</tr>";
        }
        $text .= "</tbody></table>";
    
        $response = new Response();
        $response->assign('no_results', 'innerHTML', $no_results);
        $response->assign('table_body', 'innerHTML', $text);
    
        return $response;