문제

I got this error:

A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$positionsNum

when i run.

My model:

public function load_job($id){        
    $Where = array('jobs.id' => $id);
    $this->db->select('jobs.id, positionsNum, companyName, contract_type.titleEN as contractType');
    $this->db->from('jobs');
    $this->db->join('contract_type', 'jobs.contractTypeId = contract_type.id', 'left');
    $this->db->where($Where);
    $Result = $this->db->get(); 
    return $Result->first_row() ; 
} 

my view:

   <?php if(isset($job) && count($job) > 0){   ?>
   <div class="row job-detail-row">
      <div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
       <div class="col-lg-6"><a href=""><?php  if(isset($job->contractType)) echo $job->contractType; ?></a></div>
      </div>

      <div class="row job-detail-row">
        <div class="col-lg-6 job-detail-label"><?php echo lang('num_vacancies'); ?>    </div>
        <div class="col-lg-6"><?php  echo $job->positionsNum; ?></div>
      </div>
      <?php } ?>

my Controller's function:

public function job() {

    $job_id = $this->uri->segment(3);

    if(isset($job_id) && !empty($job_id))
    {
        //one job
        $job = $this->job_model->load_job($this->GetLang, $job_id);
        $data['job'] = $job;
        $this->load->view( 'front/singleJob', $data);
    }
}

Also, i printed the returned object and it returns this:

stdClass Object(
   [id] => 9
   [positionsNum] => 2
   [companyName] => 
   [contractType] => type1
  )

But, contractType won't be printed in page even if it is already has a value. I can't figure out the problem. Any help?

도움이 되었습니까?

해결책 3

So, it is solved now :) This problem happened twice in two files: First is which stated in my question, and I had an error in another query at the same page. Second problem was in the HTML file. My complete view was like this:

<?php if(isset($service) && count($service) > 0){   ?>
  <div class="row job-detail-row">
  <div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
   <div class="col-lg-6"><a href=""><?php  if(isset($service->contractType)) echo $service->contractType; ?></a></div>
  </div>
  <?php } ?>

  <ul>
  <?php  foreach ($similarServices as $service) { ?> <<========= NOTICE: $service here is the same as the above condition
   <li>           
       <div class="title"><a href="<?php echo site_url('services/service/'.$service->id) ?>"><?php echo $service->title; ?></a>                                        
        </div>
   </li>
  <?php }//end foreach similarServices ?>  
  </ul>

It appeared because i used the same variable name $service, so i changed it and it works now. Hope this helps you.

다른 팁

The problem is here. you have a function with one parameter.

public function load_job($id);

but in controller you are passing two values. Try to pass correct values.

$job = $this->job_model->load_job($this->GetLang, $job_id);

it should be like this.

$job = $this->job_model->load_job( $job_id);

try like this

public function job() {

    $job_id = $this->uri->segment(3);

    if(isset($job_id) && !empty($job_id))
    {
        //one job
        $data['job'] = $this->job_model->load_job($this->GetLang, $job_id);
        $this->load->view( 'front/singleJob', $data);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top