[eluser]Brad K Morse[/eluser]
Receiving error, screenshot
http://cl.ly/1dc196f249bab5fbea3f
Running join to grab title of venue the volunteer is assigned to, this is for the update/edit view.
data model for grabbing the title (venues.title) of the venue that volunteer is assigned to (data.venue_id = venues.id), where id = segment(3)
Code:
function getVolunteersVenue() {
//$q = $this->db->select('*')->from('data')->join('venues', 'venues.id = data.venue_id')->get();
$this->db->select('data.id,
venues.id,
venues.id as volunteers_venue_id');
$this->db->from('data');
$this->db->join('venues', 'data.venue_id = venues.id');
$this->db->where('data.id', $this->uri->segment(3));
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
What I think I am missing, is a clause for when the the volunteer is not assigned to a venue.
It grabs the title of the venue then compares to each venue when it displays the venue drop down list:
http://cl.ly/ccfcad3485fadd6c29f7
Code in update view:
Code:
<html>
<body>
<?=anchor('/admin','view dashboard');?>
<?php
foreach($volunteers as $r) {
print form_open(uri_string()); ?>
<p>First name: <input type="text" value="<?=$r->first_name?>" name="first_name"></p>
<p>Shirt Size: <input type="text" value="<?=$r->tshirt?>" size="3" name="tshirt"></p>
<p>Venue: <?=$r->venue_id?></p>
<?php
if($r->venue_id == 0) {
$venue_id = 0;
} else {
foreach($volunteersvenue as $r) {
$venue_id = $r->volunteers_venue_id;
}
}
print 'Venue: <select name="venue_id">';
if($venue_id == NULL) {
print '<option value="" selected>- Not Assigned -</option>';
}
foreach($venues as $r) {
print '<option value="'.$r->id.'"';
if($venue_id == $r->id) { print 'selected'; }
print '>'.$r->title.'</option>';
}
print '</select>';
?>
<?=form_submit('submit', 'Update!');?>
<?=form_close();?>
<?php } ?>
</body>
</html>
Update function within controller, that loads the getVolunteersVenue() data_model
Code:
function update() {
$this->load->model('data_model');
$data['volunteers'] = $this->data_model->getVolunteer();
$data['volunteersvenue'] = $this->data_model->getVolunteersVenue();
$data['venues'] = $this->data_model->listVenues();
if($_POST) {
$data['volunteers'] = $this->data_model->updateVolunteer();
}
$this->load->view('update', $data);
}
Any help is appreciated.
Update: Fixed
Instead for checking if titles are equal, I now compare data.venue_id == venues.id for the selected part of the code
and for the fix (within the update view), for when the the query does not return a result, since sometimes data.venue_id may be empty, I did this to resolve it
Code:
if($r->venue_id == 0) {
$venue_id = 0;
} else {
foreach($volunteersvenue as $r) {
$venue_id = $r->volunteers_venue_id;
}
}
This fixes it, but I have a feeling that I could do this better, since there is some hacking going on in the update view file.