Jump to content

Recommended Posts

Hi, Im not sure whether this is a PHP or MySQL question, but im trying to get a select menu to only display the number of options that are defined in the mysql database.

 

For example

 

The code I have that retrieves the $quantity from the database.

 

Lets say that

 

$quantity = 3

 

Now I would like the option menu to only display three options, like this:

<select name="quantity">
<option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option>
<option value="1">1</option> 
<option value="2">2</option>
<option value="3">3</option>
</select>

but for example, if the $quantity was equal to 10 then I would like the menu to be displayed as such

<select name="quantity">
<option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option>
<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>
</select>

How is it possible to do this? I am completely at a loss here.

 

Many Thanks

 

aquaman

Hi Barand, thanks for your reply. Do you mean like this? Because the menu is empty 

<select name="quantity">
<option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option>
<php
for ($q=1; $q<=$quantity; $q++) {
    echo '<option value="$q">$q</option>';
}
?>
</select>

Thanks

 

aquaman

Once again - are you one of those re-typers who post fake code that they "say" is what they ran but really isn't?  Please - only post ACTUAL code that ACTUALLY ran so that we don't all waste our time solving the wrong problem.

 

Your open php tag is not an open php tag, so nothing is happening in that loop.

 

Plus - it makes for easier typing, easier reading when you don't choose to go in and out of php mode like you are doing.  Just used php and echo the entire piece of code instead of only the php var.

 

 

<?php
echo '<select name="quantity">';
echo "<option value='$quantity'>Maximum of $quantity available</option>";
for ($q=1; $q<=$quantity; $q++) 
{
    echo "<option value='$q'>$q</option>";
}
echo '</select>';

I never said I ran anything or implied I ran anything, I simply asked for some advice on something I was stuck on. How could I post ACTUAL code when the whole post started as a question, and my reply to Barand. Im sure I have said it before but arent forums places to ask for help???

 

Anyway Thank you for your solution, it was just what I was looking for.

 

Many Thanks

 

aquaman

When one posts code and says the menu is empty the IMPLIED statement is that one has actually run that code.  Otherwise why offer it for display and say that results were bad?

 

Glad to have helped tho.

Hi ginerjm, I have made a change as I needed to forward the quantity along with some hidden form items to the payment page. But for some reason the code now reads

 

Maximum of 10 available

1

2

3

4

etc

 

but it is just echoing out the number and not in a select menu.

 

Here is how i have used it in the form

echo '<form action="regsale.php" method="POST">';
echo '<input type="hidden" name="username" value="<?php echo $username ?>';
echo '<input type="hidden" name="multipostage" value="<?php echo $multipostage ?>';
echo '<input type="hidden" name="speciesCommon" value="<?php echo $speciesCommon ?>';
echo '<input type="hidden" name="postage_cost" value="<?php echo $postage_cost ?>';
echo '<input type="hidden" name="cost" value="<?php echo $cost ?>';
echo '<input type="hidden" name="business" value="<?php echo $business ?>';
echo '<select name="quantity">';
echo "<option value='$quantity'>Maximum of $quantity available</option>";
for ($q=1; $q<=$quantity; $q++) 
{
    echo "<option value='$q'>$q</option>";
}
echo '</select>';


    
echo '<input type="submit" name="submit" value="Confirm Purchase">';
echo '</form>';

I cant see where I have gone wrong. I have re read your post and I cannot see whats happened.

 

Your help is much appreciated

 

aquaman

Hi ginejm,  I have had to change the way things work on my site. Now all I am doing is submitting hidden form items, and then a selection menu, the same as before. But for some reason, instead of echoing out the select menu, it is doing this


 


Maximum of 10 available


1


2


3


4


etc.


 


I rechecked the code you posted that was working, and cant see where I have gone wrong.


 


Here is my form



echo '<form action="regsale.php" method="POST">';
echo '<input type="hidden" name="username" value="<?php echo $username ?>"';
echo '<input type="hidden" name="multipostage" value="<?php echo $multipostage ?>"';
echo '<input type="hidden" name="speciesCommon" value="<?php echo $speciesCommon ?>"';
echo '<input type="hidden" name="postage_cost" value="<?php echo $postage_cost ?>"';
echo '<input type="hidden" name="cost" value="<?php echo $cost ?>"';
echo '<input type="hidden" name="business" value="<?php echo $business ?>"';
echo '<select name="quantity">';
echo "<option value='$quantity'>Maximum of $quantity available</option>";
for ($q=1; $q<=$quantity; $q++)
{
echo "<option value='$q'>$q</option>";
}
echo '</select>';
echo '<input type="submit" name="submit" value="Confirm Purchase">';
echo '</form>';

I cant see where I have gone wrong. I have re read your post and I cannot see whats happened.


 


Your help is much appreciated


 


 


aquaman

Edited by Paul_Withers

I got a quick fix but dont know why it makes any difference

echo '<form action="regsale.php" method="POST">';
echo '<input type="hidden" name="username" value="<?php echo $username ?>"';
echo '<input type="hidden" name="multipostage" value="<?php echo $multipostage ?>"';
echo '<input type="hidden" name="speciesCommon" value="<?php echo $speciesCommon ?>"';
echo '<input type="hidden" name="postage_cost" value="<?php echo $postage_cost ?>"';
echo '<input type="hidden" name="cost" value="<?php echo $cost ?>"';
echo '<input type="hidden" name="business" value="<?php echo $business ?>"';
echo '<input type="" name="">';      
echo '<select name="quantity">';
echo "<option value='$quantity'>Maximum of $quantity available</option>";
for ($q=1; $q<=$quantity; $q++) 
{
    echo "<option value='$q'>$q</option>";
}
echo '</select>';
    
echo '<input type="submit" value="Confirm Purchase">';
echo '</form>';

adding

 

echo '<input type="" name="">';      

 

seem to have solved the problem. Why is this?

 

Thanks

 

aquaman

your hidden input fields are all missing the closing html 

 

you should always validate your resulting html at validator.w3.org

 

also, you have php echo statements outputting the html. you don't put php <?php ... ?> tags within a php echo statement because it is already php code.

concatenation or deal with single and double quotes with escaping the doubles with a backslash \"

 

Here is an example wrapped with double quotes and single quotes within with the php variable concatenation

echo "<input type='hidden' name='username' value='" . $username . "'>";
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.