This topic is locked

Dynamic Dropdown Box

9/22/2011 8:42:50 AM
PHPRunner General questions
cyberjas2001 author

Hello everybody,

Im trying to build a dropdown box based on this snippet



function Oncontcount_snippet()

{

$query = "SELECT Company, COUNT(*) AS cat_num FROM container where `Condition`='Parked' GROUP BY Company";

$result = mysql_query($query);

while($row = mysql_fetch_array($result))

{

echo "$row[Company] = $row[cat_num]
";

}

}


This query will show Companies with Parked Units only, and also how many of them are being parked at our location. This is the result:

Company A = 2

Company E = 4

Company M = 1

Company X = 5
Which is right, but now I want to trasform that Query into a Dropdown Box?

Any ideas?

Thanks!!!

C
cgphp 9/22/2011
function Oncontcount_snippet()

{

$query = "SELECT Company, COUNT(*) AS cat_num FROM container where `Condition`='Parked' GROUP BY Company";

$result = mysql_query($query);

$combo = "<select name='company'>";

while($row = mysql_fetch_array($result))

{

$combo .= "<option value='".$row['cat_num']."'>".$row['Company']."</option>";

}

$combo .= "</select>";

echo $combo;

}
cyberjas2001 author 9/22/2011

Thanks for your reply but it just shows the "companies" without the actual "parked units" count

C
cgphp 9/22/2011

Parked units are stored as valuess of the options. If you also want to show the cat_num as label, try out this code:

function Oncontcount_snippet()

{

$query = "SELECT Company, COUNT(*) AS cat_num FROM container where `Condition`='Parked' GROUP BY Company";

$result = mysql_query($query);

$combo = "<select name='company'>";

while($row = mysql_fetch_array($result))

{

$combo .= "<option value='".$row['cat_num']."'>".$row['Company']." = ".$row['cat_num']."</option>";

}

$combo .= "</select>";

echo $combo;

}
cyberjas2001 author 9/23/2011

Ok... got it, works great. THANKS
But now I want to show the last company selected in the combobox, it changes to "Select Company" everytime.

C
cgphp 9/23/2011
function Oncontcount_snippet()

{

$query = "SELECT Company, COUNT(*) AS cat_num FROM container where `Condition`='Parked' GROUP BY Company";

$result = mysql_query($query);

$combo = "<select name='company'>";

while($row = mysql_fetch_array($result))

{

$selected = (isset($_POST['company']) AND $_POST['company'] == $row['Company']) ? ' SELECTED' : '';

$combo .= "<option value='".$row['Company']."'".$selected.">".$row['Company']." = ".$row['cat_num']."</option>";

}

$combo .= "</select>";

echo $combo;

}
cyberjas2001 author 11/3/2011

Thanks for your help Cristian, now let me ask you something. How can I reset the search values on my list page?

C
cgphp 11/3/2011

The button "Show all", near the search box, reset the search result.

cyberjas2001 author 11/4/2011



The button "Show all", near the search box, reset the search result.


I know that, but still when you click on it the fields on the search option are full with the previous data. So basically it doesnt clear the fields it just clear the screen and filters.

Any other way to do it?