Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate Link name in Codeigniter from Database
    text
    copied!<p>I am new to codeigniter &amp; my company gave me a following task </p> <p>there is one table in that thr is one column status &amp; in status there are two option 1 is Active &amp; 2nd is Deactive. if current status is Active &amp; if we click on that \active link it wil turn to deactive. remember link value is fetched from database so, if link value is Active itr means in database status field value is Active &amp; when we click on Active it will turn to Deactive and in database the value of status nmust be changed to Deactive from Active.</p> <p><strong>Main_view.php (view)</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; 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; } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="&lt;?php echo base_url();?&gt;index.php/main_control/add" method="post"&gt; &lt;table align="center" border=2&gt; &lt;tr&gt; &lt;!--&lt;th&gt;ID&lt;/th&gt;--&gt; &lt;!--&lt;th&gt;Category&lt;/th&gt;--&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Description&lt;/th&gt; &lt;th&gt;Status&lt;/th&gt; &lt;/tr&gt; &lt;?php if (isset($result) &amp;&amp; !empty($result)) { //print_r($result);exit; foreach ($result as $row) { ?&gt; &lt;tr&gt; &lt;?php /*?&gt;&lt;td&gt;&lt;?php echo $row-&gt;category; ?&gt;&lt;/td&gt;&lt;?php */?&gt; &lt;td&gt;&lt;?php echo $row['name']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row['description']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="#" onClick="changeText(this,'deactivate');" id="text1link"&gt;active&lt;/td&gt; &lt;/tr&gt; &lt;?php } }?&gt; &lt;tr&gt;&lt;td align="center" colspan="4"&gt;&lt;input type="submit" value="Add New Sub Category" /&gt; &lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>main_category.php (model)</strong></p> <pre><code>&lt;?php class main_category extends CI_Model { public function getCategory(){ $this-&gt;db-&gt;select("*"); $this-&gt;db-&gt;from('main_category'); $this-&gt;db-&gt;where('m_id','0'); $query = $this-&gt;db-&gt;get(); if ($query-&gt;num_rows &gt; 0) { return $query-&gt;result(); } else { return false; } } public function category() { $x=0; $this-&gt;db-&gt;select("*"); $this-&gt;db-&gt;from('main_category'); $this-&gt;db-&gt;where('m_id','0'); $query = $this-&gt;db-&gt;get(); if ($query-&gt;num_rows &gt; 0) { $data = $query-&gt;result(); foreach($data as $value){ $valueofmain = $value-&gt;id; $arr[$x]['id'] = $valueofmain; $arr[$x]['name'] = $value-&gt;name; $arr[$x]['description'] = $value-&gt;description; $arr[$x]['status'] = $value-&gt;status; $this-&gt;db-&gt;select("*"); $this-&gt;db-&gt;from('main_category'); $this-&gt;db-&gt;where('m_id',$valueofmain); $query1 = $this-&gt;db-&gt;get(); if ($query1-&gt;num_rows &gt; 0) { $x++; $data1 = $query1-&gt;result(); foreach($data1 as $value1){ $arr[$x]['id'] = $value1-&gt;id; $arr[$x]['name'] = "-".$value1-&gt;name; $arr[$x]['description'] = $value1-&gt;description; $arr[$x]['status'] = $value1-&gt;status; } } $x++; } //print_r($arr);exit; } else { return false; } return $arr; } public function update_status() { $data = array( 'status' =&gt;$this-&gt;input-&gt;post('stat') ); // $this-&gt;db-&gt;where('status', $this-&gt;input-&gt;post-&gt;('stat')); $this-&gt;db-&gt;update('main_category', $data); } public function insert_into_db() { $data = array( 'm_id'=&gt;$this-&gt;input-&gt;post('catsel'), 'name' =&gt; $this-&gt;input-&gt;post('title'), 'description' =&gt; $this-&gt;input-&gt;post('desc'), 'status' =&gt; $this-&gt;input-&gt;post('status')); return $this-&gt;db-&gt;insert('main_category', $data); } /* public function insertcategory() { $data1 = array('m_id'=&gt;$this-&gt;input-&gt;post();) }*/ } ?&gt; </code></pre> <p><strong>main_control.php (control)</strong></p> <pre><code>?php class Main_control extends CI_Controller { function __construct() { parent::__construct(); $this-&gt;load-&gt;model('main_category'); } function index() { $res = $this-&gt;main_category-&gt;category(); $data = array('result'=&gt;$res); $this-&gt;load-&gt;view('Main_view', $data); } function add() { $res = $this-&gt;main_category-&gt;getCategory(); $data = array('result'=&gt;$res); $this-&gt;load-&gt;view('addcate',$data); } function update_status() { if($stat='active' || $stat='Deactive') { $this-&gt;main_category-&gt;update_status(); //redirect('main_control','refresh'); } } function insert_to_db() { /*if($catsel=="0 Root") { }*/ $this-&gt;main_category-&gt;insert_into_db(); //this-&gt;load-&gt;view('Main_view');//, $data); redirect('main_control','refresh'); } function addmaincategory() { $this-&gt;load-&gt;view('addcmaincate'); } public function getCategory($id) { $categories = array(); while($id != 0) { $this-&gt;db-&gt;from('main_categories'); //$this-&gt;table is a field with the table of categoris $this-&gt;db-&gt;where('id', $id); $this-&gt;db-&gt;limit(1); $result = $this-&gt;db-&gt;get()-&gt;row(); //fetching the result $categories[] = $result; $id = $result-&gt;m_id; } return $categories; } } ?&gt; </code></pre> <p><strong>addcate.php(view)</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="&lt;?php echo base_url();?&gt;index.php/main_control/insert_to_db" method="post"&gt; &lt;h2 align="center"&gt;Category Form&lt;/h2&gt; &lt;table align="center" border="2"&gt; &lt;tr&gt;&lt;td&gt;Category&lt;/td&gt; &lt;td&gt;Title&lt;/td&gt; &lt;td&gt;Desc&lt;/td&gt; &lt;td&gt;Status&lt;/td&gt; &lt;tr&gt; &lt;td&gt; &lt;select name="catsel"&gt; &lt;option selected="selected"&gt;--Select Category--&lt;/option&gt; &lt;option value="0"&gt;Root&lt;/option&gt; &lt;?php if (isset($result) &amp;&amp; !empty($result)) { foreach ($result as $row) { ?&gt; &lt;option value="&lt;?php echo $row-&gt;id; ?&gt;"&gt;&lt;?php echo $row-&gt;name; ?&gt;&lt;/option&gt; &lt;?php } }?&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt;&lt;input type="text" name="title" value="-"/&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="desc" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="radio" name="status" value="active"/&gt;active&lt;input type="radio" name="status" value="deactive"/&gt;dective&lt;/tr&gt; &lt;tr&gt;&lt;td align="center" colspan="4"&gt;&lt;input type="submit" Name="save" value="SAVE"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload