choose a item in a database dropdown list in codeigniter - php

I have a dropdown list populated from a database of car models in codeigniter.
When user choose the value in dropdown (e.g. Lexus, Honda, etc.) the list of cars of chosen manufacturer should load. Hovewer, nothing works. Here's my current code.
//controller
public function carsPage() {
$this->load->model('model_users');
$data3['images'] = $this->model_users->getRow();
$data['tags'] = $this->model_users->get_dropdown_tags();
$newData = array_merge($data, $data3);
$this->load->view('cars', $newData);
}
//model
function get_dropdown_tags() {
//select distinct car_make from cars
$tags = $this->db->query("SELECT distinct car_make from cars");
$dropdowns = $tags->result();
foreach ($dropdowns as $dropdown)
{
$dropdownlist[$dropdown->car_make] = $dropdown->car_make;
}
$finaldropdown = $dropdownlist;
return $finaldropdown;
}

I have a theory about what you are trying to ask/do and this might work.
Controller:
public function carsPage() {
$this->load->model('model_users');
$data['images'] = $this->model_users->getRow();
$data['tags'] = $this->model_users->get_dropdown_tags();
$this->load->view('cars', $data);
}
Model:
public function get_dropdown_tags($car=NULL){
$this->db->select('car_make');
$this->db->from('cars');
$this->db->where('car_make', $car);
return $this->db->get();
}

Related

Join 3 tables with codeigniter and display it

I want to create a join database. in the 'relation' table will show 'disease' which can add many 'symptoms'. How should I do it?
Here is my database:
disease : id_disease, name, details
symptoms : id_symptoms, name, details
relation : id_relation, id_disease, id_symptoms
and here is my model
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
}
Please help me.
You had a typo: instead of relation you had relastion.id_disease.
Code below should work
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relation.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
The controller below is a example only in the select $this->db->select('relation.*, disease.*, symptoms.*');
Oon the controller you can pass the data to the view the create a foreach loop in the view.
<?php
class Somecontroller extends CI_Controller {
public function index() {
$data['allrelation'] = $this->allrelation();
$this->load->view('example', $data);
}
function get_allrelation() {
$this->db->select('relation.*, disease.*, symptoms.*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms = relation.id_symptoms','left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
}
return false
}
}

How to overwrite Codeigniter 3 route to show just name instead id and name

I need a little guidance here. I'm using Codeigniter 3.
Since it is MVC fw with segment routing, I want to know how is possible to create custom route which will show just a name of record (it can be category, product, post etc..) returned from database instead of id/name when I need id segment to identify which record from database I need to return or preview by that id.
I will post some code example:
Controller
class Categories extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('CategoryM');
}
public function index(){
$data = array(
'category_list' => $this->CategoryM->listAll()
);
$this->load->view('test/category_list', $data);
}
public function preview()
{
$categoryId = $this->uri->segment(2);
$data = array (
'previewByCategory' => $this->CategoryM->previewByCategory($categoryId)
);
$this->load->view('public/preview_by_category', $data);
}
Model
<?php
class CategoryM extends CI_Model {
public function listAll() {
$query = $this->db
->select('*')
->from('categories')
->where('parentid', NULL, TRUE)
->order_by('name', 'asc')
->get();
if($query->num_rows() > 0) {
return $query->result();
}else{
return false;
}
public function previewByCategory($categoryId=''){
$query = $this->db
->select( /* posts and categories data */ )
->from('categories')
->join('posts', 'posts.categoryID = categories.id', 'left')
->where('categories.id', $categoryId)
->get();
if( $query->num_rows() > 0 ){
return $query->result();
}else{
return false;
}
}
}
View
<?php if($category_list):?>
<?php foreach($category_list as $category):?>
<h3>
<a href="<?php echo base_url() . strtolower(url_title($category->name) . '/' . $category->id);?>">
<?php echo $category->name;?>
</a>
</h3>
<?php endforeach;?>
<?php endif;?>
My Routes
// Category routes
$route['categories'] = 'categories/index';
$route['(:any)/(:num)'] = 'categories/preview/$1';
What I want is instead of routes
www.mywebsite/categoryname/categoryid/
to display just
www.mywebsite/categoryname
which will list all products from that category group. But how to do this without id in url. I'm sorry if my question is too broad. Thank's in advance.
Codeigniter does not allow you to get database access in routes.
but you can create the database instance manually.
routes.php
$slug= ($this->uri->segment(1)) ? $this->uri->segment(1) : false;
if($slug){
//include your database
require_once(BASEPATH."/database/DB.php");
$db=& DB();
$category = $this->db
->select('name')
->from('categories')
->where('parentid', NULL, TRUE)
->where('name',$slug)
->get()->row();
if($category){
//$category->name must be unique in database
$route[$category->name] = 'categories/index';
}
}
In Categories controller
class Categories extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('CategoryM');
}
public function index(){
echo $category_name=$this->uri->segment(1);
}
}
URL
www.mywebsite/categoryname
//if categoryname exist in category
output :
categoryname
First you have to add column to your category table say url_title, which is unique and validate as a URI. Then you have to modify your route.php
// Category routes
$route['categories'] = 'categories/index';
$route['(:any)'] = 'categories/$1'; // $1 pass the utl_title value to Categories/preview($url_title)
and your controller
public function preview($url_title=null) {
$categoryId = $this->uri->segment(2);
$data = array (
'previewByCategory' => $this->CategoryM->previewByCategory($url_title)
);
$this->load->view('public/preview_by_category', $data);
}
and your model
public function previewByCategory($url_title=''){
$query = $this->db
->select( /* posts and categories data */ )
->from('categories')
->join('posts', 'posts.categoryID = categories.id', 'left')
->where('categories.url_title', $url_title)
->get();
if( $query->num_rows() > 0 ){
return $query->result();
}else{
return false;
}
}
I hope this would work for you. Please be carefull my posted code will have syntax errors because I did not tested it.

