Page 1 of 1

problems with an array

Posted: Thu Jul 12, 2007 4:30 pm
by nineseveninteractive
hi

i'm having a problem with the search function on the following site http://www.jasonmillward.com

basically when you use the drop to perform a search it all works fine and pulls the correct results from the database expect when you do a search for the first term in the array - in this case hotel / catering jobs where by the full results are shown.

below is the code that performs the search


Code: Select all

function career_level($c, $id) {
	
	$levels = array("Hotel / Catering Jobs", "Industrial / Warehouse Jobs", "Office Jobs", "Sales Jobs");

	if($id >= '0')
	{
		$level = $levels[$id];
		return $level;
	
	}
	else
	{
		$select = "<select name=\"careerlevel\">\n\t";

		while(list($k,$v) = each($levels))
		{
			if($c >= '0')
			{
				if($c == $k)
				{
					$select .= "<option value=\"$k\" selected>$v</option>\n\t";
				}
				else
				{
					$select .= "<option value=\"$k\">$v</option>\n\t";
				}
			}
			else
			{
				$select .= "<option value=\"$k\">$v</option>\n\t";
			}
		}

		$select .= "</select>\n\n";

		return $select;
	}
}
coud anyone tell me why thsi is happening and how i would go about correcting the problem. i've been trying for a few days with no joy

many thanks in advance

Posted: Thu Jul 12, 2007 5:54 pm
by feyd
I'm not quite sure how your problem and this function connect.

Posted: Thu Jul 12, 2007 6:22 pm
by RobertGonzalez
You might be able to clean that code a little to start...

Code: Select all

<?php
function career_level($c, $id) { 
    $levels = array("Hotel / Catering Jobs", "Industrial / Warehouse Jobs", "Office Jobs", "Sales Jobs"); 
    
    // This does not need quotes
    // Also, what if $id is 45667564? Perhaps you could check ...
    if($id >= 0 && $id <= count($levels)) { 
        return $levels[$id]; // No need for an assignment either
    } else { 
        $select = '<select name="careerlevel">' . "\n\t";
        
        // I am more of the foreach guy myself, mainly because of the reset pointer
        foreach($levels as $k => $v) { 
            // Again, no need for quotes
            // In fact, no need for this equality check here
            $selected = $c == $k ? ' selected="selected"' : '';
            $select .= '<option value="' . $k . '"' . $selected . '>' . $v . '</option>' . "\n\t"; 
        } 
        $select .= '</select>' . "\n\n"; 
        return $select; 
    } 
}
?>