yandoos Posted July 16, 2015 Share Posted July 16, 2015 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> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 16, 2015 Share Posted July 16, 2015 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 1 Quote Link to comment Share on other sites More sharing options...
yandoos Posted July 16, 2015 Author Share Posted July 16, 2015 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 Quote Link to comment Share on other sites More sharing options...
yandoos Posted July 16, 2015 Author Share Posted July 16, 2015 I see i can add it in the bottom bit: // pass maximum qty as second argument, generates options 1 to 4 echo 'Items sold: ' . genQtyList('item_sold', $qty); Thank you very much Quote Link to comment Share on other sites More sharing options...
yandoos Posted July 16, 2015 Author Share Posted July 16, 2015 (edited) 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 July 16, 2015 by yandoos Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 16, 2015 Share Posted July 16, 2015 You shouldn't need to re-declare the function... Could you post the code which shows all the calls / decorations of genQtyList()? Quote Link to comment Share on other sites More sharing options...
yandoos Posted July 16, 2015 Author Share Posted July 16, 2015 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> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 16, 2015 Share Posted July 16, 2015 function definitions should go near the start of your code or be in a separate file that you include near the start of your code. Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted July 16, 2015 Solution Share Posted July 16, 2015 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; } ?> 1 Quote Link to comment Share on other sites More sharing options...
yandoos Posted July 16, 2015 Author Share Posted July 16, 2015 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"; Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 16, 2015 Share Posted July 16, 2015 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 Quote Link to comment 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.