CodeIgniter Forums
Create a variable in my model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Create a variable in my model? (/showthread.php?tid=54719)



Create a variable in my model? - El Forum - 09-21-2012

[eluser]ChrisF79[/eluser]
This is a very remedial question I'm sure but I have the following:

Controller
Code:
$data['records'] = $this->property_model->getOneProperty($data['mls_listing_id']);
$this->load->view('property', $data);

Model
Code:
(query here)

if($q->num_rows() > 0) {
    foreach ($q->result() as $row) {
    $data[] = $row;
    }
return $data;

View
Code:
<?php foreach ($records as $r) : ?>
<th>Asking Price:</th>
<td>$&lt;?php echo number_format($r->SALE_PRICE); ?&gt;</td>

I've trimmed most of the detail away for a simple example.

My question is, the query pulls things like the street number, street name, city, state, zip, etc. I want to combine all of that into a "address" variable. How do I do it in my model so that I can use it as $r->address; in my view?


Create a variable in my model? - El Forum - 09-21-2012

[eluser]PhilTem[/eluser]
In your model, when looping over the database/query results, you would just create a new object attribute and store your info in it like e.g.

Code:
if($q->num_rows() > 0) {
  foreach ($q->result() as $row) {
    $row->address = "prepare your address here";
    $data[] = $row;
  }
return $data;

aaaand, that's it Wink


[edit]
BTW, you might want to use & # 3 6 ; (without the spaces) instead of $ to make it the correct HTML entity and display correctly on all clients' encodings Wink

[edit2]
Needed to change the HTML entity to include spaces since it's otherwise just rendered as a dollar-sign Tongue


Create a variable in my model? - El Forum - 09-21-2012

[eluser]ChrisF79[/eluser]
Worked perfectly! Thanks