如何使用锚标记为变量赋值

时间:2018-02-21 13:39:46

标签: php jquery codeigniter

我正在尝试使用锚标记为codeigniter视图中的变量赋值,因此当用户单击特定的锚标记时,将分配一个值,但不会发生。

   <?php
                            $view_set = $this->session->userdata('view');

                            $gruid = '';

                            ?>

                            <label>View</label>
                            <a href="#"
                               class="view_set <?= ($view_set == 'gird') ? 'grid-list-ative' : ''; ?>"
                               data-view="gird" id="gird" onclick="<?php $gruid == 'grid' ?>">
                                <i class="fa fa-th-large" aria-hidden="true"></i></a>

                            <a href="#"
                               class="view_set <?= ($view_set == 'list') ? 'grid-list-ative' : ''; ?>"
                               data-view="list" id="list" onclick="<?php $gruid = 'listt'; ?>"><i
                                        class="fa fa-list-ul" aria-hidden="true"></i></a>


                            <?php

                            if ($gruid == null || !isset($gruid) || $gruid == 'grid') {

                                echo "i am in grid mode";

                                if (@$_GET['categories'] == '1' && @$_GET['search'] == NULL || @$_GET['categories'] == '2' && @$_GET['search'] == NULL || @$_GET['categories'] == '3' && @$_GET['search'] == NULL || @$_GET['categories'] == '5' && @$_GET['search'] == NULL || @$_GET['categories'] == '6' && @$_GET['search'] == NULL || @$_GET['categories'] == '1129' && @$_GET['search'] == NULL) {

                                    $view_set = 'gird';

                                }
                            } elseif ($gruid == 'listt') {
                                echo "i am in list mode";

                                $view_set = 'list';
                            } else {

                                echo "i am in last stage";
                                $view_set = 'list';
                            }

                            ?>

1 个答案:

答案 0 :(得分:0)

不能使用onClick(JavaScript函数)将值赋值给PHP变量,但可以通过$_GET方法捕获和设置URL参数。

如果您使用 Codeigniter ,则必须先加载CodeIgniter URL Helper Library

  

$这 - &GT;负载&GT;辅助(&#39; URL&#39);

尝试以下,

<?php


if(isset($_GET['view_set']) && $_GET['view_set'] == 'list') {
    $view_set = "list";
    echo "i am in list mode";
} else {
    $view_set = "grid";
    echo "i am in grid mode";

    if (@$_GET['categories'] == '1' && @$_GET['search'] == NULL || @$_GET['categories'] == '2' && @$_GET['search'] == NULL || @$_GET['categories'] == '3' && @$_GET['search'] == NULL || @$_GET['categories'] == '5' && @$_GET['search'] == NULL || @$_GET['categories'] == '6' && @$_GET['search'] == NULL || @$_GET['categories'] == '1129' && @$_GET['search'] == NULL) {
        $view_set = 'gird';
    }
}
?>

<label>View</label>
<a href="<?php echo current_url() ?>?view_set=grid" class="view_set <?= ($view_set == 'gird') ? 'grid-list-ative' : ''; ?>" data-view="gird" id="gird" >
    <i class="fa fa-th-large" aria-hidden="true"></i>
</a>
<a href="<?php echo current_url() ?>?view_set=list" class="view_set <?= ($view_set == 'list') ? 'grid-list-ative' : ''; ?>" data-view="list" id="list" >
    <i class="fa fa-list-ul" aria-hidden="true"></i>
</a>