duang8642 2016-02-16 08:48 采纳率: 0%
浏览 56
已采纳

如何通过关联数组中的键访问值

I have made a php template. The way I use the template in the code of the index.php (homepage) is this:

<?php
                                require 'scripts/back_end/views/country_select.php';
                                require 'scripts/back_end/views/view.php';
                                echo View::render('select_template.php');
                            ?>

Now this causes this error:

[17-Feb-2016 05:19:34 Europe/Berlin] PHP Notice:  Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 3
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Fatal error:  Cannot declare class CountrySelect, because the name is already in use in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/country_select.php on line 3

I think this is caused by require-ing country_select.php in the index.php and also in the select_template.php. I think commenting out the one in index.php is the solution to that. Here is the output html I get when the top require is commented out (refer to bottom of question to get the desired html output)

<select data-bind="options: 'options',
            optionsText: 'optionsText',
            optionsValue: 'optionsValue',
            value: value,
            optionsCaption: 'caption'"><option value="">caption</option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option>
        </select>

Issue when commenting out the top require:

I'm trying to access the values of the options array in this class:

    <?php

class CountrySelect {

    static $template = 'select_template.php';

    public static function display() {

        if ( class_exists( 'View' ) ) {

            // Get the full path to the template file.
            $templatePath = dirname( __FILE__ ) . static::$template;

            $viewData = array(
                "options" => '_countries',
                "optionsText" => 'name',
                "optionsValue" => 'geonameId',
                "value" => 'selectedCountry',
                "caption" => 'Country'
            );

            // Return the rendered HTML
            return View::render( $templatePath, $viewData );

        }
        else {
            return "You are trying to render a template, but we can't find the View Class";
        }
    }
}

?>

I'm getting these errors in the PHP console.

[17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 3 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant options - assumed 'options' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant optionsText - assumed 'optionsText' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant optionsValue - assumed 'optionsValue' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant value - assumed 'value' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant caption - assumed 'caption' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 15 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 15

The template that accesses the array:

    <?php

print_r($options);

include 'country_select.php';

?>
<div class="form-group col-sm-6">
    <div class="select">
        <span class="arr"></span>
        <select data-bind="options: '<? echo $options.options ?>',
            optionsText: '<? echo $options.optionsText ?>',
            optionsValue: '<? echo $options.optionsValue ?>',
            value: <? echo $options.value ?>,
            optionsCaption: '<? echo $options.caption ?>'">
        </select>
    </div>
</div>

What is the correct way to access the values of the associative array by key?

Here's the view.php file that has the render function:

    <?php

/** View.php **/

class View {

    /**
     * -------------------------------------
     * Render a Template.
     * -------------------------------------
     * 
     * @param $filePath - include path to the template.
     * @param null $viewData - any data to be used within the template.
     * @return string - 
     * 
     */
    public static function render( $filePath, $viewData = null ) {

        // Was any data sent through?
        ( $viewData ) ? extract( $viewData ) : null;

        print_r($viewData);

        ob_start();
        include ( $filePath );
        $template = ob_get_contents();
        ob_end_clean();

        return $template;
    }
}
?>

I am using this tutorial

I want my literal html template to be like this

<div class="form-group col-sm-6">
        <div class="select">
            <span class="arr"></span>
            <select data-bind="options: _regions,
                optionsText: 'name',
                optionsValue: 'geonameId',
                value: selectedCountry,
                optionsCaption: 'Country'">
            </select>
        </div>
    </div>
  • 写回答

1条回答 默认 最新

  • doulu8537 2016-02-16 08:50
    关注

    Edit: The question has been heavily modified so answering completely from the start.

    1. Index.php

    You CountrySelect class is already using the select_template.php

    So your code in index.php should be

    <?php
        require 'scripts/back_end/views/country_select.php';
        require 'scripts/back_end/views/view.php';
        echo CountrySelect::display();
    
    
    ?>
    
    1. Your Template :

    Your CountrySelect class is already including the template for you, also you are passing your $viewData as

    $viewData = array(
                    "options" => '_countries',
                    "optionsText" => 'name',
                    "optionsValue" => 'geonameId',
                    "value" => 'selectedCountry',
                    "caption" => 'Country'
                );
    

    which is exported into the View::render() scope using extract() like this

    ( $viewData ) ? extract( $viewData ) : null;
    

    which will create every key of $viewData to be a variable name, so now once extract is done, you will have 5 variables $options, $optionsText, $optionsValue, $value, $caption

    so your final template should be like

    <div class="form-group col-sm-6">
        <div class="select">
            <span class="arr"></span>
            <select data-bind="options: '<?php echo $options ?>',
                optionsText: '<?php echo $optionsText ?>',
                optionsValue: '<?php echo $optionsValue ?>',
                value: <?php echo $value ?>,
                optionsCaption: '<?php echo $caption ?>'">
            </select>
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)