Yogesh Raghav
Yogesh Raghav

Reputation: 1

How to display user data in table using session

i want to display user data suppose made a tabe like below.

 <?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'];

Now i dont want to store these values in database but display values ina table exmple below

S.NO NAME MOBILE 1 Yogesh 9717797354 2 BHASKAR 9898225441 3 ANIKESH 9594474557 4 ABHISHEK 9854774144

Now i want to display each name & mobile no inout in a table as shown below without storing in databse using session. I have tried but able to put only single value and beside inserting its updating the existing value. how can this be achieved using lop??please help??o

Upvotes: 0

Views: 5417

Answers (2)

Prav
Prav

Reputation: 2894

Session can store array of data, so you can do something like this:

<?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>";
        }
    }
?>

You can append this array every time someone adds a new contact. Like this:

<?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>

Here is none predefined content option

<?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>

Upvotes: 1

hurtbox
hurtbox

Reputation: 386

<?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>';

It uses a for loop to draw the table with values. You would have to replace the variables with the session variables you want, and change the for loop so it loops only to when you want it to end

Upvotes: 0

Related Questions