You are here: Re: Drop Down Lists « PHP Programming Language « IT news, forums, messages
Re: Drop Down Lists

Posted by Rik on 05/16/06 23:05

TristaSD wrote:
> Very effifient. Mine is a monster - I did a query inside a query:
>
> $locationquery = mysql_query ("SELECT * FROM locations");
> $comparequery = mysql_query ("SELECT location FROM reservations WHERE
> id=" . $_GET['id']);
> while ($compareresult = mysql_fetch_array ($comparequery)) {
> while ($locationrow = mysql_fetch_array ($locationquery)) {
> echo ("<option");
> if ($locationrow['name'] == $compareresult['location']) {
> //the magic happens here
> echo (" selected");
> }
> echo (">$locationrow[name]</option>");
> }
> }
>
> Because $comparequery fetches only one value, any way to retrieve it
> BEFORE going into the forst loop?


First of all, you don't need the first while loop,. I assume it's only on
record that has that certain id, so you can just:
$compareresult = mysql_fetch_array ($comparequery)
while ($locationrow = mysql_fetch_array ($locationquery)) {
if ($locationrow['name'] == $compareresult['location']) {
echo (" selected");
}
}

It's possible in one query:

if(isset($_GET['id'])&&$_GET['id']!=''){
$id = mysql_rel_escape_string(GET['id']);
$query = "SELECT x.name, IF(x.name=y.location, ' selected=\"selected\"',
'') as `selected` FROM locations x, reservations y WHERE y.id=$id";
} else {
$query = "SELECT name, '' as `selected` FROM locations";
}

$locationquery = mysql_query($query);
while ($locationrow = mysql_fetch_array ($locationquery)){
printf('<option%s>%s</option>', $locationrow['selected'],
$locationrow['name']);
}

I'm not enterily sure it's quicker though... My MySQL is still not as it
should be.

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация