2017-10-08 11 views
0

私は以下のようにユーザデータを表示したいと考えています。セッションを使用してテーブルにユーザデータを表示するには

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

今は 2データベース内のこれらの値を保存したいが、表示値INAテーブルexmple S.NO NAME MOBILE

以下 1ヨーゲッシュ9717797354いけBHASKAR 9898225441 3 ANIKESH 9594474557 4アビシェーク9854774144

ここでは、セッションを使用してdatabseに保存せずに、以下のように各名& mobile inoutoutを表に表示します。私は試してみましたが、既存の値を更新するだけで、単一の値だけを置くことができます。 これが垂れ使用して達成することができるか?? ?? O

答えて

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

それは値を持つテーブルを描画するためにforループを使用しています助けてください。変数を必要なセッション変数に置き換え、forループを変更して終了したいときにのみループするようにする必要があります。

1

セッションはデータの配列を格納できるため、

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

+0

であるだけでなく、私は、その提出にS.Noを示してもwanto自動でなければならないようにユーザーが追加したときのように増分すると1,2,3,4,5などのようにテーブルが増えます –

+0

@YogeshRaghav多次元配列を使用して2つの異なるアプローチでポストを更新しました(大きくなると乱雑になります)、2回目は、ループが実行されるたびに変数を「1」増加させるより簡単な解決策です(記憶操作は不要です)。 –

+0

また、私はそれを使用することができますarray_merge..like suppos eif私は、既存のものを使用して横に名前&モバイルいいえとして投稿された新しい入力に基づいて新しいテーブルを作成したい...私は、それが現在のものに追加されるはずの別のものを掲示すれば追加されました..そのような..静的なデータは使用されませんか?? –