princeads Posted February 17, 2008 Share Posted February 17, 2008 Hi Guys, I've got a problem when trying to fill a select (multples allowed) menu with data from the database. It seems to retrieve the data fine, but I have an inner loop that tries to find any matches with its id to the outer loop and then select the value or not. Any help on this would be much apprieciated. <select id="pageID" multiple="multiple" size="20" name="pageID[]"> <?php while ($row4 = mysql_fetch_assoc($result4)){ while ($row5 = mysql_fetch_assoc($result5)) { if($row4['ID'] == $row5['pageID']) { ?> <option selected="selected" value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option> <?php } else{?> <option value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option> <?php } } }?> </select> Quote Link to comment Share on other sites More sharing options...
Barand Posted February 17, 2008 Share Posted February 17, 2008 I'm assuming this situation. Where match in the two tables, show selected. If no match then, because we LEFT JOIN, pageID from table5 will have null value. [pre] table4 table5 -------- --------- ID ------------< pageID title secondtitle [/pre] <?php $sql = "SELECT a.ID, a.title, a.secondtitle, b.pageID FROM table4 a LEFT JOIN table5 b ON a.ID = b.pageID"; echo '<select id="pageID" multiple="multiple" size="20" name="pageID[]">'; $res = mysql_query ($sql) or die (mysql_error()); while (list($id, $title, $title2, $page) = mysql_fetch_row($res)) { $sel = $page ? 'selected' : ''; echo "<option $sel value='$id'>$title $title2</option>"; } echo '</select>'; Quote Link to comment Share on other sites More sharing options...
princeads Posted February 17, 2008 Author Share Posted February 17, 2008 Hiya thanks for your help, really is appriciated. I think what your saying is what I'm trying to achieve, to give it more background I have a pluginpages table that shows all the pages and their plugins. There is also a cmspages table to record all the pages on the site. Therefore I'm trying to provide a select menu to allow the user to first see which pages their plugin is on and then secondly change the select menu if they want it to appear on different pages. The code you provided didn't seem to work unfortunately. Here is more of my original code. $query4 = "SELECT * FROM cmspages"; $result4 = mysql_query($query4) or die("ERROR: Select page details problem"); $query5 = "SELECT * FROM cmspluginpages WHERE pluginID = '$pluginID'"; echo $query5; $result5 = mysql_query($query5) or die("ERROR: Select plugin details problem"); <select id="pageID" multiple="multiple" size="20" name="pageID[]"> <?php while ($row4 = mysql_fetch_assoc($result4)){ while ($row5 = mysql_fetch_assoc($result5)) { if($row4['ID'] == $row5['pageID']) { ?> <option selected="selected" value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option> <?php } else{?> <option value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option> <?php } } }?> </select> Am I just being an idoit with not getting your code to work?? Sorry for the trouble, having one of those days! cheers Adam Quote Link to comment Share on other sites More sharing options...
Barand Posted February 17, 2008 Share Posted February 17, 2008 how about <?php $sql = "SELECT a.ID, a.title, a.secondtitle, b.pageID FROM cmspages a LEFT JOIN cmspluginpages b ON a.ID = b.pageID AND b.pluginID = '$pluginID' "; echo '<select id="pageID" multiple="multiple" size="20" name="pageID[]">'; $res = mysql_query ($sql) or die (mysql_error()); while (list($id, $title, $title2, $page) = mysql_fetch_row($res)) { $sel = $page ? 'selected' : ''; echo "<option $sel value='$id'>$title $title2</option>"; } echo '</select>'; Quote Link to comment Share on other sites More sharing options...
princeads Posted February 17, 2008 Author Share Posted February 17, 2008 Legend! Thanks Barand for all your help, work perfect! Cheers so much, Adam Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.