How to get field names of mysql table in codeigniter? - php

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
}

Related

How to display data from MYSQL in codeignitor?

I want to create a dynaic page, I have created model and controller and also data subitted in database successfully. Now, i'm having problem while displaying that data on front end.
Here is my Modal:
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
return $query->result_array();
}
Here is my Controller:
function corporate()
{
$popular['popular'] = $this->auth_model->getPopularcourses();
$data1['corporate'] = $this->auth_model->getcorporate();
$data["institute_details"] = $this->auth_model->getInstitutedetails();
$data1['course'] = $this->auth_model->getcoursesdetailes();
$this->load->view('nulearnFront/header', $data);
$this->load->view('nulearnFront/corporate', $data1);
$this->load->view('nulearnFront/footer', $popular);
}
Try this
First you can print_r() the data you receive.
print_r($corporate);
After that you can use foreach to display all the data
foreach($corporate as $value)
{
////do code according to your requirement
}
I hope this may be help out to solve your problem
Add View file this code
<?php
if (isset($corporate) && !empty($corporate)) {
foreach ($corporate as $cdata) {
echo $cdata->YourValue(db column name);
}
}
?>
You are so close to the answer. You are passing the data from your Controller class. So what you have to do is just get that data as the follows,
I get the corporate values as it is returning an array data. So here you go,
In your view.php file,
<?php
if (isset($corporate)) { // Check if the data is set or not
foreach ($corporate as $corporateData) {
?>
// Your HTML goes here, table or etc.
<?php echo $corporateData->databaseColumnName // Value that need to print from the database ?>
<?php
}
}
?>
Hope this helps you.
print the query and run it to check
function getcorporate(){
$q="SELECT * from corporate";
$query=$this->db->query($q);
print_r($this->db->last_query());die();
return $query->result_array();
}
if query works fine then you can foreah the query
foreach($corporate as $corporate)
{
echo corporate;
}
if it does not return result then change result_array() to result() in model

How to send multiple values to mysql tables in yii

I'm new to PHP and Yii framework. how to insert multiple questions in database
This is Form view code
<?php echo $form->textField($model,'questions',array('id'=>"content_#index#_question textBox")); ?>
<?php echo $form->textField($model,'questions',array('id'=>"content_#index#_question textBox")); ?>
Here is My Controller
public function actionAdd_quick()
{
$model=new Question;
$answers=new Answers;
if(isset($_POST['Question'],$_POST['Answers']))
{
$model->attributes=$_POST['Question'];
$answers->attributes=$_POST['Answers'];
foreach ($model['questions'] as $value) {
$model->questions = $value;
}
$model->save();
Yii::app()->user->setFlash('add_quick','Thank you for ');
$this->refresh();
}
$this->render('add_quick',array('model'=>$model,'answers'=>$answers));
}
actually my process is to create multiple questions and answers ,but now getting error like
"Invalid argument supplied for foreach()" by using my code and how to send multiple questions and answers to mysql tables...
change this
foreach ($model['questions'] as $value) {
$model->questions = $value;
}
$model->save();
on this
foreach ($model['questions'] as $value) {
$model->questions = $value;
$model->save();
}
Try this
<?php echo $form->textField($model,'questions[]',array('id'=>"content_#index#_question textBox")); ?>

cakephp getting value of textfield in model

Hi I have dynamically added input fields on my add view. When I submit my form I want all them input fields to be concatenated into one string and stored in a single database field.
Im trying to achieve this using the beforeSave method of cakephp but I can't seem to find out how to get the values of a text field within the Model.
function beforeSave($options)
{
$result = '';
$bool = true;
$counter = 0;
while($bool == true)
{
$result = $result + ',' + $this->data['Variable']['selectOptions' + counter];
}
return true;
}
Anyone any ideas on how to achieve this?
Thanks in advance.
In my opinion (from MVC point of view) it would be better to concatenate the fields in controller and then unset unnecessary fields before saving the model...
after get the post values in model you can merge the array values into a single variable like this..
<?php
$var = array('test1','test2','test3','test4','test5','test6');
$new_values = implode(',',$var);
echo $new_values;
?>
You can retrieve these values after saving to database.
this might not be the perfect answer but might give you some head start in that direction
function beforeSave($options = array()) {
pr($this->data); // <= show data you intend to save
exit;
}
use foreach to loop the data array ($this->data) and perform concatenation on the values and assign the concatenated string to the feild name
function beforeSave($options = array()) {
foreach ($this->data['Variable'] as $key=>$value)
{
$feildflag = strstr($key, 'selectOptions');
if($feildflag){
$concatenatedstring .= $value;
}
}
$this->data['Variable']['your_feild_name'] = $concatenatedstring ;
}
Your dynamic form field should looks like in a foreach loop:
<?php echo $this->Form->input('Model.field][', array());
In your model:
function beforeSave($options)
{
if(!empty($this->data))
{
$this->data['Model']['common_field'] = implode(",", $this->data['Model'['field'];
unset($this->data['Model']['field']);
}
}

codigniter drop down list from database

//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

Codeigniter form_helper getting database rows to be values in select menu

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

Categories