用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2483|回复: 0
收起左侧

如何制作提示信息页面-求翻译成中文

[复制链接]
发表于 2014-2-11 00:17:31 | 显示全部楼层 |阅读模式

在使用codeigniter过程中,每次操作完成,我都不知道该如何显示提示信息,在谷歌搜索后,发现国外一个博客,详细讲述了他是如何制作提示信息页面的。

codeigniter: 显示提示信息 How do I display result or error messages in CodeIgniter app

Sometimes we need to display general messages or error messages to the user noticing about certain action, for example on success or failure.

Imagine you are submitting a form and then creating or updating the data in the database. You may be interested in noticing the user about the result for that action .

This is the way I use to display general messages (and error messages) in CodeIgniter application.

The result

This is how my message looks.

忙碌的松鼠-codeigniter信息提示页面


How to display the message?

Now, in order to display that information message , I do the following:

First, when submitting the form, after validating it, in the controller I call the model function that will save the entity (license in this case). I make sure that model function returns a result, and FALSE result in case of error (I usually return an array result on success with an ID element with the last inserted identifier if the result was an insert, or the just updated entity identifier if it was an update, but that is out of this article’s scope).

Controller

Imagine we are calling the following method in the controller:

php代码:

[size=1em][size=1em]
1
$result = $this->license_model->create_or_update_license($agent_id, $state, $data);



Then, we need to see if that was an error or not. Based on that result, I will use CodeIgniter’s set_flashdata function in the Session library, as follows:

php代码:

[size=1em][size=1em]
01
if ($result)  

[backcolor=rgb(248, 248, 248) !important][size=1em]
02
{  

[size=1em]
03
    $this->session->set_flashdata( 'message', array( 'title' => 'License created','content' => 'License has been saved sucessfully', 'type' => 'message' ));  

[backcolor=rgb(248, 248, 248) !important][size=1em]
04
    redirect('agent/licenses');      

[size=1em]
05
   

[backcolor=rgb(248, 248, 248) !important][size=1em]
06
} else

[size=1em]
07
{  

[backcolor=rgb(248, 248, 248) !important][size=1em]
08
    $this->session->set_flashdata( 'message', array( 'title' => 'License error','content' => 'License could not be saved', 'type' => 'error' ));  

[size=1em]
09
    redirect('agent/licenses');  

[backcolor=rgb(248, 248, 248) !important][size=1em]
10
}   



Once the Flash message has been set, I redirect the user to the form or a list of results. That is needed in order to get the flash working (you cannot just load the view in this case… well, you can but this method will not work in such case). When comparing $result TRUE or FALSE, please notice the different value for type . I am using type=message for successful messages, and type=error for error mesages.

View

In the view, just above the form, I use this snippet.

php代码:

[size=1em][size=1em]
1
<!--?= @flash_message() ?-->



To keep it simple, I just added @ to avoid any error message, but probably if you are looking for a better coding practice and you may not do that.

A Helper

Then, I put this flash message function as a helper function, so it is available when calling from the view.

php代码:

[size=1em][size=1em]
01
function flash_message()  

[backcolor=rgb(248, 248, 248) !important][size=1em]
02
{  

[size=1em]
03
    // get flash message from CI instance  

[backcolor=rgb(248, 248, 248) !important][size=1em]
04
    $ci =& get_instance();  

[size=1em]
05
    $flashmsg = $ci->session->flashdata('message');  

[backcolor=rgb(248, 248, 248) !important][size=1em]
06
   

[size=1em]
07
    $html = '';  

[backcolor=rgb(248, 248, 248) !important][size=1em]
08
    if (is_array($flashmsg))  

[size=1em]
09
    {  

[backcolor=rgb(248, 248, 248) !important][size=1em]
10
        $html = '<div id="flashmessage" class="'.$flashmsg[type].'">  

[size=1em]
11
            <img style="float: right; cursor: pointer" id="closemessage"src="'.base_url().'images/cross.png">  

[backcolor=rgb(248, 248, 248) !important][size=1em]
12
            <strong>'.$flashmsg['title'].'</strong>  

[size=1em]
13
            <p>'.$flashmsg['content'].'</p>  

[backcolor=rgb(248, 248, 248) !important][size=1em]
14
            </div>';  

[size=1em]
15
    }  

[backcolor=rgb(248, 248, 248) !important][size=1em]
16
    return $html;  

[size=1em]
17
}



jQuery part

Finally, I added a simple effect to slide down and blink the message box , and adding the close functionality with a simple jQuery function. You can do something like this. Of course, you can modify it or just avoid using the jQuery effect if it is desired.

js代码:

[size=1em][size=1em]
1
// first slide down and blink the message box  

[backcolor=rgb(248, 248, 248) !important][size=1em]
2
$("#flashmessage").animate({top: "0px"}, 1000 ).show('fast').fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);



Some CSS styling

Finally, just add some CSS styling for #flashmessage, using message or error class names.

css代码:

[size=1em][size=1em]
1
.message{  

[backcolor=rgb(248, 248, 248) !important][size=1em]
2
    border:1px solid #CCCCCC;  

[size=1em]
3
    width:300px;  

[backcolor=rgb(248, 248, 248) !important][size=1em]
4
    border:1px solid #c93;  

[size=1em]
5
    background:#ffc;  

[backcolor=rgb(248, 248, 248) !important][size=1em]
6
    padding:5px;  

[size=1em]
7
    color: #333333;  

[backcolor=rgb(248, 248, 248) !important][size=1em]
8
    margin-bottom:10px;  

[size=1em]
9
}   



Enjoy it.   来源:http://www.gretheer.com/2013/06/codeigniter-message.html







本版积分规则