【问题标题】:Dynamic footer content CodeIgniter动态页脚内容 CodeIgniter
【发布时间】:2016-08-14 18:48:24
【问题描述】:

在我看来,我有一个时事通讯注册。我希望此注册框和链接仅在

时可见

a) 用户未登录

b) 用户已登录但尚未订阅

我的视图代码如下所示:

        <div id="footer">
            <div class="bottom clearfix">
                <div class="container">
                    <div class="row">
                        <div class="col-lg-4 sitemap clearfix">
                            <h4>Hulp nodig?</h4>
                            <a href="#">Wat is</a>
                            <a href="#">Veelgestelde vragen</a>
                            <a href="#">Hoe werkt het?</a>
                            <a href="#">Contacteer ons</a>
                        </div>
                        <div class="col-lg-4 follow clearfix">
                            <h4>Follow us at</h4>
                            <a href="https://www.facebook.com" target="_blank" class="facebook"><i class="fa fa-facebook-square" aria-hidden="true"></i></a>
                            <a href="" target="_blank" class="twitter"><i class="fa fa-twitter-square" aria-hidden="true"></i></a>
                            <a href="https://www.youtube.com" target="_blank" class="youtube"><i class="fa fa-youtube-square" aria-hidden="true"></i></a>
                        </div>
                        <div class="col-lg-4 newsletter clearfix">
                            <h4>Subscribe to newsletter</h4>
                            <p>
                                <input type="text" id="mail" placeholder="Mail" />
                                <a href="#" class="btn btn-newsletter">Send</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

现在我们有几个控制器文件(Login、Register、User、Dashboard、Home...),它们都有这样的方法:

public function index()
    {
        $this->is_logged_in();

        // Passing Variables
        $data['title'] = '';
        $data['class'] = 'home';

        // Template declaration
        $partials = array('head' => '_master/header/head_home', 'navigation' => '_master/header/navigation', 'banner' => 'home/home_banner', 'filter' => 'home/home_filter', 'features' => 'home/home_features', 'content' => 'home/home_content', 'footer' => '_master/footer/footer');
        $this->template->load('_master/master', $partials, $data);
    }

我需要的是调用我自己定义的方法来检查我的数据库是否用户已经订阅了时事通讯。并基于该布尔结果显示输入字段与否。

如何以最好的方式解决这个问题?

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    有几种方法,虽然我不知道最好的方法,但我会给你一个解决方案。

    我认为最好和最简单的方法是使用会话。

    在你的登录函数/脚本中,设置一个额外的参数来检查订阅是否与用户登录状态相同。根据这个“$this->is_logged_in();”我希望你已经有一个参数来检查登录状态。

    我假设您正在使用 codeigniter 本地库,如果您没有,请按照外部 3rd 方方式或您自己的方式进行操作。

    在您的登录脚本中,

    // db script goes here for checking the user has been subscribed or not.
    // according to the subscription status, the variable $subscription_status could be 0 or 1 (or wahtever you want)
    // subscribed = 1, not = 0
    
    $this->session->set_userdata('subscription' => $subscription_status);
    

    在你的 CSS 中,

    .display_none
    {
        display: none;
    }
    

    在你看来,

    <div id="footer">
        <div class="bottom clearfix">
            <div class="container">
    
                <div id="subscription_box" class="row <?php echo empty($this->session->userdata('subscription_status')) || $this->session->userdata('subscription_status') !== 1? '' : 'display_none' ?>">
                    <div class="col-lg-4 sitemap clearfix">
                        <h4>Hulp nodig?</h4>
                        <a href="#">Wat is</a>
                        <a href="#">Veelgestelde vragen</a>
                        <a href="#">Hoe werkt het?</a>
                        <a href="#">Contacteer ons</a>
                    </div>
                    <div class="col-lg-4 follow clearfix">
                        <h4>Follow us at</h4>
                        <a href="https://www.facebook.com" target="_blank" class="facebook"><i class="fa fa-facebook-square" aria-hidden="true"></i></a>
                        <a href="" target="_blank" class="twitter"><i class="fa fa-twitter-square" aria-hidden="true"></i></a>
                        <a href="https://www.youtube.com" target="_blank" class="youtube"><i class="fa fa-youtube-square" aria-hidden="true"></i></a>
                    </div>
                    <div class="col-lg-4 newsletter clearfix">
                        <h4>Subscribe to newsletter</h4>
                        <p>
                            <input type="text" id="mail" placeholder="Mail" />
                            <a href="#" class="btn btn-newsletter">Send</a>
                        </p>
                    </div>
                </div>  
    
            </div>
        </div>
    </div>
    

    我放置了一个名为“subscription_box”的 id,因为如果用户已订阅,那么您可以使用 AJAX 从 JavaScript 中删除类“display_none”。或任何你想要的效果。

    按照这个以更好的方式解决您的问题, CodeIgniter Session Library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多