CodeIgniter Forums
Select box with names - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Select box with names (/showthread.php?tid=17108)

Pages: 1 2 3


Select box with names - El Forum - 03-25-2009

[eluser]mdcode[/eluser]
I have a table with a list of names, like this:
Code:
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(5)      | NO   | PRI | NULL    | auto_increment |
| first_name | varchar(50) | NO   |     |         |                |
| last_name  | varchar(50) | NO   |     |         |                |
| active     | int(1)      | NO   |     | 1       |                |
+------------+-------------+------+-----+---------+----------------+

Now in my view, I am using the form helper to create a select box with this line:
Code:
<?php echo form_dropdown('rep', $reps, '', $selstyle); ?>

I can pull a list of the names that are in the table with this query in my model:
Code:
/* get all representatives */
function get_all_reps()
{
    $this->db->select('*');
    $this->db->from('reps');
    $this->db->where('active', '1');
    $this->db->order_by('first_name');
                
    $query = $this->db->get();
    return ($query->num_rows) ? $query->result_array() : FALSE;
}

And passing these names to my view with this part of the controller:
Code:
/* grab the rep list */
if ($reps = $this->projects_model->get_all_reps())
{
    foreach ($reps as $key=>$value)
    $data['reps'][$value['id']] = $value['first_name'];
}

This all works well, so you might ask what's the problem? I want to be able to display both the first_name and the last_name of the representatives in the table, but I can't figure out how to write the code that passes both names into the select box so the source code looks like this:
Code:
<select name="rep" style='width:160px;'>
<option value="1">Joe Bloggs</option>
<option value="2">Jane Doe</option>
</select>

Any help on this would be greatly appreciated.


Select box with names - El Forum - 03-25-2009

[eluser]jedd[/eluser]
Why not just:

Code:
if ($reps = $this->projects_model->get_all_reps())
    foreach ($reps as $key=>$value)
         $data['reps'][$value['id']] = $value['first_name'] ." ". $value['last_name'];



Select box with names - El Forum - 03-25-2009

[eluser]mdcode[/eluser]
Man am I such a fricking idiot... I was doing something like that with the previous system before I started trying to convert it all over to CI, I should've seen something like this...

Thanks muchly jedd!!


Select box with names - El Forum - 03-25-2009

[eluser]jedd[/eluser]
No wuckers. If it's any consolation, I did this a couple of days ago, after spending quite some time trying to work out how to get an array, straight from a db lookup, that could be fed directly into a form select .. but once I gave that fantasy up, this (and a few other niggly things) became trivially easy. I think we get spoiled around here by seeing way too many elegant one-liners - well, I do at least - and start to feel a bit dumb if I have to go back to old-school PHP constructs. Wink


Select box with names - El Forum - 03-25-2009

[eluser]mdcode[/eluser]
Quick one that I think is along the same lines then... do you have any idea why this one will not work and how, if there is a way to incorporate the $substrates variable into the foreach loop? I have looked at the php documentation and I can't actually see a way, but as you know I'm new at this:

From my CONTROLLER:
Code:
/* grab the substrates list */
$substrates = $this->projects_model->get_all_substrates();
$subtypes = $this->projects_model->get_all_subtypes();
{
    foreach ($subtypes as $key=>$value)
    $data['subtypes'][$value['id']] = $value['substrates'] ." - ". $value['subtype'];
}

Do I need another foreach loop, and incorporate them somehow, if so how?

This is the queries from the MODEL if it's any help, but I'm pretty sure this would be the controllers job:
Code:
/* get all substrates */
function get_all_substrates()
{
    $this->db->select('*');
    $this->db->from('substrates');
    $this->db->order_by('substrate');
    
    $query = $this->db->get();
    return ($query->num_rows) ? $query->result_array() : FALSE;
}
            
/* get all subtypes */
function get_all_subtypes()
{
    $this->db->select('*');
    $this->db->from('subtypes');
    $this->db->order_by('ssid', 'subtype');
    
    $query = $this->db->get();
    return ($query->num_rows) ? $query->result_array() : FALSE;
}



