complex05 Posted March 15, 2007 Share Posted March 15, 2007 Hello, How do I remove an element from an array? Here is my current code $sql = mysql_query("SELECT suitenos FROM gssettings WHERE id=1"); $row = mysql_fetch_assoc($sql); $suites = split(",",$row['suitenos']); $number_of_suites = count($suites); for($i=0;$i<$number_of_suites;$i++) { if($suiteno == $suites[$i]) { $selected = "SELECTED"; } else { $selected = ""; } echo<<<endhtml <option value="$suites[$i]" $selected>$suites[$i]</option> endhtml; } Now I have another table in my database called "gsdays" that holds values of suite numbers that are already booked. What I want to do is remove any suite numbers that are already booked from my $suites array. To get the booked suites, I would do the following query. $query = mysql_query("SELECT suite FROM gsdays WHERE date='2007-03-24'"); $row = mysql_fetch_assoc($query); $booked_suites = $row['suite']; so basically what I want to do is remove $booked_suites from $suites. I tried using array_splice but I don't know what I'm doing Quote Link to comment https://forums.phpfreaks.com/topic/42851-remove-element-from-array/ Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 first of all, there is a MUCH easier way of executing your current code: <?php $sql = "SELECT suitenos FROM gssettings WHERE id = 1"; $query = mysql_query($sql) OR die(mysql_error()); $i = 0; echo "<select name=\"drop_down\">\n"; while($row = mysql_fetch_array($query)){ $suites = implode("," $row['suitenos']); echo "<option value=\"{row['suitenos']}\"". (($row['suitenos'] == $suites[$i]) ? (" SELECTED") : ("")) .">\n"; $i++; } echo "</selected>\n"; ?> second, you use the unset() function to remove an element of an array. like this: <?php $sql = "SELECT suitenos FROM gssettings WHERE id = 1"; $query_settings = mysql_query($sql) OR die(mysql_error()); $suites = mysql_fetch_assoc($query_settings); $sql = "SELECT suite FROM gsdays WHERE date = '{$date}'"; $query_booked = mysql_query($sql) OR die(mysql_error()); $booked = mysql_fetch_assoc($query_booked); foreach($suites as $suite){ foreach($booked as $book){ if($suite == $book){ $unset($suite); } } } $sql = "SELECT suitenos FROM gssettings WHERE id = 1"; $query = mysql_query($sql) OR die(mysql_error()); echo "<select name=\"drop_down\">\n"; while($row = mysql_fetch_array($query)){ echo "<option value=\"{row['suitenos']}\""; foreach($suites as $suite){ (($row['suitenos'] == $suite) ? (" SELECTED") : ("")) } echo ">\n"; } echo "</selected>\n"; ?> that should work. Quote Link to comment https://forums.phpfreaks.com/topic/42851-remove-element-from-array/#findComment-208089 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 i almost forgot... you have to print something after your option tag, otherwise your dropdown will be empty. replace the while loop with this: while($row = mysql_fetch_array($query)){ echo "<option value=\"{row['suitenos']}\""; foreach($suites as $suite){ (($row['suitenos'] == $suite) ? (" SELECTED") : ("")); } echo ">{$row['suitenos']}\n"; } i also forgot to add a semicolon after my if statement inside the foreach. notice i added it here. Quote Link to comment https://forums.phpfreaks.com/topic/42851-remove-element-from-array/#findComment-208118 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.