0

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

Shadow
  • 33,525
  • 10
  • 51
  • 64

2 Answers2

1

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>
Prav
  • 2,785
  • 1
  • 21
  • 30
  • well i wanto also show S.No in that filed ,like it have to be autoincrement like when user added ,it will get incremented in table like 1,2,3,4,5 & so on – Yogesh Raghav Oct 09 '17 at 19:49
  • @YogeshRaghav I have updated the post with two different approaches, first using multidimensional array (when get bigger, it get messy), second is more simple solution of increasing the variable by `1` every time loop ran (No storage manipulation required). – Prav Oct 09 '17 at 20:20
  • also can i use it withou usng array_merge..like suppos eif i want to create a new table based on new input posted as name & mobile no beside using existing one...i mean if user post name and mobile so that should be added,if it post another that should be added to current one..like that..no static data shud be used?? – Yogesh Raghav Oct 09 '17 at 20:40
  • If you just remove everything above this line `// if new contact added`. You wont get the predefined data. Instead you'll get two `input ` boxes. And every time you add new contact it will start listing. – Prav Oct 09 '17 at 20:46
  • well in case if i dont want predifebd data but instead i us eto display the data that iam entering thruinpt..cant it be achieved? or i have to use predefined data ..i have tried by keeping predifed array field blank but when imam posting it showing lank values of 1st record..so thers no chance? – Yogesh Raghav Oct 09 '17 at 20:49
  • If you need to persist data, you'll have to write it to a permanent data source; such as file or database. You can try writing to a CSV file and reading it back. – Prav Oct 09 '17 at 20:53
0
<?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

hurtbox
  • 376
  • 1
  • 3
  • 13