nottoolate Posted October 19, 2011 Share Posted October 19, 2011 Hello, I'm trying to create an select box utilizing the optgroup tag. The select box should list all the parent jobs as the label and the child jobs as the options. I have the parent jobs listing, but the problem is getting the child jobs to group with the correct parent job. Parent Jobs $parent_job_list = get_parent_job_list(); Child Jobs $child_job_list = get_child_job_list($job_id); <tr> <td style="text-align: right">Job:</td> <td> <select name="par" multiple="multiple" size="10"> <option value="0" selected>ALL</option> {section name=par loop=$par_job_list} <optgroup label="{$par_job_list[par][0]}, {$par_job_list[par][1]}"> {section name=job loop=$child_job_list} <option value="{$child_job_list[job][0]}">{$child_job_list[job][0]}, {$child_job_list[job][1]}</option> {/section} </optgroup> {/section} </select> </td> </tr> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2011 Share Posted October 19, 2011 That isn't PHP code. Wrong forum. Quote Link to comment Share on other sites More sharing options...
nottoolate Posted October 19, 2011 Author Share Posted October 19, 2011 it is PHP. The functions and arrays involved are the problem. The html/smarty portion is the working portion, I included it to show how its all put together. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2011 Share Posted October 19, 2011 Then you need to show the structure of the arrays that are created and state how you want the structure changed. From what you are saying the arrays are being created, just not in the format you need them. But, all you've shown (of PHP code) is a couple of lines that apparently call some custom methods that return data. How the hell are we supposed to know what format the data is returned or how you want that format to be? Quote Link to comment Share on other sites More sharing options...
creata.physics Posted October 19, 2011 Share Posted October 19, 2011 That isn't PHP code. Wrong forum. Are you sure? He posted it in this section, and I'm not sure, his array data looks like it's made with php. I'll give him the benefit of the doubt and say it is. It just maybe a template system that handles the special tags differently. I could very well be wrong. Regardless of it being php code or not, we will need to know what the array structure is like for your $parent_job_list and $child_job_list to even begin to start helping you. So please provide us with that information. Edit: Sorry, was a bit late in this post. Quote Link to comment Share on other sites More sharing options...
nottoolate Posted October 19, 2011 Author Share Posted October 19, 2011 //Parent Job List function get_parent_job_list(){ //Get parent job list $sql = "select job_id, name from job where (par_job_id is null and ((name like '%PJ%') or (name like '%PD%'))) or (par_job_id is null) order by job_id"; $rs = get_rs_array("db", $sql); return $rs; } //Child Job List function get_child_job_list($job_id){ //Get child job list $sql = "select job_id, name from job where ((par_job_id = ".$job_id.") or (job_id = ".$job_id.")) order by job_id"; $rs = get_rs_array("db", $sql); return $rs; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2011 Share Posted October 19, 2011 OK, so you provided how the lists are currently constructed, but you didn't state how you wanted them changed. I like to help, but I want to see that you at least put forth some effort. You will probably want to create a mufti-dimensional array. You could do it using the two methods you currently have - but that would mean running multiple queries to get all the child records. That is inefficient. You should instead have a new method to get ALL the data with a single query. Then create the mufti-dimensional array, then change your template code accordingly. Let me take a look and see what I can come up with and I'll post back. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2011 Share Posted October 19, 2011 After looking at your two queries I see that there is some unnecessary logic in the method get_parent_job_list(). Your WHERE clause is as follows where (par_job_id is null and ((name like '%PJ%') or (name like '%PD%'))) OR (par_job_id is null) That is logically the same as where par_job_id is null Those LIKE portions do nothing because the part before the last OR matches all records that par_job_id is null AND the name matches those conditions. But the part after the last OR matches ALL the records where par_job_id is null. Anyway, create the following method: function get_full_job_list() { $sql = "SELECT p.job_id as parent_id, p.name as parent_name, c.job_id as child_id, c.name as child_name FROM job AS p JOIN job as c ON c.parent_id = p.job_id WHERE p.par_job_id is null ORDER BY p.name, c.name"; $rs = get_rs_array("db", $sql); return $rs; } Note: I created the method to sort the results based on names as that would make sense to the user. You can change if needed Then call the method and format the results in a hierarchical array. $full_job_list = get_full_job_list(); $job_list = array(); foreach($full_job_list as $idx => $job) { if(!isset($job_list[$idx])) { $job_list[$idx] = array('p_id' => $job[0], 'p_name' => $job[1], 'jobs' => array()); } $job_list[$idx]['jobs'][] = array('c_id' => $job[2], 'c_name' => $job[3], } Lastly, change your smarty code to correspond to the hierarchical array. {section name=par loop=$job_list} <optgroup label="{$job_list[par]['p_id']}, {$job_list[par]['p_name']}"> {section name=job loop=$job_list[par]['jobs']} <option value="{$job_list[par]['jobs'][job]['c_id']}">{$job_list[par]['jobs'][job]['c_id']}, {$job_list[par]['jobs'][job]['c_name']}</option> {/section} </optgroup> {/section} Quote Link to comment Share on other sites More sharing options...
nottoolate Posted October 19, 2011 Author Share Posted October 19, 2011 Ok, thanks! That helped out a lot. I don't know much about php, etc. I was handed all this work, so it's harder for me to understand how everything works and comes together. Thanks again I appreciate it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.