codeigniter flash消息不起作用

时间:2017-02-19 20:36:46

标签: php codeigniter message flash-message

我无法在我的项目中显示flash消息(查看文件),任何身体帮助我多次杀了我的视图页面中显示$ flash消息。我是伪造者的新手,试图从网上学到一些东西。

Controller : 
function delete_image($update_id)
    {
        if(!is_numeric($update_id))
    {
        redirect('site_security/not_allowed');
    }
    $this->load->library('session');
    $this->load->module('site_security');
    $this->site_security->_make_sure_is_admin();

    $data = $this->fetch_data_from_db($update_id);

    $picture = $data['picture'];
    $big_pic_path = './media/blog_pics/'.$picture;
    $small_picture = str_replace('.', '_thumb.', $picture);

    $small_pic_path = './media/blog_pics/'.$small_picture;

    //attempt to remove the images
    if (file_exists($big_pic_path)) {
        unlink($big_pic_path);
    }

    if (file_exists($small_pic_path)) {
        unlink($small_pic_path);
    }
    //update the database
    unset($data);
    $data['picture'] = "";
    $this->_update($update_id, $data);
    $flash_msg = "The image was successfully deleted.";
    $value = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>';
    $this->session->set_flashdata('item', $value);

    redirect('blog/create/'.$update_id);
    }

查看文件:

 <h1><?= $headline ?></h1>
<?= validation_errors("<p style='color: red;'>", "</p>") ?>
<?php
if(isset($flash)){
    echo $flash;
}
?>
<?php
if(is_numeric($update_id)) { ?>
<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="halflings-icon white edit"></i><span class="break"></span>Additional Option</h2>
            <div class="box-icon">
                <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
                <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
            </div>
        </div>
        <div class="box-content">

            <?php 
            if($picture==""){
            ?>

            <a href="<?= base_url() ?>blog/upload_image/<?= $update_id ?>"><button type="button" class="btn btn-primary">Upload Image</button></a>
            <?php
            }
            else
            {
            ?>
            <a href="<?= base_url() ?>blog/delete_image/<?= $update_id ?>"><button type="button" class="btn btn-danger">Delete  Image</button></a>
            <?php
            }

        if($update_id>2){
        ?>
            <a href="<?= base_url() ?>blog/deleteconf/<?= $update_id ?>"><button type="button" class="btn btn-danger">Delete Blog Entry</button></a>
        <?php
        }
        ?>
            <a href="<?= base_url().$page_url ?>"><button type="button" class="btn btn-default">View Blog Entry</button></a>
        </div>
    </div><!--/span-->
</div><!--/row-->
<?php
} 
?>

<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="halflings-icon white edit"></i><span class="break"></span>Blog Entry Details</h2>
            <div class="box-icon">
                <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
                <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
            </div>
        </div>
        <div class="box-content">
        <?php 
        $form_location = base_url()."blog/create/".$update_id;
        ?>
            <form class="form-horizontal" method="post" action="<?= $form_location ?>">
              <fieldset>
              <div class="control-group">
                  <label class="control-label" for="typeahead">Date Published </label>
                  <div class="controls">
                    <input type="text" name="date_published" class="input-xlarge datepicker" id="date01" value="<?= $date_published ?>">
                  </div>
                </div>
                <div class="control-group">
                  <label class="control-label" for="typeahead">Blog Entry Title </label>
                  <div class="controls">
                    <input type="text" class="span7" name="page_title" value="<?= $page_title ?>">
                  </div>
                </div>

                <div class="control-group hidden-phone">
                  <label class="control-label">Blog Entry Keywords</label>
                  <div class="controls">
                    <textarea class="span7" rows="3" name="page_keywords"><?php echo $page_keywords; ?> </textarea>
                  </div>
                </div>
                <div class="control-group hidden-phone">
                  <label class="control-label">Blog Entry Description</label>
                  <div class="controls">
                    <textarea class="span7" rows="3" name="page_description"><?php echo $page_description; ?> </textarea>
                  </div>
                </div>

                <div class="control-group hidden-phone">
                  <label class="control-label" for="textarea2">Blog Entry Content</label>
                  <div class="controls">
                    <textarea class="cleditor" id="textarea2" rows="3" name="page_content"><?php echo $page_content; ?> </textarea>
                  </div>
                </div>
                <div class="control-group">
                  <label class="control-label" for="typeahead">Author </label>
                  <div class="controls">
                    <input type="text" class="span7" name="author" value="<?= $author ?>">
                  </div>
                </div>
                <div class="form-actions">
                  <button type="submit" class="btn btn-primary" name="submit" value="Submit">Submit</button>
                  <button type="submit" name="submit" value="Cancel" class="btn">Cancel</button>
                </div>
              </fieldset>
            </form>   

        </div>
    </div><!--/span-->
</div><!--/row-->

<?php
if(isset($picture) && ($picture!=null) ){
?>
<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="halflings-icon white edit"></i><span class="break"></span>Image</h2>
            <div class="box-icon">
                <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
                <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
            </div>
        </div>
        <div class="box-content">
            <img src="<?= base_url() ?>media/blog_pics/<?= $picture ?>" class="thumbnail" width="100">
        </div>
    </div><!--/span-->
</div><!--/row-->
<?php
}
?>

3 个答案:

答案 0 :(得分:1)

如果您想获取已设置的flashdata消息

尝试

<?php if($this->session->flashdata('item')) {?>
<?php echo $this->session->flashdata('item');?>
<?php }?>

答案 1 :(得分:1)

您只需更改此代码

 <?php
if(isset($flash)){
    echo $flash;
}
?>

    <?php
if(isset($this->session->flashdata('item'))){
    echo $flash;
}
?>

答案 2 :(得分:1)

在控制器中更改

$flash_msg = "The image was successfully deleted.";
$value = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>';
$this->session->set_flashdata('item', $value);

$this->session->set_flashdata('item', 'The image was successfully deleted.');

在您的视图页面中添加

<?php if($this->session->flashdata('item')){ ?>
<div class="alert alert-success">
<h4><?php echo $this->session->flashdata('item'); ?></h4>
           </div>
          <?php } ?>