Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/42851-remove-element-from-array/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.