My database definitely contains values and I am trying to read these values for a specific column (name) into an array like so:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
}
}
then referencing to this function like so:
public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
$fieldl = $fields;
$data= array();
$data['fieldl'] = $fieldl;
$this->load->view('clientlist', $data);
}
This basically completes the database query in the model, passes the information into the controller where it puts the array into another array with a key (key being the same name as the array) so that I can pass it into my view, then my view which looks like this :
<html>
<body>
<p> <?php print_r($fieldl); ?></p>
<ul>
<?php
$fieldl = array();
?>
<p> <?php print_r($fieldl); ?> </p>
<?php
foreach($fieldl as $l) {
?>
<li>
<?php echo $l;?>
</li>
<?php
}
Then lists it
but even though I KNOW I have data in "name" the print_r is showing that my array is empty? Help please and thank you!
Are you sure your model is returning any value? If not, the return some:
public function listcli()
{
$this->db->distinct();
$this->db->select('name');
return $this->db->get('table_name')->result_array(); // return value
}
Please try the following
public function listcli(){
$this->db->distinct();
$this->db->select('name');
$query = $this->db->get('table_name');
return ($query->num_rows > 0 ? $query->result() : array());
}
If it does not help ...
Please give me table structure with some dummy date, So that I can help you resolve this.
Related
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 this controller where all the available users and its respective information are passed in the view through an array:
function view() {
$data = array();
if($query = $this->m_user->get_user()) {
$data['user'] = $query;
}
$this->load->view('v_view_user', $data);
}
In my view I used this method (the norm) to view all that was passed:
<?php echo "user_name here" ?>
<?php if(isset($user)) : foreach ($user as $row) :
echo $row->user_name;
end foreach;
end if;
?>
What I want to do is to print a specific index (the name to be specific) before the code given above.
For the model:
function get_employees() {
$query = $this->db->get('user');
return $query->result();
}
By the way, the array contains user_id, user_name, user_family_name, ..., [and other more].
Your help will be highly appreciated.
$query->result(); will return the array of objects. So you can get user_name as below:
<?php if(isset($user)) : foreach ($user as $row) :
echo $row->user_name;
end foreach;
end if;
?>
EDIT: After question updated with my answer
you can use below code to get outside the loop:
echo $user[0]->user_name; // returns the first user_name
It was hard to come up with a title. I am using CodeIgniter with models/views/controller. I have the following tables in my MySQL database that are relevant:
In my model I have the following function:
function get_shoptable() {
$this->db->from('productshop')->where('productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
In my controller I use the above function like
$data['bookshop'] = $this->Product_model->get_shoptable();
In my view I am foreaching $bookshop. My problem is, what is the best wayto show shopName, instead of showing shopId. Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data.
Try some like this:
function get_shoptable() {
$this->db->from('productshop')
->join('shop', 'productshop.shopId = shop.shopId')
->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
Model:
function get_products() {
$this->db->select('productshop.productUrl, productshop.price, productshop.deliveryTime, productshop.shippingCast, productshop.inventory, productshop.productId, productshop.shopId, shop.shopName');
$this->db->from('productshop');
$this->db->join('shop', 'productshop.shopId = shop.shopId');
$this->db->where('productshop.productId', $this->productId);
return $this->db->get()->result_array();
}
Controller:
function products() {
$data['products'] = $this->model_name->get_product();
$this->load->view('products', $data);
}
VIEW:
<?php foreach($products as $p): ?>
<h1><?php echo $p['productUrl']; ?></h1>
<h1><?php echo $p['shopName']; ?></h1>
<?php endforeach(); ?>
get an overlook to active class of codeigniter for details of functions
function get_shoptable()
{
$this->db->from('productshop')
$this->db->join('shop', 'productshop.shopId = shop.shopId')
$this->db->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
//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
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