【问题标题】:how assign value to a variable using anchor tag [duplicate]如何使用锚标签为变量赋值[重复]
【发布时间】:2018-02-21 13:39:45
【问题描述】:

我正在尝试使用锚标记为 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';
                            }

                            ?>

【问题讨论】:

标签: php jquery codeigniter


【解决方案1】:

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

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

$this->load->helper('url');

试试下面,

<?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>
猜你喜欢
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 2013-09-13
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多