Question

I have a form to update an article.in this form I have tow select boxes one for section and another for sub section.An article should have a section but sub section is not necessary.In my update form if a section has subsections it should bring it.My problem is that in my update form if a section does not have any subsection it does not show the continuation of my form because in model it returns false.I tried to return null or an empty array but it could not solve my problem.
Model:

function get_subsection($sec_id,$subsec_name){
$this->db->selec("*");
$this->db->where('sec_id',$sec_id);
$this->db->where('subsec_name !=',$subsec_name);
$query=$this->db->get('sub_section');
if($query->num_rows() > 0)
{
 return $query;
}
else
return false;

}     

if this function returns false in update form it does not show the continuation of form and the edit submit button.I removed the else condition but it can not solve the problem.
Controller

function edit($id){
$data['rec']=$this->amodel->edit_art($id);//get the record to update
foreach($data['rec'] as $a)
{
$sections['items']=$this->amodel->section('$a->sec_id');//select all sections without that which selected by user
$subsetion['sub']=$this->amodel->subsection($a->sec_id,$a->subsec_name);//select all subsections of a section which selected by user
}
$this->load->view('edit_art',array_merge($data,$sections,$subsection));
}

the problem is with $subsetion which can has no record in database. please guide me what should I do to show all form element even the subsection be empty.

Was it helpful?

Solution

If i understand ur problem then you can try this

Model:

function get_subsection($sec_id,$subsec_name){
$this->db->selec("*");
$this->db->where('sec_id',$sec_id);
$this->db->where('subsec_name !=',$subsec_name);
$query=$this->db->get('sub_section');

return $query->result();

Controller:

function edit($id){
$data['rec']=$this->amodel->edit_art($id);//get the record to update
foreach($data['rec'] as $a)
{
$data['items']=$this->amodel->section('$a->sec_id');//select all sections without that which selected by user
$data['sub']=$this->amodel->subsection($a->sec_id,$a->subsec_name);//select all     subsections of a section which selected by user
}
$this->load->view('edit_art',$data);
}

View :

 <?php echo form_dropdown('items',$items,"",'class=""'); ?>
  <?php echo form_dropdown('sub',$sub,"",'class=""'); ?>

Let me know is it ok or not. if not ok then post your view code.

OTHER TIPS

Try the following:

function edit($id){
$data['rec']=$this->amodel->edit_art($id);//get the record to update

foreach($data['rec'] as $a){
    if($this->amodel->count_sections($a->sec_id) > 0)
        $sections['items']=$this->amodel->section('$a->sec_id');//select all sections without that which selected by user
    else
        $sections['items'] = NULL;

    if($this->amodel->count_subsections($a->sec_id,$a->subsec_name) > 0)
        $subsetion['sub']=$this->amodel->subsection($a->sec_id,$a->subsec_name);//select all subsections of a section which selected by user
    else
        $subsetion['sub']= NULL;
}

$this->load->view('edit_art',array_merge($data,$sections,$subsection));
}

Count the number of available sections and subsections and try to read the records if they are avalable! Write the required functions (count_sections and count_subsections) in the model!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top