【问题标题】:How to get as much inputs as line displayed in html?如何获得与 html 中显示的行一样多的输入?
【发布时间】:2019-07-31 08:11:40
【问题描述】:

我想在单击按钮时打开一个带有包含输入的表单的弹出窗口。我这样做没有问题,特殊性是我想要与原始窗口中的一行单元格一样多的输入(以弹出窗口的形式)。

基本上我有 page1.php 输出 SQL SELECT 查询。对于每一行,我都会生成一个按钮,用于在 onClick() 事件上打开一个弹出窗口。 我想使用这个按钮让用户可以修改一行。

我以这种方式生成我的表格:

        $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;

我不知道如何在一行中生成尽可能多的 inputsas 单元格

还有

如何将inputs 设置为已填充的默认值。

我想从中吸取教训,因此,如果可能的话,请向我解释我在这里做错了什么,或者我的方法是否缺乏洞察力。

【问题讨论】:

  • 每行的列数应该是不变的吧?然后你需要做的就是计算 $row 包含多少元素,一次,得到数字。
  • @misorude 是的,列数是恒定的,但是如何从弹出窗口中获取单元格内容?
  • 现在这不是一个完全不同的问题吗?通常,您只需通过链接/按钮传递行 ID,然后在提供弹出内容的脚本中,进行数据库查询以获取该特定行的数据……
  • @misorude 我的问题是关于这两件事。但是我不确定是否有必要再次进行数据库查询。随着我边做边学,我每次都迷失了使用的方法

标签: php html php-pgsql


【解决方案1】:

page1.php 文件中,您将执行以下操作:

    $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;

然后,在文件page2.php 中,您将执行以下操作:

    $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;

【讨论】:

  • 这不是我真正想要的。在这里,您建议我在每个单元格上添加输入。但我正在寻找一种方法来打开一个新窗口,在该窗口中我将输入与单元格一样多的文本输入。参见上图,单击按钮时,我想打开一个弹出窗口,其中每个单元格的内容都将在文本输入中输出。我正在努力完成这一步
  • 我无法理解您的问题。首先,您说您不想在表格中输入文本输入 - 然后您说您想要与单元格一样多的输入(并且在问题中您说输入应该具有预先填充的值)。由于您正在构建表格 - 您显然知道(或可以计算)行数和列数。一旦你知道这个数字 - 你可以使用一个简单的 FOR 循环来创建相应数量的 INPUT 并将它们的 VALUE 属性设置为你喜欢/需要的任何值。
  • 有 2 个不同的页面。在 page1.php 我输出我的表和值。我对此没有任何问题。在每一行我都有一个按钮,它打开一个新窗口: page2.php 我希望在 page2.php 上填充与 page1.php 中的一行字段一样多的文本 INPUT。并且文本 INPUTvalues 是 page1.php 上单击行中字段的值
  • 好吧,您只需确保查询结果中的每一行都有一个唯一的 ID - 然后在您获取 page2.php 时提供此 ID,以便它可以从数据库中获取正确的记录.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
相关资源
最近更新 更多