Click to See Complete Forum and Search --> : PHP MySQL drop down menu question


welsh
11-23-2006, 07:20 PM
hey, im editing the placement of an image in a gallery. right now when i click edit the image, the name, caption, and what gallery it is in appears. i want the gallery to appear in a drop down menu, however i want wat ever gallery it is in to appear first. So say if i had something like the gallerys: christmas, new years and easter. and the picture i was editing was in the new years gallery the drop down menu would be:
new years
christmas
easter
now the code im sort of confused on here what ive got so far:

$group = $_POST['id'];

$connection = mysql_connect($host,$user,$password)
or die ('Couldnt connect: '.$connection.' - Error: '.mysql_error());
$db = mysql_select_db($database, $connection)
or die ('Couldnt select database: '.$db.' - Error: '.mysql_error());

$query = "SELECT * FROM galleryDirect";
$result = mysql_query($query) or die ('Couldnt run query: '.$query.' - Error: '.mysql_error());

echo '<select name="gallery" size=1>';
while($row = mysql_fetch_array($result)){
if ($group == $row['id']) {
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option> \n';
}
while($id != $row['id'] and $row = mysql_fetch_array($result)){
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option> \n';
}
}

echo '</select>';

}

now after reading this code i realized it is extremely flawed. as it run the if statement in every loop! i hope i explained it enough, any help would be great.

chazzy
11-23-2006, 07:44 PM
why do you have the while inside of the while?

just do


while($row = mysql_fetch_array($result)){
if ($group == $row['id']) {
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option> \n';
}
else{
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option> \n';
}
}


Even then, why do you differentiate them? just to make the group appear first?

If you use an array, or actual classes/data structures instead of just printing out everything in the database, this would work much better.

welsh
11-23-2006, 08:05 PM
um, how would i do this? i just want to have which gallery it is in first appear first to show it is in that gallery, then the other options are the other gallerys you can change it to. im not sure why i had a while inside a while.

chazzy
11-23-2006, 08:20 PM
create an array of arrays (i try avoiding calling this a multidimensional array)


$currentCategory = array('id','name');
$otherCategories = array();
while($row = mysql_fetch_array($result)){
if ($group == $row['id']) {
$currentCategory['id'] = $row['id'];
$currentCategory['name'] = $row['name'];
}
else{
$myCategory = array('id','name');
$myCategory['id'] = $row['id'];
$myCategory['name'] = $row['name'];
$otherCategories[] = $myCategory;
}
}

Then echo out the values when you need to.