Jump to content

Select with dynamic results


Go to solution Solved by cyberRobot,

Recommended Posts

Hello

 

I have a html select with static options 1, 2 ,3 ,4 ,5, 6, 7, 8, 9, 10  which represents how many items have been sold.

I have a select query that finds the qty of the items and I want force it so that the number of select options represents the qty. For example if there is only  4 qty the select options should be 1, 2, 3, 4 and not go any further. I'm really not sure how to do this though, can you help me please?

 

Thank you

<?php $qty = $build['qty'];?>

Items sold: <select name="item_sold">
    <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>
Link to comment
https://forums.phpfreaks.com/topic/297325-select-with-dynamic-results/
Share on other sites

Generate the qty field using a function. Example

function genQtyList($name, $qtyMax = 10, $qtyMin = 1)
{
    $html = "\n<select name=\"$name\">\n";

    for($i = $qtyMin; $i <= $qtyMax; $i++)
        $html .= "\t<option>$i</option>\n";
    $html .= "</select>\n";

    return $html;
}

// by default generates options 1 to 10
echo 'Qty: ' . genQtyList('qty1');
echo '<br />';

// pass maximum qty as second argument, generates options 1 to 4
echo 'Items sold: ' . genQtyList('item_sold', 4);

You pass the name of the field as the first argument. The second argument sets the maximum qty. Optionally set the minimum qty as the third argument

  • Like 1

Thank you very much this is really helpful!

 

How can I assign another variable to the $qtyMax? At the moment it seems I must add a max number?

 <?php $qty = $build['qty'];?>


function genQtyList($name, $qtyMax = 10, $qtyMin = 1)

function genQtyList($name, $qtyMax = $qty $qtyMin = 1)

Thank you

Hello is there away to undeclare this function after? i ask because it is nested between a while loop and for the next record displays an error:  Fatal error: Cannot redeclare genQtyList()

 

I've tried:

$declare = function genQtyList($item_sold, $qtyMax = '$qty', $qtyMin = 1)
{
    $html = "\n<select name=\"$item_sold\">\n";

    for($i = $qtyMin; $i <= $qtyMax; $i++)
        $html .= "\t<option>$i</option>\n";
    $html .= "</select>\n";

    return $html;
}

// pass maximum qty as second argument, generates options 1 to 4
echo 'Items sold: ' . genQtyList('item_sold', $qty);

unset($declare);?>

But i just get a white screen.

Edited by yandoos

Ok I will post the code below and attach a screen print so you can see what i am trying to achieve.

I have a number of records that are displayed with various status. All those records with a status = Delivered the select menu is shown where the user can select the number of items sold. This then updates the database accordingly. I wanted it so each select menu only offers the possible max of the items they have (as detailed in $qty).

 

Thank you so much, I really appreciate it. :)

<?php
  
  
  ini_set('display_errors', 1); 
error_reporting(E_ALL);
include "include/session.php";
  include('config.php'); 
  $email = $_SESSION['mem_id'];

   
	  

?>
<!DOCTYPE html>
<html>
<head>
<title>Members Area</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>

<p>Welcome! <?php echo $_SESSION['userid']; ?></p>

<?php 

$finder = mysqli_query($connection,"SELECT mem_id, user_level FROM plus_signup WHERE mem_id = '$email'")  or die (mysql_error());
while($builder = mysqli_fetch_array($finder))
    {   
	$user =  $builder['mem_id']; 
	$userlevel =  $builder['user_level']; }


$data = mysqli_query($connection,"
SELECT * FROM distribution join product ON distribution.pid = product.pid
                                  join plus_signup ON distribution.mem_id = plus_signup.mem_id
					WHERE distribution.mem_id = '$user' AND  NOT distribution.status = 'Rejected' ORDER BY FIELD(status, 'Offered', 'Delivered','Dispatched','Accepted', 'Complete', 'Rejected')")  or die (mysql_error());
	
	
	
					
	if($userlevel == '1') { 
	
	echo '<div class="admin-controls"> Products | New Clients | Distributions </div> ';
	}else {
	echo 'not admin';}				
					
					
								  
		if(count($data)) {  				  
echo '<table class="tables" width="100%" border="0">';
								  
while($build = mysqli_fetch_array($data))
    {  
	
$qty =  $build['qty'];	

$test =  $build['pdid'];

echo'<br/>';


	
if ($build['status'] == 'Offered') {
echo '<tr><td valign="top" style="border:1px dashed #1fa91f;"><img src="images/1.jpg" style="float:right;"/><p>';

} else { echo '<tr><td valign="top"><img src="images/1.jpg" style="float:right;"/><p>';}
	
	
	
	echo 'Name:';
	echo $build['productname']; 
	echo '</p><p>Description:';
	echo $build['description']; 
	echo '</p><p>Price:';
	echo $build['price'];
	echo '</p><p>Qty:';
	echo $build['qty'];
	
		
	if ($build['status'] == 'Offered') {?>


<FORM name ="form1" method ="post" action ="accept_order.php">
<input name="pdid" type="hidden" value="<?php echo $build['pdid'];?>" />
<input name="pid" type="hidden" value="<?php echo $build['pid'];?>" />
<input name="qty" type="hidden" value="<?php echo $build['qty'];?>" />
<input name="stock" type="hidden" value="<?php echo $build['stock'];?>" />

<Input type = "Radio" Name ="order" value= "Accepted" style="width:20px; vertical-align: baseline; margin-top:0px;"><strong>Accept this item?</strong>

<Input type = "Radio" Name ="order" id="radio1" value= "Rejected" style="width:20px; vertical-align: baseline; margin-top:0px;"><strong>Reject this item?</strong>


<Input type = "Submit" Name = "Submit1"  id="mysubmit" VALUE = "Submit" >

</FORM>


<?php }	
	
	else if ($build['status'] == 'Delivered' || $build['status'] == 'Complete')   {
	
	
	
	echo '</p><p>Status:';
	echo $build['status']; 
	
	echo '</p><p>'; 
	
	
	if ($build['qty'] > 0) { 
	
	$connection = mysqli_connect($dbhost_name, $username, $password, $database);
$sql1 = mysqli_query($connection,"SELECT qty FROM distribution WHERE pdid = '$test' AND status = 'Delivered'" ) ; 
while($build1 = mysqli_fetch_array($sql1))
    {  
    
    $qty = $build1 ['qty'];
    echo $build1 ['qty'];
    $qtyMax = '1';
    
    }?>
	
	<FORM name ="form2" method ="post" action ="item_sold.php">
<input name="pdid" type="hidden" value="<?php echo $build['pdid'];?>" />
<input name="pid" type="hidden" value="<?php echo $build['pid'];?>" />
<input name="qty" type="hidden" value="<?php echo $build['qty'];?>" />
<input name="stock" type="hidden" value="<?php echo $build['stock'];?>" />
<input name="price" type="hidden" value="<?php echo $build['price'];?>" />
<input name="balance" type="hidden" value="<?php echo $build['balance'];?>" />

<?php 

 function genQtyList($item_sold, $qtyMax = '$qty', $qtyMin = 1)
{
    $html = "\n<select name=\"$item_sold\">\n";

    for($i = $qtyMin; $i <= $qtyMax; $i++)
        $html .= "\t<option>$i</option>\n";
    $html .= "</select>\n";

    return $html;
}

// pass maximum qty as second argument, generates options 1 to 4
echo 'Items sold: ' . genQtyList('item_sold', $qty);


?>

<Input type = "Submit" Name = "Submit2"  id="mysubmit" VALUE = "Submit" >

</FORM>	
	
	<?php
	
	}echo '<p style="color:#d42626;">Balance Owed: £';echo $build['balance'];}
	
	else {echo '</p><p>Status:';
	echo $build['status']; 
	
	echo '</p><p>';}
	 
	 echo '</td></tr>';			
	}
	
	echo '</table>';
	} else {echo'No';}		
	
	echo '';	
	?>	
	
  </body>
  </html>
  

print.png

  • Solution

The problem is caused by the function definition being made inside a loop. The code should work if you move the definition as mentioned by mac_gyver. You could try something like the following:

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
include "include/session.php";
include('config.php'); 
$email = $_SESSION['mem_id'];
 
function genQtyList($item_sold, $qtyMax = '$qty', $qtyMin = 1) {
    $html = "\n<select name=\"$item_sold\">\n";
    for($i = $qtyMin; $i <= $qtyMax; $i++)
        $html .= "\t<option>$i</option>\n";
    $html .= "</select>\n";
 
    return $html;
}
?>
  • Like 1

Thank you very much that's really done it!

 

So to take this a step further I could add this function to a functions.php file and simply call the function on this page at the top? So I  don't need to have the actual function on the page?

 

include "include/functions.php";

So to take this a step further I could add this function to a functions.php file and simply call the function on this page at the top? So I  don't need to have the actual function on the page?

 

include "include/functions.php";

 

If the function is needed for multiple pages on your website, then it would be a good idea to save the definition in an external file and import it as needed. Note that you would want to use require instead of include since the function is critical to the operation of the page. More information about require can be found here:

http://php.net/manual/en/function.require.php

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.