Select box with names - El Forum - 03-25-2009

[eluser]mdcode[/eluser]
[quote author="jedd" date="1238038076"]No wuckers. If it's any consolation, I did this a couple of days ago, after spending quite some time trying to work out how to get an array, straight from a db lookup, that could be fed directly into a form select .. but once I gave that fantasy up, this (and a few other niggly things) became trivially easy. I think we get spoiled around here by seeing way too many elegant one-liners - well, I do at least - and start to feel a bit dumb if I have to go back to old-school PHP constructs. Wink[/quote]

Indeed, the help and code on here is fantastic and helpful - just yesterday I spent about an hour changing all my database tables and colum names to coincide with recommendation from you and the CI community.

Unfortunately, any old school to anyone else is still new stuff to me... Hopefully I'm getting there or I can at least understand it.


Select box with names - El Forum - 03-25-2009

[eluser]jedd[/eluser]
Give me an idea of the content of your database, and what your actual desired output is. A var_dump from the substrate / subtype arrays, as an alternative to the db dump. My gut feel is you either need to have a different method in your model that pulls this data out, properly, up front -- or your code will be something like this:

Code:
/* grab the substrates list */
$substrates = $this->projects_model->get_all_substrates();
$subtypes = $this->projects_model->get_all_subtypes();
{
    foreach ($subtypes as $key=>$value)
    $data['subtypes'][$value['id']] = $substrates[$subtype['substrate_id']] ." - ". $value['subtype'];
}

But, absent an understanding of the data, this is pure speculation.


Select box with names - El Forum - 03-25-2009

[eluser]mdcode[/eluser]
Ok, I'll leave that code for the moment as I would like to do this properly, otherwise it's probably not worth doing... This is the table structure for the two tables involved:
Code:
Substrate Table:
+-----------+-------------+
| Field     | Type        |
+-----------+-------------+
| id        | int(3)      |
| substrate | varchar(50) |
+-----------+-------------+

Subtype Table
+-----------+-------------+
| Field     | Type        |
+-----------+-------------+
| id        | int(3)      |
| ssid      | int(3)      |
| subtype   | varchar(50) |
+-----------+-------------+

The code above is the same for the Model, and the controller is as I posted above as well. The desired output viewing the source I want this:

Code:
<select>
<option value="1">Paper - Coated</option>
<option value="2">Paper - Uncoated</option>
<option value="3">Board - Batabak</option>
</select>

The part before the hyphen is the substrate, and the part after is the subtype, coming from the tables. As an added bonus, I would also like the Substrates to be option groups so the out put would be like this:

Code:
<select>
<optgroup>Paper</optgroup>
<option value="1">Paper - Coated</option>
<option value="2">Paper - Uncoated</option>
<optgroup>Board</optgroup>
<option value="3">Board - Batabak</option>
</select>

Once the form is submitted however, the result of this must be the id from the subtypes table.


Select box with names - El Forum - 03-25-2009

[eluser]jwindhorst[/eluser]
Sounds like it's more of a query issue really.

Code:
// in model
function get_paired_subs()
{
    $sql = "select subtype.id, subtype, substrate from subtype, substrate where subtype.ssid=substrate.id";
    // run query
    // were it me, I would fit the data into an array with your id being the key
    return $results;
}

// in controller
$substrates = $this->projects_model->get_paired_subs();
// remember $k is your id
foreach($substrates as $k=$v)
{
  // This part should be much easier now since you combined your data when pulled it from the DB

}



Select box with names - El Forum - 03-25-2009

[eluser]mdcode[/eluser]
Thanks jedd, while I have not tried this one yet, I was wondering on your query -- it's similar to the one I had worked out in the old system (without CI) but when I try to do it the same way in CI, I only get two results (incorrect as they had different substrates, and improperly formatted, just the subtypes) and that's it. I'm afraid I don't fully understand what you're doing in your query.

Is it helpful, or necessary to do a join with the two tables, where substrates.id = subtypes.ssid?

Thanks.