Welcome Guest, Not a member yet? Register   Sign In
Passing variables question
#1

[eluser]ChrisF79[/eluser]
I'm learning CodeIgniter and have a basic question regarding passing variables around. Basically, I want to query my database in my property_model to get one property for sale. That part works. But, I want to then build the page's <title> tag using the information from the query. Where do I actually do the building of the title and how do I go about doing it? Any help is greatly appreciated!

<b>SITE</b>
Code:
function property() {
  
  $this->load->model('property_model');
  
  $data['mls_listing_id'] = substr($this->uri->segment(3,0), 0, 9);
    
  if ($data['mls_listing_id'] != 0) {
   $data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
  
   $this->load->view('templates/header', $data);
   $this->load->view('property', $data);
   $this->load->view('templates/footer', $data);
  }
  
  else {
   echo 'PROPERTY NOT FOUND!';
  }
  
}


<b>MODEL</b>
Code:
/*********************************************************************/
/* getOneProperty
/* Returns just one property to display for the property listing page
**********************************************************************/
function getOneProperty($mls_listing_id) {
  $q = $this->db->query('SELECT
       MLS_LISTING_ID,
       MLS_OFFICE_NAME,
       PROPERTY_TYPE_CODE,
       PROP_TYPE_DESCRIPTION,
       REMARKS,
       STATUS_CODE,
       SALE_PRICE,
       SOLD_PRICE,
       STREET_NUMBER,
       STREET_NAME,
       STREET_TYPE,
       STREET_DIRECTION,
       UNIT_NUMBER,
       LONGITUDE,
       LATITUDE,
       CITY,
       ZIP_CODE,
       COUNTY,
       SUBDIVISION,
       COMMUNITY_NAME,
       YEAR_BUILT,
       ACRES,
       BUILDING_SQUARE_FOOTAGE,
       BEDROOMS,
       BATHS_TOTAL,
       BATHS_HALF,
       BATHS_THREE_QUARTER,
       HOA_FEES,
       HOA_FREQUENCY,
       TAXES,
       TAX_YEAR,
       MASTER_BED,
       BED2,
       BED3,
       BED4,
       BED5,
       KITCHEN,
       BREAKFAST,
       LAUNDRY,
       DEN,
       DINING,
       FAMILY,
       LIVING,
       GREAT,
       FEATURE_CODES,
       PHOTO_QUANTITY,
       AVM,
       BLOGGING

       FROM listings WHERE MLS_LISTING_ID = ' . $mls_listing_id);
  
  if($q->num_rows() > 0) {
   foreach ($q->result() as $row) {
    $data[] = $row;
   }
  }
  
  $q->free_result();
  return $data;

}
#2

[eluser]TWP Marketing[/eluser]
[quote author="ChrisF79" date="1345403304"]I'm learning CodeIgniter and have a basic question regarding passing variables around. Basically, I want to query my database in my property_model to get one property for sale. That part works. But, I want to then build the page's &lt;title&gt; tag using the information from the query. Where do I actually do the building of the title and how do I go about doing it? Any help is greatly appreciated!


add a line in the controller to load the title var:


<b>SITE</b>
Code:
function property() {
  
  $this->load->model('property_model');
  
  $data['mls_listing_id'] = substr($this->uri->segment(3,0), 0, 9);
    
  if ($data['mls_listing_id'] != 0) {
   $data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
  
   $data['title'] = 'This is the title of the page'; // add this and use the correct title text

   $this->load->view('templates/header', $data);
   $this->load->view('property', $data);
   $this->load->view('templates/footer', $data);
  }
  
  else {
   echo 'PROPERTY NOT FOUND!';
  }
  
}
In your templates/header file, echo the title var
Code:
...
&lt;meta name="title" content="&lt;?php echo $title;?&gt;" /&gt;
...
#3

[eluser]ChrisF79[/eluser]
The part where I pass the variable to my view is fine. The part I can't figure out is how to use the results of the query in the title of the page.
#4

[eluser]TWP Marketing[/eluser]
The query was parsed into an array of rows an that was put into $data['records']

That array will be exploded by CI and made available to the view as the array: $records

Before you call the view, you have all those rows of data in $data['records'].

I don't know which piece of data you want to put into the header meta 'title'.

I suggest that the information is not in $data['records'], since the title is just a text about the page.

If your db is a set of MLS listings, then you can compose the title text yourself, for instance:

Code:
&lt;meta name='title' content='MLS Listing of Houses' /&gt;

If you want to get more specific, then the title needs information such as the 'MLS Listing id' which was used in the WHERE clause of the query string.

Can you tell me what, exactly, you want to be in the meta title content?
#5

[eluser]Samus[/eluser]
[quote author="ChrisF79" date="1345410688"]The part where I pass the variable to my view is fine. The part I can't figure out is how to use the results of the query in the title of the page.[/quote]
Code:
$data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
Does this not return the result object from your query?

All you have to do in your view is something like this:

Code:
$records->fieldname.
Because you've passed the result of your query into $records. So you could end up doing something like this in your header view:

Code:
...
&lt;title&gt;&lt;?= $records->BEDROOMS; ?&gt; bedroom house&lt;/title&gt;
...

You get the jist.
#6

[eluser]ChrisF79[/eluser]
[quote author="Samus" date="1345411922"]
All you have to do in your view is something like this:

Code:
$records->fieldname.

You get the jist.[/quote]

Now we're onto something. In my view, I have a simple print_r($records) that spits out the data. It is getting passed through just fine.

However, I tried updating my controller to the following and it didn't work:

Code:
function property() {
  
  $this->load->model('property_model');
  
  $data['mls_listing_id'] = substr($this->uri->segment(3,0), 0, 9);
    
  if ($data['mls_listing_id'] != 0) {
   $data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
  
   $data['title'] = $records->SUBDIVISION; // add this and use the correct title text
  
   $this->load->view('templates/header', $data);
   $this->load->view('property', $data);
   $this->load->view('templates/footer', $data);
  }
  
  else {
   echo 'PROPERTY NOT FOUND!';
  }
  
}

That's not working out. You can see what I'm trying to do at http://listingnaples.com/site/property/2...s-fl-34108
#7

[eluser]Samus[/eluser]
[quote author="ChrisF79" date="1345413301"][quote author="Samus" date="1345411922"]
All you have to do in your view is something like this:

Code:
$records->fieldname.

You get the jist.[/quote]

Now we're onto something. In my view, I have a simple print_r($records) that spits out the data. It is getting passed through just fine.

However, I tried updating my controller to the following and it didn't work:

Code:
function property() {
  
  $this->load->model('property_model');
  
  $data['mls_listing_id'] = substr($this->uri->segment(3,0), 0, 9);
    
  if ($data['mls_listing_id'] != 0) {
   $data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
  
   $data['title'] = $records->SUBDIVISION; // add this and use the correct title text
  
   $this->load->view('templates/header', $data);
   $this->load->view('property', $data);
   $this->load->view('templates/footer', $data);
  }
  
  else {
   echo 'PROPERTY NOT FOUND!';
  }
  
}

That's not working out. You can see what I'm trying to do at http://listingnaples.com/site/property/2...s-fl-34108[/quote]
Because '$records' isn't defined yet.

It gets defined when it's exploded in your view. So use it in your view.
#8

[eluser]TWP Marketing[/eluser]
[quote author="ChrisF79" date="1345413301"]
Code:
function property() {
  
  $this->load->model('property_model');
  
  $data['mls_listing_id'] = substr($this->uri->segment(3,0), 0, 9);
    
  if ($data['mls_listing_id'] != 0) {
   $data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
  
   $data['title'] = $data['records']['SUBDIVISION']; // Read $data['records'] array here
  
   $this->load->view('templates/header', $data);
   $this->load->view('property', $data);
   $this->load->view('templates/footer', $data);
  }
  
  else {
   echo 'PROPERTY NOT FOUND!';
  }
  
}
[/quote]
Look at the new line to see how to access the $data array inside the controller, BEFORE it is sent to the view and exploded (see php.net for the explode function).




Theme © iAndrew 2016 - Forum software by © MyBB