[eluser]Unknown[/eluser]
Hi, I'm writing my first CodeIgniter application. I want to display a list of companies with addresses. The addresses are stored in 4 database fields, I want to concatenate these with commas. I am trying to do this in the Model as I figure it's data manipulation. I've concatenated the address fields in the SQL query, but I need to get rid of the double commas that occur if a field is blank. When I try this code, my str_replace function is ignored and I get double commas anyway.
Model:
Code:
<?php
class Companies_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_companies()
{
$query = $this->db->query('SELECT trading_name, CONCAT(IFNULL(address_line1,""), ", ",
IFNULL(address_town,""), ", ",
IFNULL(address_county,""), ", ",
IFNULL(address_postcode,"")) as address,
url,
telephone
FROM companies');
foreach ($query->result_array() as $row)
{
$row['address'] = str_replace(', , ', ', ', $row['address']);
}
return $query->result_array();
}
}
Controller:
Code:
<?php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('companies_model');
}
function index()
{
$data['companies'] = $this->companies_model->get_companies();
$this->load->helper('url');
$this->load->view('header');
$this->load->view('results', $data);
$this->load->view('search');
$this->load->view('footer');
}
}
View:
Code:
<section id="results">
<?php foreach ($companies as $company):?>
<p><strong><?php echo $company['trading_name']; ?></strong><br />
<?php echo $company['address']; ?>
</p>
<?php endforeach;?>
</section>
How do I modify $row['address']? Or is there a better approach?
Thanks