Jump to content

optgroup array help


nottoolate

Recommended Posts

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

//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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.