2016-05-26 13 views
0

我有這個foreach循環,當我點擊每個表的最後一個按鈕時,我想把與表關聯的變量放在$_SESSION數組上。

我該怎麼做?

我從數據庫中獲取數據,然後將其顯示在視圖上。我從中獲取數據的表中的每一行都顯示在一個引導程序表上。問題是,在每個表的最後,我都有一個按鈕來刪除與上表中數據關聯的數據庫中的行。但是,當我點擊一個按鈕時(不管它是哪個按鈕),每次都從最後一個表中刪除數據。我能理解爲什麼。只是不能讓它刪除我想要的行。

下面是代碼:

foreach ($interesses as $linha) 
      { 
       $area = $linha["AREA"]; 
       $atuacao = $linha["GRUPO_ATUACAO"]; 
       $disponibilidade= $linha["DISPONIBILIDADE"]; 
       $aano = $linha["ALTURA_ANO"]; 

       $container = '"container"'; 
       $table = '"table"'; 

       $post = '"post"'; 

       $utf = '"utf-8"'; 

       echo "<div class=".$container.">"; 



       $_SESSION['area'] = $area; 
       $_SESSION['atuacao'] = $atuacao; 
       $_SESSION['disponibilidade'] = $disponibilidade; 
       $_SESSION['aano'] = $aano; 

       echo "<table class=".$table.">"; 
       echo "<thead>"; 
       echo "<tr>"; 
       echo "<th></th>"; 
       echo "<th></th>"; 
        echo "</tr>"; 
        echo "</thead>"; 
        echo "<tbody>"; 
        echo "<tr>"; 
        echo "<td><h4>Area de interesse<h4></td>"; 
        echo "<td><h4>".$area."</td></h4>"; 
        echo "</tr>"; 
        echo "<tr>"; 
        echo "<td><h4>Grupo de atua&ccedil;&atilde;o<h4></td>"; 
        echo "<td><h4>".$atuacao."</td></h4>"; 
        echo "</tr>"; 
        echo "<tr>"; 
        echo "<td><h4>Disponibilidade<h4></td>"; 
        echo "<td><h4>".$disponibilidade."</td></h4>"; 
        echo "</tr>"; 
        echo "<tr>"; 
        echo "<td><h4>Epoca do ano<h4></td>"; 
        echo "<td><h4>".$aano."</td></h4>"; 
        echo "</tr>"; 
        echo "</thead>"; 
        echo "</table>"; 

        $hiper = "Apagarinteresse"; 
        $button = '"io"'; 


       echo "<a href=".site_url($hiper)." class=".$button."/>Apagar interesse</a>"; 



       echo "</div>"; 

       echo "<br></br>"; 
+0

可以顯示執行刪除操作的代碼 –

+0

我認爲一個更簡單的答案是根本不使用會話,而是讓按鈕單擊返回表名和行號。 – Jerry

回答

0

你必須做單獨的數組中foreach循環的每個會話變量

foreach ($interesses as $linha) 
{ 
// code 

$_SESSION['area'][] = $area; 
$_SESSION['atuacao'][] = $atuacao; 
$_SESSION['disponibilidade'][] = $disponibilidade; 
$_SESSION['aano'][] = $aano; 

// code 

} 
0

試試這個

foreach ($interesses as $linha) 
{ 
    $area    = $linha["AREA"]; 
    $atuacao   = $linha["GRUPO_ATUACAO"]; 
    $disponibilidade = $linha["DISPONIBILIDADE"]; 
    $aano    = $linha["ALTURA_ANO"]; 

    $newdata[] = array(
     'area' => '$area', 
     'atuacao' => '$atuacao', 
     'disponibilidade' => '$disponibilidade', 
     'aano' => '$aano' 
    ); 

    # Table code 
} 

# after forloop set the session 
$this->session->set_userdata($newdata); 
+0

它不起作用。一樣。它會刪除最後一個表格數據。並即時得到這個錯誤:消息:未知:跳過數字鍵0.但是,謝謝 –

+0

讓我看看刪除代碼 –

0

這裏是使控制器使用$ _SESSION:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Apagarinteresse extends CI_Controller { 


function __construct() 
{ 
parent::__construct(); 


$this->load->library('Form_validation'); 
$this->load->helper('url'); 
$this->load->model('apagarinteresse_model'); 
$this->load->helper('form'); 
$this->load->library('session'); 
$this->load->model('perfilvoluntario_model'); 
$this->load->model('mostrainteresse_model'); 




} 

function index(){ 

$this->apaga_interesse($this->session->area, $this->session->atuacao, $this-  >session->disponibilidade, $this->session->aano); 

} 


function apaga_interesse($area, $atuacao, $disponibilidade, $aano){ 



$data['erro_login'] = ""; 




    $this->apagarinteresse_model->apaga_interesse($area, $atuacao , $disponibilidade, $aano ,$this->session->email); 

    $query = $this->mostrainteresse_model->get_interesses($this->session->email); 

    $data['interesses'] = $query->result_array(); 
    $this->load->view("view_interesses", $data); 

    echo '<script> 
      alert("Interesse apagado!Confirme abaixo."); 
     </script>'; 



    } 





    } 

?>