【问题标题】:How to apply style to a PHP session variable that I'll echo if ISSET is true如果 ISSET 为真,如何将样式应用于我将回显的 PHP 会话变量
【发布时间】:2014-10-14 05:36:11
【问题描述】:

我认为有这段代码:

<div>
    <?php
        if(ISSET($message))
            echo $message;
    ?>
</div>

对我来说很好用。除了,当我尝试以下列方式编辑此 div 时,将样式应用于$message

<div class="msg done">
    <?php
        if(ISSET($message))
            echo $message;
    ?>
</div>

即使在我的会话过期之后,或者即使 ISSET($message) 为 FALSE,该样式仍然保留在我的视图页面中。基本上,我希望仅在 ISSET($message) 为 TRUE 且显示 $message 时应用此样式,否则不应用。

在我的视图页面中以以下方式提到了对这种样式的引用:

<link rel="stylesheet" media="screen,projection" type="text/css" href="css/main.css" />

在 main.css 中,样式在这里完成:

.msg.done {background:url("../design/ico-done.gif") 10px 50% no-repeat;}

鉴于ISSET($message) 为TRUE,我如何仅将样式应用于$message?我认为我的问题是:无论ISSET($message) 值如何,我的整个 div 都是样式化的。这就是为什么这种风格一直存在的原因。无论如何我可以解决这个问题吗?

编辑 - 1:

我忘了说,这是一个 CodeIgniter 应用程序。我的 $message 变量是一个会话变量,它来自我的控制器 - employees.php。下面给出了我的整个控制器代码,也许那里有问题:

 <?php
 if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
class Employees extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    //$this->load->helper('url');
    $this->load->model('employee_model');
    $this->load->library('pagination');
    $this->load->library('session');
  }

  //Show all employees
  public function index()
  {
      //$data['employee_list'] = $this->employee_model->get_all_employees();

      //pagination codes
      $config = array();
      //$config['base_url'] = base_url().'admin_logins/employee2';
      //$config['base_url'] = base_url('admin_logins/employee2');
      $config['base_url'] = site_url('admin_logins/employee2');
      $config['total_rows'] = $this->employee_model->record_count();
      $config['per_page'] = 10;
      $config["uri_segment"] = 3;
      $choice = $config["total_rows"] / $config["per_page"];
      $config["num_links"] = round($choice);


      $this->pagination->initialize($config);

      $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
      $data["results"] = $this->employee_model->
        fetch_employees($config["per_page"], $page);
      $data["message"] = $this->session->flashdata('message');
      $data["links"] = $this->pagination->create_links();

      $this->load->view('/admin_logins/employee_list', $data);

  }


  //Insert an employee
  public function insert_employee_db()
  {

      $edata['emp_id'] = $this->input->post('emp_id');
      $edata['emp_name'] = $this->input->post('emp_name');
      $edata['emp_mobile_no'] = $this->input->post('emp_mobile_no');
      $res = $this->employee_model->insert_employee($edata);
      if($res)
      {
          $this->session->set_flashdata('message','Employee added successfully');
          header('location:'.base_url()."index.php/employees/".$this->index());
      }
  }

  //Edit an existing employee
  public function edit_employee_db()
  {
      $edata['emp_id']=$_POST['emp_id'];
      $edata['emp_name']=$_POST['emp_name'];
      $edata['emp_mobile_no']=$_POST['emp_mobile_no'];
      $res=$this->employee_model->edit_employee($edata, $_POST['emp_id']);
      if($res)
      {
          $this->session->set_flashdata('message','Employee edited successfully');
          header('location:'.base_url()."index.php/employees/".$this->index());
      }
  } 
}
?>

编辑 - 2:

我最近的尝试:

<?php
    if(isset($message)){
?>
<p class= "msg done">
    <?php 
        echo $message;
        unset($message);
    ?>
</p>
<?php
    }
    else {

    }
?>

但是,还是没有运气。

编辑 - 3:

我按以下方式编辑了我的代码:

<?php
    if(ISSET($message))
        echo $message;
?>

现在运行良好,会话到期后消息消失。

另外,如果我按以下方式编辑它:

<div style = "color: red">
    <?php
    if(ISSET($message))
        echo $message;
    ?>    
</div>

它工作正常,并且样式和消息都会在会话到期时消失。

但是每当我通过外部 css 文件 main.css 将样式应用于我的 div 时,就会遇到问题。

【问题讨论】:

  • 亲爱的尝试我的解决方案,如果它不起作用我想删除它。谢谢,让我知道。它基于您原来的问题。谢谢

标签: php html css codeigniter


【解决方案1】:
    <?php
        if(isset($message)){
    ?>
          <div class="msg done"><?php echo $message ?></div>
    <?php
        }
        else{
   ?>
          <div class="nothing">Nothing</div>
  <?php
        }
    ?>

【讨论】:

  • 同样的问题,伙计。消息消失了,但样式仍然存在。
  • 你在样式显示的任何地方都使用过吗:无; ?
【解决方案2】:

@Crescent Moon,请在您的应用程序中使用以下代码。

<?php $style = (isset($message)) ? 'msg done' : ''; ?>

<div class="<?php echo $style; ?>">
    <?php
        //Print $message & unset appropriate session variable.
        if(isset($message)) { 
            echo $message;
            unset($message);
        }
    ?>
</div>

因此,按照上述代码样式,仅应用 $message 设置,否则您的样式不会应用于 div。

如果您还有任何问题,请告诉我。

【讨论】:

  • 不行,样式还在。注意 $message 是一个会话变量。
  • @Crescent Moon,打印会话变量后,您需要在 echo $messege 之后立即取消设置该变量;所以会话变量未设置,样式也从页面中消失。可以试试吗?
  • echo($message)之后,我添加了unset($message),但风格仍然存在。
  • @Crescent Moon,我已经按照您的期望编辑了代码,您可以试试上面的代码吗?如果它仍然不起作用,那么在打印消息后打印您的会话,可能是会话没有在您结束时销毁!
  • 我以为 CodeIgniter session->flashdata 应该在每次浏览器请求后销毁?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
相关资源
最近更新 更多