【问题标题】:How to display user data in table using session如何使用会话在表中显示用户数据
【发布时间】:2018-03-20 01:31:47
【问题描述】:

我想显示用户数据,假设制作了如下表格。

 <?php session_start() ?>
 <form method="post" action="">
 <input type="text" name="name"/>
 <input type="text" name="mobile"/>
 <input type="submit" name="submit"/>
 </form>

 <?php
 $_SESSION['name']=$_POST['name'];
 $_SESSION['mobile']=$_POST['mobile'];

现在我不想将这些值存储在数据库中,而是在下面的表格示例中显示值

S.NO NAME MOBILE 1 约格什 9717797354 2 巴斯卡尔 9898225441 3 阿尼克什 9594474557 4 阿布舍克 9854774144

现在我想在表格中显示每个名称和移动 no inout,如下所示,而不使用会话存储在数据库中。我已经尝试过,但只能放置单个值,并且除了插入其更新现有值之外。 如何使用 lop 来实现这一点??请帮助??o

【问题讨论】:

    标签: php html session


    【解决方案1】:

    Session 可以存储数据数组,所以你可以这样做:

    <?php
        session_start();
        $contact = array(
            "1" => array("Yogesh" => "9717797354"),
            "2" => array("BHASKAR" => "9898225441"),
            "3" => array("ANIKESH" => "9594474557"),
            "3" => array("ABHISHEK" => "9854774144"),
        );
        
        $_SESSION["contact"] = $contact;
        $cl = $_SESSION["contact"];
    
        foreach ($cl as $pos => $info) {
            foreach ($info as $name => $number) {
                echo $pos.": ".$name.": ".$number."</br>";
            }
        }
    ?>

    您可以在每次有人添加新联系人时附加此数组。像这样:

    <?php
        session_start();
        $contact = array(
            "Yogesh" => "9717797354",
            "BHASKAR" => "9898225441",
            "ANIKESH" => "9594474557",
            "ABHISHEK" => "9854774144",
        );
        
        // Load the predifined list if not already loaded
        if (!isset($_SESSION["contact"])) {
            $_SESSION["contact"] = $contact;
        }
    
        // if new contact added
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (isset($_POST["name"]) && isset($_POST["mobile"])) {
                if (isset($_POST["name"]) && isset($_POST["mobile"])) {
                    $name = $_POST["name"];
                    $number = $_POST["mobile"];
                    $appendArray = array(
                        $name => $number
                    );
    
                    // create new array by merging the existing one and the new data.
                    $newList = array_merge($_SESSION["contact"], $appendArray);
                    $_SESSION["contact"] = $newList;
                }
            }
        }
    ?>
    
    <table>
      <tr>
        <th>S.NO</th>
        <th>NAME</th>
        <th>MOBILE</th>
      </tr>
      <?php
        $cl = $_SESSION["contact"];
        $size = 0;
        foreach ($cl as $name => $number) {
      ?>
        <tr>
          <td><?php echo ++$size; ?></td>
          <td><?php echo $name; ?></td>
          <td><?php echo $number; ?></td>
        </tr>
      <?php } ?>
    </table>
    
    <form method="post" action="">
    <input type="text" name="name"/>
    <input type="text" name="mobile"/>
    <input type="submit" name="submit"/>
    </form>

    这里没有预定义的内容选项

    <?php
        session_start();
        
        // if new contact added
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (isset($_POST["name"]) && isset($_POST["mobile"])) {
                if (isset($_POST["name"]) && isset($_POST["mobile"])) {
                    $name = $_POST["name"];
                    $number = $_POST["mobile"];
                    $appendArray = array(
                        $name => $number
                    );
    
                    // Run if the first time
                    if (!isset($_SESSION["contact"])) {
                        $_SESSION["contact"] = $appendArray;
                    } else {
                        
                        // create new array by merging the existing one and the new data.
                        $newList = array_merge($_SESSION["contacts"], $appendArray);
                        $_SESSION["contact"] = $newList;
                    }
                }
            }
        }
    ?>
    
    <table>
      <tr>
        <th>S.NO</th>
        <th>NAME</th>
        <th>MOBILE</th>
      </tr>
      <?php
        if (isset($_SESSION["contact"])){
        $cl = $_SESSION["contact"];
        $size = 0;
        foreach ($cl as $name => $number) {
      ?>
        <tr>
          <td><?php echo ++$size; ?></td>
          <td><?php echo $name; ?></td>
          <td><?php echo $number; ?></td>
        </tr>
        <?php }} ?>
    </table>
    
    <form method="post" action="">
    <input type="text" name="name"/>
    <input type="text" name="mobile"/>
    <input type="submit" name="submit"/>
    </form>

    【讨论】:

    • 好吧,我还想在该文件中显示 S.No,就像它必须像用户添加时一样自动递增,它将在表中递增,如 1、2、3、4、5 等等
    • @YogeshRaghav 我已经用两种不同的方法更新了帖子,第一种是使用多维数组(当变大时会变得混乱),第二种是更简单的解决方案,即每次循环将变量增加1运行(无需存储操作)。
    • 我也可以在不使用 array_merge 的情况下使用它..就像假设我想根据新输入创建一个新表name 和 mobile 应该被添加,如果它发布另一个应该添加到当前的..like that.. no static data shud are used??
    • 如果您只是删除此行以上的所有内容// if new contact added。你不会得到预定义的数据。相反,您将获得两个 input 框。每次添加新联系人时,它都会开始列出。
    • 好吧,如果我不想要 predifebd 数据,而是我使用 eto 显示我正在输入 thruinpt 的数据..不能实现吗?或者我必须使用预定义的数据..我尝试将预定义的数组字段保留为空白,但是当阿訇发布它时显示第一条记录的 lank 值..所以没有机会?
    【解决方案2】:
    <?php
    echo '<table><tr><th>ONE</th><th>TWO</th></tr>';
    for($i = 0; $i < 3; $i++) {
        echo '<tr>';
        echo '<td>';
        echo $i;
        echo '</td>';
        echo '<td>';
        echo $i*2;
        echo '</td>';
        echo '</tr>';
    }
    echo '</table>';
    

    它使用 for 循环来绘制带有值的表格。您必须将变量替换为所需的会话变量,并更改 for 循环,使其仅在您希望结束时循环

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 2011-09-13
      相关资源
      最近更新 更多