使用变量后我不能再使用它了

时间:2016-03-24 01:33:25

标签: php

出于某种原因,当我创建一个变量并使用它时我不能再次使用它,我将它用于foreach,然后在结束foreach之后使用var dump并将其显示为空变量

我如何使用变量

<div class="col-md-3 col-sm-4" style="margin-bottom: 25px;">
    <h2><?php echo System::translate("Job listings"); ?></h2>
    <aside>
        <div class="sidebar">
            <div class="related-people">
                <?php if(Session::get('company_using') == $this->company->company_id): ?>
                    <div class="pull-right showHideOptions" style="font-size:12px;">
                        <a href="<?php echo config::get('URL'); ?>company#create" style="color:#127cc3;"><span class="profileIcon"><i class="fa fa-cog"></i></span></a>                                 </div>
                <?php endif; ?>

                <ul>

                    <?php if(empty($this->jobs)): ?>
                        <li><?php echo System::translate("No jobs to show"); ?></li>
                    <?php endif; ?>

            <?php foreach(array_splice($this->jobs, 0,5) as $job): ?>
                <li>
                    <div class="text-box"> 
                        <strong class="name">
                            <a href="<?php echo config::get('URL'); ?>jobs/view/<?php echo System::escape($job->job_id); ?>">
                                <?php echo System::escape(ucwords($job->job_name)); ?>
                            </a>
                        </strong> 
                            <span><i class="fa fa-map-marker"></i>
                                <?php echo System::escape($job->job_country); ?>
                            </span> 
                            <span><i class="fa fa-map-marker"></i>
                                <?php if(strlen($job->job_description) > '85'): ?>
                                    <?php echo substr(System::escape($job->job_description),0,85); ?>...
                                <?php else: ?>
                                    <?php echo substr(System::escape($job->job_description),0,85); ?>
                                <?php endif; ?> 
                            </span> 
                    </div>
                </li>
            <?php endforeach; ?>
        <?php var_dump($this->jobs); if(!empty($this->jobs)): ?>
            <a class="btn btn-info col-sm-12" href="<?php echo Config::get('URL'); ?>directory/jobs/<?php echo System::escape($this->company->company_id); ?>/all" id="jobs"><?php echo System::translate("Jobs"); ?></a>
        <?php endif; ?>
        </div>
    </div>
</div>

当我使用foreach时,日期回声很好,但在使用它之后我得到了var_dump:

  

array(0){}

我如何启动变量

//jobs
$jobs = CompanyModel::project_history($company->company_id);

$this->View->renderPage('company/profile', array('jobs' => $jobs))

我如何定位renderPage

public function renderPage($filename, $data = null)
{
    if ($data) {
        foreach ($data as $key => $value) {
            $this->{$key} = $value;
        }
    }

    require Config::get('PATH_VIEW') . '_templates/header.php';

    require Config::get('PATH_VIEW') . $filename . '.php';
    require Config::get('PATH_VIEW') . '_templates/footer.php';
}

带有数据的模型(当我使用它时可以使用它但在使用它之后会变空

public static function project_history($id = null)
{
    //get the user's employment history
    $database = DatabaseFactory::getFactory()->getConnection();

    //select by session or id
    if($id == null):

        //the sql we're performing.
        $sql = "SELECT * 
            FROM   jobs 
            WHERE  job_user = ? 
                   AND job_enabled = 1
                   AND job_company = ?";


        //run the query
        $job = $database->prepare($sql);
        $job->execute(array(Session::get('id'), Session::get('company_using')));
    else:

        //the sql we're performing.
        $sql = "SELECT * 
            FROM   jobs 
            WHERE job_company = ?
                AND job_enabled = 1";


        //run the query
        $job = $database->prepare($sql);
        $job->execute(array($id));

    endif;

    //what where the results?
    return $job->fetchAll();

}

所以只是为了澄清一下,我用模型推断了我的变量,在我的html页面上我可以使用那个变量,因为某些原因它会清空,任何想法是什么导致这个?

1 个答案:

答案 0 :(得分:0)

你写道:

foreach( array_splice( $this->jobs, 0,5 ) as $job )

array_splice() 删除数组的一部分并将其替换为其他内容(并返回已删除的部分)。

通过此操作,您可以清空数组。

使用for循环:

for( $i = 0; $i < 5; $i++ )
{
    $job = $this->jobs[$i];
}

或简单的foreach(您的数组为空,因此您处理所有数组元素):

foreach( $this->jobs as $job )