Jump to content

How to generate <select> <option>'s based on what is NOT already in the database


matthewst

Recommended Posts

Example table 1:

person: 1 oak_spaces: 5

person: 2 oak_spaces: 8

 

My first drop down will select the correct work number. The second drop down(the one listed below) will then display the appropriate number of options.

 

Example table 2:

person: 1 space: 2

person: 1 space: 3

person: 1 space: 5

 

How would I make my drop down only show options 1 and 4?

 

 

$sql="SELECT oak_spaces FROM data_table WHERE id = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
//the above query will get me the maximum number of options to display
//how do I set up my query (for table 2) and php to only echo 1 and 4? (in this example 5 is the max)

?>
<select name="quote_num" class="formTextbox" size="1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<?php
if ($row['oak_spaces'] > '20'){
?>
<option value="21">21</option>
<option value="22">22</option>
<?php
}
if ($row['oak_spaces'] > '22'){
?>
<option value="23">23</option>
<option value="24">24</option>
<?php
}
if ($row['oak_spaces'] > '24'){
?>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<?php
}
if ($row['oak_spaces'] > '28'){
?>
<option value="29">29</option>
<option value="30">30</option>
<?php
}
}
?>
</select>

 

Maybe a preg_match? Or a preg_not_match? :shrug:

Link to comment
Share on other sites

How would I make my drop down only show options 1 and 4?

 

You need to query the database for (and only for) the data you actually need. Your post doesn't make allot of sense to me or I would offer more help adjusting your query.

Link to comment
Share on other sites

Your post doesn't make allot of sense to me

 

Fair enough.

 

I have multiple tables in a database, one table holds the max number of widgets, somewhere between 20 and 30. For this I'll say max is 20. The first drop down on the form (the one NOT shown) gives me my $q variable that get_quote_num.php uses to dynamically display the second drop down (the one I displayed in post one). In another table I have listed users and the widgets that are already taken.

example:

user 1 widget 1

user 1 widget 3

 

My question is how do I write a query or some php to only show select options 2, 4-20?

 

$sql="SELECT oak_spaces FROM data_table WHERE id = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
//I assume the new query will need to go here
//something like
//$sql = "select widgets from table where user = user"
//which will give me 1 and 3
//now echo "<select>"
//!!!!!!right here, how do I NOT display<option value'1'>  but skip to 2 like below
?>
<select name="quote_num" class="formTextbox" size="1">
<option value="2">2</option>
<option value="4">4</option>
<option value="5">5</option>

Link to comment
Share on other sites

Got it!! I just have to use the following code for each <option>.

 

<?php
if(mysql_num_rows(mysql_query("SELECT whatev FROM some_table WHERE number = 1 AND person_id = '".$q."'"))){
echo "<option value='1' disabled>1</option>";
}else{
echo "<option value='1'>1</option>";
}
?>

 

BTW Andrew, have you seen Bubba Ho-Tep?

Link to comment
Share on other sites

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.