如何在每个循环中将正确的变量与$ _SESSION数组相关联?

时间:2016-05-26 16:50:37

标签: php codeigniter

我有这个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>";

3 个答案:

答案 0 :(得分:0)

您已为foreach循环中的每个会话变量创建单独的数组

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

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

// code

}

答案 1 :(得分: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);

答案 2 :(得分:0)

这是使用$ _SESSION的控制器:

[assembly: ExportRenderer(typeof(LoginView), typeof(LoginViewRenderer))]
namespace ShowsCalendar.Droid.ViewRenderer
{
public class LoginViewRenderer : PageRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        base.OnElementChanged(e);

        var context = Forms.Context;

        var baseAddress = ConfigHelper.TraktAPIURL;
        var auth = new OAuth2Authenticator(
            clientId: ConfigHelper.ClientID,
            redirectUrl: new Uri("urn:ietf:wg:oauth:2.0:oob"),
            scope: "",
            authorizeUrl: new Uri(baseAddress + "/oauth/authorize?response_type=code")
            );
        auth.AllowCancel = true;
        auth.Completed += AuthenticateCompleted;            
        var intent = auth.GetUI(context);
        context.StartActivity(intent);
    }

    private void AuthenticateCompleted(object sender, AuthenticatorCompletedEventArgs e)
    {
        if (!e.IsAuthenticated)
        {
            return;                
        }
        App.AccessToken = e.Account.Properties["access_token"].ToString();
        AccountStore.Create().Save(e.Account, "Trakt");
    }
}