Welcome Guest, Not a member yet? Register   Sign In
Error in displaying result from database
#1

[eluser]kalpesh[/eluser]
Hi,
I try to display data in views but it doesn't give me the what i want it.
In my controller
Code:
if($this->input->post('mysubmit'))
  {
      $data['city']=$this->input->post('City');
       $data['BrandDetail']=$this->Brand_Model->SelectBrandByCity($data['city']);
  }

  $this->load->view('Mobile_Offer_Template', $data);

In my model
Code:
function SelectBrandByCity($city)
      {
          $this->load->database();
        $this->db->select('Detail','Pro_Id');
        $this->db->where('City',$city);
        $this->db->limit(3);
        $query = $this->db->get('product_city');
        $count='0';
            foreach ($query->result() as $row)
        {
          $data[$row->Detail]= $row->Detail;
                  $data[$row->Pro_Id]= $row->Pro_Id;
          $count++;
        }
        return $data;
      }
In my view:
Code:
foreach($BrandDetail as $row) {
    ?>
<div id="Offer" class="selectcity">
<img name="" src="" width="40" height="20" alt="" />&nbsp;&lt;?php echo $row['Detail'];?&gt;:&lt;?php echo $row['Pro_Id'];?&gt;
</a></div>
&lt;?php  } ?&gt;

when i try to print row it is giving me this error:
Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: stdClass::$Pro_Id

Filename: models/Brand_Model.php

Line Number: 23
#2

[eluser]jalalski[/eluser]
Well, the error is telling you that there is no such field (property) as Pro_Id in the result set being returned. I'd check that it exists... maybe a 'print_r($row)' at the right point???
#3

[eluser]TheFuzzy0ne[/eluser]
That happens when you try to call on the property of an object that doesn't exist. It's a sign that you don't have any results to display. I'd suggest you use:
Code:
if ($query->num_rows()) {

To confirm that you have rows before they exist. This is why I prefer using result_array instead. It seems more forgiving that using an object.
#4

[eluser]kalpesh[/eluser]
I try to what u people say.
it still giving me that error.
And also giving this
Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: stdClass::$Pro_Id

Filename: models/Brand_Model.php

Line Number: 25


Hyderabad
:
s:s
I:I
#5

[eluser]TheFuzzy0ne[/eluser]
Please post your code as it is now. If you're checking for result rows, and none exist, then that line of code should never run.
#6

[eluser]kalpesh[/eluser]
Here is my Controller:
Code:
&lt;?php
include_once("Base.php");
class Offer extends Base {
  function index() {
    $this->Offer_list();
  }
  function Offer_list() {
  $this->load->helper('form');
  $this->load->model('brand');
  $Base=$this->Base_list();
  $data['style']=$Base['style'];
  $data['base']=$Base['base'];
  $data['images']=$Base['images'];
  if($this->input->post('mysubmit'))
  {
         $data['city']=$this->input->post('City');
       $data['BrandDetail']=$this->brand->SelectBrandByCity($data['city']);
  }

  $this->load->view('Offer_Template', $data);
  }
}
?&gt;

this is my Model
Code:
&lt;?php
    class brand extends Model{

      function brand(){
        parent::Model();
        $this->load->helper('url');
      }
      function general(){
        $data['SelectCity']      = 'Select City';
        return $data;
      }
      function SelectBrandByCity($city)
      {
          $this->load->database();
        $this->db->select('Detail','Pro_Id');
        $this->db->where('City',$city);
        $this->db->limit(3);
        $query = $this->db->get('product_city');
        $count='0';
        if($query->num_rows())
        {
        foreach ($query->result() as $row)
        {
          $data[$row->Detail]= $row->Detail;
                 $data[$row->Pro_Id]= $row->Pro_Id;
          $count++;
        }
        }
        return $data;
      }
    }
    ?&gt;

This is my view file:
Code:
<!DOCTYPE html PUBLIC '-//WAPFORUM//DTD XHTML Mobile 1.0//EN' 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd'>
&lt;html &gt;
&lt;head profile="http://gmpg.org/xfn/11"&gt;
&lt;meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /&gt;
&lt;title&gt;Offer&lt;/title&gt;
&lt;link media="all" href="&lt;?php echo "$base"."$style";?&gt;" type="text/css" rel="stylesheet" /&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="Header">
&lt;?php  $this->load->view('Header'); ?&gt;
</div>
</br>
<div id="Main Section" width="200px;" style="">
<div id="Section" class="selectcity">&lt;?php echo $city;?&gt; </div>
<div id="Offers Section">
&lt;?php
foreach($BrandDetail as $row) {
    ?&gt;
<div id="Offer" class="selectcity"><img name="" src="" width="40" height="20" alt="" />&nbsp;&lt;?php echo $row['Detail'];?&gt;:&lt;?php echo $row['Pro_Id'];?&gt;
</a></div>
&lt;?php   } ?&gt;
</div>
</br>
&lt;/body&gt;
&lt;/html&gt;

Can there is some way to get two or more columns from database table from model to controller ?
#7

[eluser]TheFuzzy0ne[/eluser]
The syntax for your select statement is incorrect.
Code:
$this->db->select('Detail','Pro_Id'); # Two parameters.

# Should be...

$this->db->select('Detail, Pro_Id'); # One parameter.

The second parameter can be set to FALSE, which prevents the string from being escaped (it's TRUE by default), but you don't need that for your query.

The select statement as it was was only selecting 'Detail', not 'Pro_Id'.

Another way to achieve the same thing:
Code:
$this->db->select('Detail');
$this->db->select('Pro_Id');

Sorry I didn't notice this earlier.
#8

[eluser]kalpesh[/eluser]
Hi,that error gone.
but now it is given me output like this.
Code:
Hyderabad Offers

A PHP Error was encountered
Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/Offer_Template.php

Line Number: 21


1:1
s:s
2:2
I:I
1:1

Pro_Id and Detail is not printing.
It should be display like this.
50% Off : 4

why i am not getting the data from database




Theme © iAndrew 2016 - Forum software by © MyBB