//model
function shop_dropdown()
{
$this->db->select('shop');
$this->db->from('shop');
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name'];
}
return $data;
}
controller//
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop['select_options'] = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
view//
<?php
echo form_dropdown('shop', $select_options);
?>
this is not not working.please help me creating a drop downlist from database.if you can write a new code.
thanks in advance
Modify like this
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
and in your view
echo form_dropdown('shop', $shop->option);//option is an value taking form database
that's it.accept answer if it useful for you
I am not sure if you have autoload form helper, if you didn't, you can't use the form_dropdown function unless you load it in the controller. I don't see you load form helper anywhere.
http://codeigniter.com/user_guide/helpers/form_helper.html
you are selecting 'shop' column in your model.
I think your model should be like this
function shop_dropdown()
{
$this->db->select('id,name'); //column names you want to select, can be optional if you want to select all columns.
$this->db->from('shop'); //table name, required
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name']; //make sure 'id' and 'name' ,columns are present in table
}
return $data;
}
And I hope you have edited application/config/databse.php
Related
I am facing problem to join these tables and also know how to make controller code to view data in view file with text boxes.
public function get_order_return_info()
{
$this->db->select('tbl_order_details.*', false);
$this->db->select('tbl_order_details.order_details_id', false);
$this->db->select('tbl_order.order_id', false);
$this->db->select('tbl_product.product_id', false);
$this->db->select('tbl_inventory.product_quantity', false);
$this->db->from('tbl_order_details');
$this->db->join('tbl_order', 'tbl_order_details.order_id = tbl_order.order_id ', 'left');
$this->db->join('tbl_product', 'tbl_order_details.product_code = tbl_product.product_id ', 'left');
$this->db->join('tbl_inventory', 'tbl_product.product_id = tbl_inventory.product_id ', 'left');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
You can use MVC to solve this
//Controller
function get_data() {
$data['list'] = $this->your_model->get_data();
$this->load->view('your_view/location',$data);
}
//Model
function get_data() {
$sql = "your_query";
$list = $this->db->query($sql)->result_array();
return $list;
}
//View
foreach($list as $data) {
echo $data['your_selected_field']; // it could be table,text field,or just text
}
API Reference : https://www.codeigniter.com/user_guide/database/results.html#result-arrays
Hope this helps
First u can create OrderModel to work with order table. Then create function get_order_data.
//Order Model
public function get_orders() {
// your db query to get orders
return $result;
}
Then u can load model in controller using $this->load->model('OrderModel'); Best place is __construct to load model
And call your get_orders() function
// Your controller
// Create data array to store front data
$data = [];
$data['orders'] = $this->OrderModel->get_orders();
After getting $orders pass it to view using following codes
$this->load->view('path/to/view', $data);
Finally u can call your order data in view using $orders
I hope this will help u
You need to set alias to the table to function it properly like this.
$this->db->select('a.*,a.order_details_id, b.order_id ', false);
$this->db->from('tbl_order_details as a');
$this->db->join('tbl_order as b', 'a.order_id = b.order_id ', 'left');
If you want select 1 row only you need to use this function.
$this->db->get('tbl_order_details')->row_array();
or if you want all result row you need to use this.
$this->db->get('tbl_order_details')->result_array();
On your controller you should add this lines to pass your result on your view page.
public function index()
{
$data['orderDetails'] = $this->your_model->get_order_return_info();
$this->load->view('pages/yourview/index',$data);
}
And finally to set your order details to the text boxes. You need to echo it inside the text boxes like this.
<input type="Text" name="orderID" value="<?php echo $orderDetails['order_id'];?>" >
If you use result_array() you need to use foreach loop to echo it.
Please help me. i can not use my dataTable properly. what I want to do is
select from table and use thewherefunction. but i cant do it properly.
here is my controller code
public function reporttable ()
{
$zz = array('empnumber' => $this->input->post('empnumber'));
//$this->db->order_by("surname", "asc");
$this->datatables->select('date_auto,particulars,earned_VL,aul_VL,balance_VL,aulx_VL,earned_SL,aul_SL,balance_SL,aulx_SL,date_leave,action_taken')
->from('tblreport')->where($zz);
echo $this->datatables->generate();
}
this is my supposed query:
select * from tblreport where empnumber = (the empnumber in my textbox.)
there, i get a value from a textbox to my view. but it didn't work. i know that is wrong. can you please help me with my problem? thank you.
<p align="center"> <?php echo $this->table->generate();?></p></div>
<?php foreach ($tblemployee as $row){?>
<input type="text" name="empnumber" readonly="readonly" value="<?php echo $row->empnumber;?>"/>
<input type="hidden" name="empnumber" value="<?php echo $row->empnumber;?>"/>
here is my view for guide. thank you.
as Simple you can use
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
Try this :
To make it more simplier.
In model :
public function reporttable ($id){
$this->db->select('*');
$this->db->from('tblreport');
$this->db->where('empnumber', $id);
$query = $this->db->get();
return $query->return_array(); // use this if so want to return many query if not you can also use first_row('array')
}
In controller :
public function function_name (){
$data['variable_name'] = $this->model_name->reporttable($id); // change the model_name by your own model where your function reporttable is located and use the variable_name for your view,
$this->load->view('view_name' , $data); // load the view page you want to display.
}
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
I have a table called 'News' with three columns: 'id', 'title' and 'details'
I have a function ('get_entry') inside a codeigniter model class (called 'News_model')
function get_entry()
{
$this->load->database();
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
return $data['newsarray'];
}
I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.
foreach ($this->News_model->get_entry() as $key => $value){
array_push($data['theNews'],$value->title);
}
I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.
About your code:
You have two 'return' in your get_entry function:
function get_entry()
{
$this->load->database();
// First
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
// Second
return $data['newsarray'];
}
Change it to:
function get_entry()
{
$this->load->database();
$query = $this->db->select('id,title,details')->from('news');
$data['newsarray'] = $query->row_array();
return $data['newsarray'];
}
It should work now.
Some advices:
Don't use Codeigniter 2 anymore. Version 3 is alive.
If you plan to return whole table columns, i suggest you to use the following code for the query:
$query = $this->db->get('news', 1, 20);
Where 1, 20 is the limit.
Now you can get the result:
return $query->result();
A simple example:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news', 1, 20);
return $query->result();
}
This method returns the query result as an array of objects that you can print like so in your controller:
$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
echo $news->id;
}
Look at CI 3 Query Builder query builder for more examples.
One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.
Changing the code to this in the function worked:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news');
//return $query->result();
foreach ($query->result() as $row)
{
echo "</br>";
echo $row->id;
echo "</br>";
echo $row->title;
echo "</br>";
echo $row->details;
echo "</br>";
}
}
Calling the function like so prints it out:
$news_array = $this->News_model->get_entry();
i am new in codeigniter. And i am trying to get field name of a table with a query.
I have write a query
"select * from user"
and pass it to $this->db->query() function. i am getting records. But i want to get field names of table. so how can i get that?
Can anybody help me please. Thanks in advance.
using database library write this code to list all fields:
$this->db->list_fields('table')
take a look here: https://codeigniter.com/userguide3/database/results.html#CI_DB_result::list_fields
Some Times this may helpful
$fields = $this->db->field_data('table_name');
foreach ($fields as $field)
{
echo $field->name;
echo $field->type;
echo $field->max_length;
echo $field->primary_key;
}
What you did is to get the datas from the table....
here your table is user so
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
foreach($result as $field)
{
$data[] = $field;
return $data;
}
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}
hope this will help you
you can use the below code to fetch the field from db
$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
echo $field;
}
with the help of this code we can fetch the field names from db
include('db.php');
$col="SHOW COLUMNS FROM `camera details`";
$output=mysqli_query($db,$col);
$kal=array();
while($row=mysqli_fetch_array($output))
{
if($row['Field']!='id')
{
?><div class="values"><?php echo $row['Field'];
echo "<br>";?></div><br><br><?php
array_push($kal, $row['Field']);
}
}
?>
<?php**
The answer given above by Anwar needs modification, there is no need for foreach loop in model function as it will only give the first element despite using foreach, which means the foreach loop is not bringing the fields using $data[], below code should give you 100% result
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
return $result();
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}
I am writing a form, which has a select menu in it, I want the values to pulled from the database, so I thought it would be something along these lines:
My view
<?php
echo form_open('admin/save_content');
echo form_fieldset();
echo form_dropdown('categories', $select_options);
echo form_submit('category_submit', 'Submit');
echo form_fieldset_close();
echo form_close();
?>
My controller
function add_content() {
$data = array();
$this->is_logged_in();
$this->load->model('category_model');
$data['select_options'] = $this->category_model->get_all_online();
$this->load->view('admin/content/add_content', $data);
}
my model
public function get_all_online() {
$this->db->select('*');
$this->db->from('category');
$this->db->where('category_online', 1);
$query = $this->db->get();
return $query->result();
}
now when I place the $selected_options in the form dropdown I get this error,
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass
could not be converted to string
Filename: helpers/form_helper.php
Line Number: 331
You need to pass an array to your dropdown, where the array key will be the value that is POSTed and the value will the text that is displayed.
To achieve this, change your controller like so:
function add_content() {
$data = array();
$this->is_logged_in();
$this->load->model('category_model');
$data['select_options'] = $this->category_model->get_all_online_select();
$this->load->view('admin/content/add_content', $data);
}
and add this function to your model
public function get_all_online_select() {
$this->db->select('id, name'); //change this to the two main values you want to use
$this->db->from('category');
$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row){
$data[$row['id']]=$row['name'];
}
return $data;
}
That should do the trick
I personally hate to make assumptions in my Models about how my data will be used as that is the job of the controller. If you add a MY_array_helper.php and paste this in:
function array_to_select() {
$args = func_get_args();
$return = array();
switch(count($args)):
case 3:
foreach ($args[0] as $itteration):
if(is_object($itteration)) $itteration = (array) $itteration;
$return[$itteration[$args[1]]] = $itteration[$args[2]];
endforeach;
break;
case 2:
foreach ($args[0] as $key => $itteration):
if(is_object($itteration)) $itteration = (array) $itteration;
$return[$key] = $itteration[$args[1]];
endforeach;
break;
case 1:
foreach ($args[0] as $itteration):
$return[$itteration] = $itteration;
endforeach;
break;
default:
return FALSE;
break;
endswitch;
return $return;
}
Then you can do something like this:
function add_content() {
$data = array();
$this->is_logged_in();
$this->load->model('category_model');
$this->load->helper('array');
$data['select_options'] = array_to_select($this->category_model->get_all_online(), 'id', 'title');
$this->load->view('admin/content/add_content', $data);
}
That supports multi-dimensional arrays by passing in one or two keys, or single dimensional arrays by using the value as the value and the key.
Eg: array_to_select(array('value1', 'value2')) gives array('value1'=>'value1', 'value2'=>'value2')
You need to return an array of strings, result() is an array of objects.
Maybe try this in your model:
return $query->result_array();
In your view, you can add foreach there instead of in Model.
<?php
echo form_open('admin/save_content');
echo form_fieldset();
foreach($select_options->result_array() as $row){
$data[$row['id']]=$row['name'];
echo form_dropdown('categories', $row);
}
echo form_submit('category_submit', 'Submit');
echo form_fieldset_close();
echo form_close();
?>
Not tested.
I have edited Phil Surgeon's array helper to work with a simple db query with only two fields (id & value). So the helper class now looks like this:
<?php
function array_to_select() {
//get args
$args = func_get_args();
//get args key names
$keys = array_keys($args[0][0]);
//set return array
$return = array();
foreach ($args[0] as $itteration){
//$itteration[$keys[0]] is field id value, $itteration[$keys[1]] is field name value
$return[$itteration[$keys[0]]] = $itteration[$keys[1]];
}
return $return;
}
And you can use it again in your controller.
Hope it's usefull.
form drop down with lot of options codeigniter form drop down menu with validation class also