我是codeigniter&的新手。我的公司给了我一个以下的任务
有一个表,其中thr是一列状态&在状态中有两个选项1是Active&第二是Deactive。 如果当前状态为Active&如果我们点击那个\ active链接,它将变为deactive。 记住链接值是从数据库中获取的,因此,如果链接值为Active,则表示数据库状态字段值为Active&当我们点击“活动”时,它将变为“非活动”,并且在数据库中,状态值的值将从“活动”更改为“非活动”。
Main_view.php(查看)
<html>
<head>
<script type="text/javascript">
oldTextAry = new Array();
function changeText (fieldObj, newTexStr) {
if (newTexStr == fieldObj.innerHTML) {
fieldObj.innerHTML = oldTextAry[fieldObj.id];
} else {
oldTextAry[fieldObj.id] = fieldObj.innerHTML;
fieldObj.innerHTML = newTexStr;
}
}
</script>
</head>
<body>
<form action="<?php echo base_url();?>index.php/main_control/add" method="post">
<table align="center" border=2>
<tr>
<!--<th>ID</th>-->
<!--<th>Category</th>-->
<th>Name</th>
<th>Description</th>
<th>Status</th>
</tr>
<?php
if (isset($result) && !empty($result))
{
//print_r($result);exit;
foreach ($result as $row)
{
?> <tr>
<?php /*?><td><?php echo $row->category; ?></td><?php */?>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><a href="#" onClick="changeText(this,'deactivate');" id="text1link">active</td>
</tr>
<?php
} }?>
<tr><td align="center" colspan="4"><input type="submit" value="Add New Sub Category" /> </td></tr>
</table>
</form>
</body>
</html>
main_category.php(型号)
<?php
class main_category extends CI_Model
{
public function getCategory(){
$this->db->select("*");
$this->db->from('main_category');
$this->db->where('m_id','0');
$query = $this->db->get();
if ($query->num_rows > 0)
{
return $query->result();
}
else
{
return false;
}
}
public function category()
{
$x=0;
$this->db->select("*");
$this->db->from('main_category');
$this->db->where('m_id','0');
$query = $this->db->get();
if ($query->num_rows > 0)
{
$data = $query->result();
foreach($data as $value){
$valueofmain = $value->id;
$arr[$x]['id'] = $valueofmain;
$arr[$x]['name'] = $value->name;
$arr[$x]['description'] = $value->description;
$arr[$x]['status'] = $value->status;
$this->db->select("*");
$this->db->from('main_category');
$this->db->where('m_id',$valueofmain);
$query1 = $this->db->get();
if ($query1->num_rows > 0)
{
$x++;
$data1 = $query1->result();
foreach($data1 as $value1){
$arr[$x]['id'] = $value1->id;
$arr[$x]['name'] = "-".$value1->name;
$arr[$x]['description'] = $value1->description;
$arr[$x]['status'] = $value1->status;
}
}
$x++;
}
//print_r($arr);exit;
}
else
{
return false;
}
return $arr;
}
public function update_status()
{
$data = array( 'status' =>$this->input->post('stat') );
// $this->db->where('status', $this->input->post->('stat'));
$this->db->update('main_category', $data);
}
public function insert_into_db()
{
$data = array(
'm_id'=>$this->input->post('catsel'),
'name' => $this->input->post('title'),
'description' => $this->input->post('desc'),
'status' => $this->input->post('status'));
return $this->db->insert('main_category', $data);
}
/* public function insertcategory()
{
$data1 = array('m_id'=>$this->input->post();)
}*/
}
?>
main_control.php(控制)
?php
class Main_control extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('main_category');
}
function index()
{
$res = $this->main_category->category();
$data = array('result'=>$res);
$this->load->view('Main_view', $data);
}
function add()
{
$res = $this->main_category->getCategory();
$data = array('result'=>$res);
$this->load->view('addcate',$data);
}
function update_status()
{
if($stat='active' || $stat='Deactive')
{
$this->main_category->update_status();
//redirect('main_control','refresh');
}
}
function insert_to_db()
{
/*if($catsel=="0 Root")
{
}*/
$this->main_category->insert_into_db();
//this->load->view('Main_view');//, $data);
redirect('main_control','refresh');
}
function addmaincategory()
{
$this->load->view('addcmaincate');
}
public function getCategory($id)
{
$categories = array();
while($id != 0)
{
$this->db->from('main_categories'); //$this->table is a field with the table of categoris
$this->db->where('id', $id);
$this->db->limit(1);
$result = $this->db->get()->row(); //fetching the result
$categories[] = $result;
$id = $result->m_id;
}
return $categories;
}
}
?>
addcate.php(视图)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="<?php echo base_url();?>index.php/main_control/insert_to_db" method="post">
<h2 align="center">Category Form</h2>
<table align="center" border="2">
<tr><td>Category</td>
<td>Title</td>
<td>Desc</td>
<td>Status</td>
<tr>
<td>
<select name="catsel">
<option selected="selected">--Select Category--</option>
<option value="0">Root</option>
<?php
if (isset($result) && !empty($result))
{
foreach ($result as $row)
{
?>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php
} }?>
</select>
</td>
<td><input type="text" name="title" value="-"/></td>
<td><input type="text" name="desc" /></td>
<td><input type="radio" name="status" value="active"/>active<input type="radio" name="status" value="deactive"/>dective</tr>
<tr><td align="center" colspan="4"><input type="submit" Name="save" value="SAVE"/></td> </tr>
</table>
</body>
</html>