Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform a horizontal table row into a vertical one

I am using CodeIgniter 2.1.0 and MySQL. I want to display a horizontal data row as a vertical one. When I fetch a single row from the database and echo it, it looks like

----------------------------------------
id    | name   | address | email       |
----------------------------------------
1     | Foo    | Bar     | [email protected] |
----------------------------------------

I have used CodeIgniters table library to generate the above table. instead of this, I want it show like this:

------
id : 1
name: foo
address : bar
email: [email protected]
-------------------

How do I do this with CodeIgniter 2.1.0?

like image 206
Shabib Avatar asked Jun 24 '26 08:06

Shabib


2 Answers

If you have any problem to implement my previous answer... Here is the details... Let me know is it useful or not? Thanks.

/* for controller */

$data['user_data'] = $this->modle_name->function_name; modle_name= your modle name where the specific function exists. function_name= function into the modle by which you get all the table value from mysql db.

then write the function currectly in modle and go to the view page:it may be like that

/* model */

function function_name(){
$sql = "select * from tablename where condition";
$query = $this->db->query($sql);
return $query->result_array();
}

/* for view */

<?php foreach ($user_data as $data) {?>
<tr>
 <?php echo 'ID:' . '  ' ?>
 <?php echo ($data['id']); ?>
 <?php echo 'Name:' . '  ' ?>
 <?php echo ($data['name']); ?>
 <?php echo 'Address:' . '  ' ?>
 <?php echo ($data['address']); ?>
 <?php echo 'E-mail:' . '  ' ?>
 <?php echo ($data['email']); ?>
</tr> 
<?php } ?>
like image 195
Sakib Avatar answered Jul 01 '26 06:07

Sakib


i have figured out one solution to this problem, but i think it is not the best way. instead of using table library, i have changed my model,controller and view like this: model:

function detail()
{
 $this->db->where('id',$this -> session -> userdata('id'));
 $query=$this->db->get('user');
 $row=$query->row_array();
 return $row;
}

controller:

$this->load->model('my_model');
$this->my_model->detail();
$data=array(
 'id'=>$query['id'],
 'name'=>$query['name'],
 'address'=>$query['address'],
 'email'=>$query['email']
);
$this->load->view('my_view',$data);

view:

<div>
id : <?php echo $id;?><br/>
name: <?php echo $name;?><br/>
address: <?php echo $address;?><br/>
email: <?php echo $email;?>
</div>

this works perfectly. but i wished to solve this in a more simpler manner.

like image 25
Shabib Avatar answered Jul 01 '26 07:07

Shabib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!