Forum Moderators: open

Message Too Old, No Replies

Php / Sql Droplist Function

         

m8fyu

10:20 am on Jun 20, 2007 (gmt 0)

10+ Year Member



i am having a few troubles trying to populate a drop down menu using php / sql and would welcome any guidance or links that may help me get my head round this scripting malarkey. I usually work on front end projects but have recently been thrown into the deep end and have ended up being the person responsible for maintaining the code. Grey hairs have started to appear at my temples since trying to work out arrays and their syntax. I'm getting there but would really appreciate any help.

i have a db with table named zt_locations. which lists aevery location in our system. unfortunately the table is a bit scruffy because we have everything jammed into just the one table. i would have preferred regional info to be in a separate table. anyway, we have a parent_id field so anything with parent id of 'world' would be a region or continent. then we

have all countries and cities listed within this same table. each has a parent_id to identify the parent location.

eg:
Asia - parent_id='world'
Thailand - parent_id='asia'
Bangkok - parent_id='thailand'

i would like to populate the drop down menu so that firstly the region is identified (using parent_id = 'world') and then all countries in the region listed below. something along the lines of:

Africa
- Madagascar
- South Africa
- Tanzania
Asia
- Loc1
- Loc2
- Loc3
- Loc4
Caribbean
- Loc1
- Loc2
- Loc3

I'd also like to create a separate list that lists the Country and then all locations in each country:

Australia
- Sydney
- Port Lincoln

Bahamas

etc

I'm working with existing code which is rather confusing to me. There are built in functions that are referred to whch I think are rather self explanatory but

as for the coding itself...well that confuses the heck out of me although I am making some headway in figuring out just how things work.

function droplist_locations($arr)
{
$major = false;

if($arr[0] == "1")
$major = true;

if(!empty($arr[1]))
{
$str = "<form method='POST' action='".$this->_parent->call_page."'>";
$str .= "<input type='hidden' name='pageid' value='".$arr[1]."'>";
}

$str .= "<select name='locid'>";

$qry="SELECT * FROM zt_locations WHERE active=1 AND parent_id in (".$this->find_region().") ORDER BY name";

$this->_parent->db->readx($qry);
$rc = $this->_parent->db->resultcount();
$dat = $this->_parent->db->data;

for($i=0;$i<$rc;$i++)
{
$str.="<option value='".$dat[$i]["id"]."'>"; //location id
$s=$dat[$i]["name"]; //name
$x = $this->_parent->xtranslate($dat[$i]['id'], "zt_locations", "name");

if($x!=$dat[$i]['id'])
$s = $x;
$str.=$s;
$str.="</option>";
}

$str .= "</select>";

if(!empty($arr[1]))
{
$str .= "<input type='submit' value='Go'>";
$str .= "</form>";
}
return $str;
}

Thanks in advance to all you scripting guru's. I will get there one day but at this time I am content being jealous of you all.