Welcome Guest, Not a member yet? Register   Sign In
Form selecting data from two database tables problem
#1

[eluser]Unknown[/eluser]
Hi there,

I've been trying to create a form in my application where you can edit data for a post, and assign it to a value in another table.

Eg. I have a list of created 'projects', I then need to edit the project(which i have working fine) and also assign to a client(where client id and name is stored in a 'clients' database table.

So far, it won't populate the dropdown with the list of clients...

My 'Edit' Controller
Code:
function edit(){
        if ($this->uri->segment(3)) {
            $id = $this->uri->segment(3);
        }
        $id = $this->uri->segment(3);
        $data['query']= $this->site_model->edit_project($id);
        $this->load->model('clients_model');
        $data['c_query']= $this->clients_model->get_records();
                
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('project_name', 'project_name', 'trim|required|min_length[1]');
        $this->form_validation->set_rules('project_desc', 'project_desc', 'trim|required|min_length[1]');
        $this->form_validation->set_rules('project_deadline', 'project_deadline', 'trim|required');
        
        $this->form_validation->set_rules('clients_id', 'clients_id', 'trim|required');
        $this->form_validation->set_rules('clients_name', 'clients_name', 'trim|required');
        
        if($this->form_validation->run() == FALSE)
        {
            $data['main_content'] = 'project_view';
            $this->load->view('includes/template_inner', $data);
            
        }
        
        else
        {            
        $this->site_model->reinsert($id);

        $this->index();
        }
        
    }

My 'Site' Model
Code:
function edit_project($where){
        $sql = "SELECT * FROM projects WHERE id = ?";
        $query=$this->db->query($sql, array($where));
        return $query;
    }

function get_clients_c()
    {    
        $c_query = $sql = "SELECT clients_id, clients_name FROM clients";
        return $c_query;
        }

function reinsert(){
        $this->db->where('id',$_POST['id']);
        $this->db->update('projects',$this->db->escape($_POST));
    }

My 'Clients' Model
Code:
function get_records()
    {
        $query = $this->db->get('clients');
        return $query->result();
    }

My 'project_view' View
Code:
<?php foreach ($query->result() as $row): ?>
                    <?php echo form_open('site/edit'); ?>
                        <?php echo form_hidden('id', $this->uri->segment(3)); ?>
                        
<p>    
    <label for="project_name">Project Name:</label>&lt;input type="text" style="width:520px;" name="project_name" value="&lt;?php echo $row-&gt;project_name ?&gt;" />
</p>

<p>                    
    <label for="project_name">Project Description</label>&lt;textarea name="project_desc" style="width:520px;" rows="10"&gt;&lt;?php echo $row->project_desc ?&gt;&lt;/textarea&gt;
</p>
                                                
<p>
    <label for="date1">Deadline:</label>
    &lt;input name="project_deadline" id="date" value="&lt;?php echo $row-&gt;project_deadline ?&gt;" type="textbox"/>
</p>

<p>
<label for="clients_name">Client:</label>
    <select name="clients_name">
    <option value="0">- - - - - -</option>
&lt;?php foreach ($c_query->result_array() as $inner_row):
                            $selected=0;
                            if($inner_row['clients_id']==$row->clients_name){
                                $selected = " SELECTED ";
                            }
                                echo "<option value=\"".$inner_row['clients_id']."\" ".$selected.">".$inner_row['clients_name']."</option>\n";
                            $selected=0;
                            endforeach; ?&gt;
    
    </select><br />
</p>



<br  /><br  />
                        
                        
                        &lt;input type="submit" class="button" value="Update Project" /&gt;&lt;span class="members_area_h2"> &lt;?php echo anchor('site', 'Cancel', array('title' => 'Cancel edit','class' => 'cancel')); ?&gt;</span>
                        
                        &lt;/form&gt;        
                    &lt;?php endforeach; ?&gt;

Im confused as to why my dropdown for assigning a client isn't working. It seems to affect whether my footer appears or not also, therefore unsure if the code i have used for my dropdown somehow doesn't close its tags properly...is there something obvious im missing!

Many thanks!

John




Theme © iAndrew 2016 - Forum software by © MyBB