Welcome Guest, Not a member yet? Register   Sign In
Please help me to display count values returned from a database on form drop down
#1

[eluser]Unknown[/eluser]
hi guys i am new in code ignitor
i have a challenge for displaying values pulled from a database.
i need data to be shown in form dropdown like this

chicago (3)
los angels (5)
lagos (9)
dubai (4)

here is my model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Fetch_data extends CI_Model
{
public function __construct()
{
parent:: __construct();
}

function getloc()
{
$query = $this->db->query('
SELECT location,
COUNT(*) FROM houseforrent
GROUP BY location
');
return $query->result();
}
}

here is my controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

function index()
{
$this->load->model('fetch_data');
$dat = $this->fetch_data->getloc();
$data['recs'] = $dat;


$data['main_view'] = 'home';
$this->load->view('includes/contents',$data);
}
}

here is my view

<?php
echo form_open('main/bylochouserent');

$submit = array
(
'name' => 'submit',
'value' => 'Find'
);

foreach($recs as $da):
$ona[$da->location] = $da->location . " ".$da[count('*')] = $da[count('*')];
endforeach;
$di = "style = height :12 width : 20";
echo form_dropdown('street',$ona, 'sinza',$di);
echo form_submit($submit);
echo form_close();
?>

#2

[eluser]CroNiX[/eluser]
SELECT location,
COUNT(*) AS house_count FROM houseforrent
GROUP BY location

...

$ona[$da->location] = $da->location . " ".$da['house_count'] = $da['house_count'];
#3

[eluser]Unknown[/eluser]
thank you mr cronix you saved my day.
#4

[eluser]sv3tli0[/eluser]
It will be good if you cache that count for few minutes...
Few counts on each page loading will f*ck your DB Smile




Theme © iAndrew 2016 - Forum software by © MyBB