2016-01-15 19 views
1

這是表格,當用戶在選擇框中選擇一些值時生成該表格。 如何在會話中保存此生成的表格,並在每次當前用戶登錄時出現。PHP:如何保存在會話生成表中,並且每當curent用戶登錄時都會出現apear?

<table border="0" class="table table-hover table-striped" name="raspored" id="raspored" class="tablesorter"> 
    <thead> 
     <tr COLSPAN=2 BGCOLOR="#6D8FFF"> 
      <th>ИД</th> 
      <th>Предмет</th> 
      <th>Професор</th> 
      <th>Ден</th> 
      <th>Час</th> 
      <th>Просторија</th> 
      <th>Тип</th> 
     </tr> 
    </thead> 
    <?php 
    foreach ($_POST['predmet'] as $predmet) { 
     $_SESSION['predmet'] = $_POST['predmet']; 
     $rows = get_info($db, $predmet); 
     if (count($rows)){ 
      foreach ($rows as $row) {  
       $_SESSION['row'] = $rows; 
       echo "<tr>" . 
        "<td>" . $row["ID"] . "</td>" . 
        "<td>" . $row["predmet"] . "</td>" . 
        "<td>" . $row["profesor"] . "</td>" . 
        "<td>" . $row["den"] . "</td>" . 
        "<td>" . $row["chas"] . "</td>" . 
        "<td>" . $row["prostorija"] . "</td>" . 
        "<td>" . $row["tip"] . "</td>" . 
        "</tr>"; 
      } 
     } 
    } 
    ?> 
</table> 
+0

http://php.net/manual/en/session.examples.basic.php - http://php.net/manual/en/reserved.variables.session.php –

+0

你不會想要保存整張桌子,那真的沒什麼意義。你可能**想要將數據保存在表中,然後用你的PHP繪製表格。 (@ Fred-ii- - 在我經常感興趣的相同問題上看到你很高興!) –

+0

@cale_b這是你的全部屬性;-)我只是覺得我會給他們看點東西。 –

回答

1

你可以這樣做。縮進並沒有完全正確,但它仍然應該起作用。

<?php 
session_start(); //call this if you haven't already done so - you'll need to call it before any HTML is output to the page though unless you have output buffering turned on such as via http://php.net/manual/en/outcontrol.configuration.php#ini.output-buffering 
if ($_SESSION['table_stored_in_session']) { 
    echo $_SESSION['table_stored_in_session']; 
} else { 
     ob_start(); 
    ?> 
    <table border="0" class="table table-hover table-striped" name="raspored" id="raspored" class="tablesorter"> 
     <thead> 
      <tr COLSPAN=2 BGCOLOR="#6D8FFF"> 
       <th>??</th> 
       <th>???????</th> 
       <th>????????</th> 
       <th>???</th> 
       <th>???</th> 
       <th>??????????</th> 
       <th>???</th> 
      </tr> 
     </thead> 
     <?php 
     foreach ($_POST['predmet'] as $predmet) { 
      $_SESSION['predmet'] = $_POST['predmet']; 
      $rows = get_info($db, $predmet); 
      if (count($rows)){ 
       foreach ($rows as $row) {  
        $_SESSION['row'] = $rows; 
        echo "<tr>" . 
         "<td>" . $row["ID"] . "</td>" . 
         "<td>" . $row["predmet"] . "</td>" . 
         "<td>" . $row["profesor"] . "</td>" . 
         "<td>" . $row["den"] . "</td>" . 
         "<td>" . $row["chas"] . "</td>" . 
         "<td>" . $row["prostorija"] . "</td>" . 
         "<td>" . $row["tip"] . "</td>" . 
         "</tr>"; 
       } 
      } 
     } 
     ?> 
    </table> 
    <?php 
    $table = ob_get_clean(); 
    $_SESSION['table_stored_in_session'] = $table; 
    echo $table; 
} 
?> 
+0

這使會話表。但是當用戶註銷並再次登錄時,表將不起作用。 –

+0

@ValentinGjorgoski - 不是在註銷時銷燬整個會話,而是可以「取消」存儲他們登錄的用戶的$ _SESSION變量(例如他們的用戶標識等),或者如果這不是一個選項,您可以將該表存儲在共享內存中(請參閱http://php.net/manual/en/book.shmop.php)或使用某種類似www.php.net/apcu_add的擴展名 - 這是假設的該表對於每個用戶都是相同的,除非你爲每個表存儲了一個不同的表(但是如果你這樣做並且從未將表從內存中釋放)意識到內存泄漏。 – sa289

相關問題