how to get the result of cart in the model in codeigniter?

My cart have two roomname.I loop the cart to get all value of roomname and want data of the both two roomname in the view.
My Model
public function getextrabedinfo(){
foreach ($this->cart->contents() as $cart) {
$roomname = $cart['roomname'];
$query = $this->db->query("SELECT * FROM roomcalendar where roomname='$roomname' ");
return $query->result();
}}
According to your question if you want to send data from model to view then try your code in this way..
public function getextrabedinfo(){
$alldata = array();
foreach ($this->cart->contents() as $cart) {
$roomname = $cart['roomname'];
$query = $this->db->query("SELECT * FROM roomcalendar where roomname='$roomname' ");
$alldata[] = $query->result();
}
return $alldata ;
}
Try this .. Hope it will work.

Update all table rows on database by using one query in codeigniter

I have table in my database named settings
and it have 3 Fields id, key, value
and i need to update all settings in one page using one query
this is my settings model
class Settings_model extends CI_Model{
/*
* Get global settings
*/
public function get_global_settings() {
$query = $this->db->get('settings');
$result = $query->result();
return $result;
}
/* Update setting */
public function update($data, $id) {
$this->db->where('id', $id);
$this->db->update('settings', $data);
return true;
}
}
and this is my controller
class Settings extends MY_Controller{
public function __construct() {
parent::__construct();
//Access control
if(!$this->session->userdata('logged_in')) {
redirect('admin/login');
}
}
public function index(){
// Get Categories
$data['settings'] = $this->Settings_model->get_global_settings();
// Load View
$data['main_content'] = 'admin/settings/index';
$this->load->view('admin/layouts/main',$data);
}
public function edit($id) {
// Calidation Rules
$this->form_validation->set_rules('value','Name','trim|required|xss_clean');
$data['settings'] = $this->Settings_model->get_group($id);
if($this->form_validation->run() == FALSE) {
// View
$data['main_content'] = 'admin/settings/edit';
$this->load->view('admin/layouts/main',$data);
} else {
// Create Articles Data Array
$data = array (
'value' => $this->input->post('value')
);
// Articles Table Insert
$this->Settings_model->update($data, $id);
// Create message
$this->session->set_flashdata('setting_saved', 'Your setting has been saved');
// Redirect page
redirect('admin/settings');
}
}
}
but it only update one row by id
i need to make a foreach loop to update all rows

How can i make to get not only by id but by one more column also

this my
model
public function get_news($NewsId= FALSE)
{
if ($NewsId === FALSE)
{
$this->db->select('*');
$this->db->from('news');
$this->db->where('Status' , 1);
$this->db->where('Language' , 2);
return $query->result_array();
$config['per_page']; $this->uri->segment(3);
}
$query = $this->db->get_where('news', array('NewsId' => $NewsId));
return $query->row_array();
controller
public function single($NewsId)
{
$data['news_item'] = $this->news_model->get_news($NewsId);
if (empty($data['news_item']))
{
show_404();
}
$data['NewsId'] = $data['news_item']['NewsId'];
$this->load->view('news/single', $data);
}
view
<?php echo $news_item['TittleNews'];?>
i have other view that foreach all my news table and i want create read more the i have created on up my code it works fine
want to make read more with NewsId and one other column it mean first check the NewsId and then other colum and then show me on my view which is singly
you can use a model method like this:
function getBy($data){
foreach($data as $key=>$value){
$this->db->where($key,$value);
}
$query = $this->db->get('users_table');
return $query->row(); //or $query->result(); as you want
}
so then you call it with
$this->model->getBy(
array(
'username'=>'Mark',
'id'=>'90',
'email'=>'asd#asd.com'
);
);
obviously you can then extend the model method as much as you can

Categories