Archive for April, 2010

How to get all data from database in code Igniter

function get_all_students()
{
$query = $this->db->get(‘students’);
if ($query->num_rows() > 0)
{
$result = $query->result_array();
return $result;
}
else
{
return ‘0’;
}
}

// students is the table name of database

For details you may visit  http://codeigniter.com/user_guide/database/active_record.html

How to Insert data to database in code Igniter

function add($input)
{
$student_data = array(
‘name’ => $input[‘name’],
‘roll’ => $input[‘roll’]
);
$test = $this->db->insert(‘students’, $student_data);

// Produces: INSERT INTO student (name, roll) VALUES (‘name’, ‘roll’)

How to print all data as a row of table in Code Igniter

<?php foreach($students as $row):
?>
<tr>
<td ><?php echo $row[‘name’];?></td>
<td ><?php echo $row[‘roll’];?></td>
/* Creating link on Edit */
<td><?php echo anchor(‘students/edit/’.$row[‘id’],’Edit‘);?></td>
/* Student is Controller name and Edit is the function if controller */
</tr>
<?php endforeach; ?>

How to create form in Code Igniter Framework

/*
<form method="post" 
action="<?= site_url('Controller_name/
Controller_function').'/'.$student['id']; ?>">
<h1>Add information of Data</h1>
<input type="text" name="name"> <lable>Name</label>
<input type="text" name="roll"> <label>roll</label>
<input type="submit" name="submit" value="submit">
</form>
*/