mysql - Automatically reselect a dropdown Option with PHP -


i hoping can help.

my code below , hoping when user submits form, option value selected remains @ top of drop down list.

any appreciated.

<?php require_once('connections/reps.php'); ?> <?php function getpercent ($amount, $total)  {     $count1 = $amount / $total;     $count2 = $count1 * 100;     $count = number_format($count2, 0);     return $count; } $countdate = time(); ?> <?php  if (!function_exists("getsqlvaluestring")) { function getsqlvaluestring($thevalue, $thetype, $thedefinedvalue = "", $thenotdefinedvalue = "")  {   if (php_version < 6) {     $thevalue = get_magic_quotes_gpc() ? stripslashes($thevalue) : $thevalue;   }    $thevalue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($thevalue) : mysql_escape_string($thevalue);    switch ($thetype) {     case "text":       $thevalue = ($thevalue != "") ? "'" . $thevalue . "'" : "null";       break;         case "long":     case "int":       $thevalue = ($thevalue != "") ? intval($thevalue) : "null";       break;     case "double":       $thevalue = ($thevalue != "") ? doubleval($thevalue) : "null";       break;     case "date":       $thevalue = ($thevalue != "") ? "'" . $thevalue . "'" : "null";       break;     case "defined":       $thevalue = ($thevalue != "") ? $thedefinedvalue : $thenotdefinedvalue;       break;   }   return $thevalue; } }  $currentpage = $_server["php_self"];  $maxrows_orders = 10; $pagenum_orders = 0; if (isset($_get['pagenum_orders'])) {   $pagenum_orders = $_get['pagenum_orders']; } $startrow_orders = $pagenum_orders * $maxrows_orders;  mysql_select_db($database_reps, $reps); $query_orders = "select orders.id id, customers.`name` customername, products.productname product, orders.quantity quantity, orders.salesprice salesprice, orders.price price, orders.paid paid, orders.ageofpayment ageofpayment, orders.orderdate orderdate, products.price productprice, staff.staffname staffmember, orders.bonus bonus orders inner join staff staff on orders.staffmember = staff.id inner join products products on orders.product = products.id inner join customers customers on orders.customername = customers.id  1 = 1 ";  //the above makes true  // below takes customer name select input , filters based on selection. filters order  if (isset($_get['customername2']) && $_get['customername2'] != 'false' )  { $query_orders .= "and customername = '" .$_get['customername2']."' "; }  // makes sure product not false , filters  if (isset($_get['product']) && $_get['product'] != 'false')  { $query_orders .= "and product = '" .$_get['product']."' "; }  // below filters hte customer name  if (isset($_get['customername'])&& $_get['customername'] != 'false') { $query_orders .= "order '".$_get['customername']."' "; }   echo "$query_orders <br /> <br />";  $query_limit_orders = sprintf("%s limit %d, %d", $query_orders, $startrow_orders, $maxrows_orders); $orders = mysql_query($query_limit_orders, $reps) or die(mysql_error()); $row_orders = mysql_fetch_assoc($orders);   if (isset($_get['totalrows_orders'])) {   $totalrows_orders = $_get['totalrows_orders']; } else {   $all_orders = mysql_query($query_orders);   $totalrows_orders = mysql_num_rows($all_orders); } $totalpages_orders = ceil($totalrows_orders/$maxrows_orders)-1;  $querystring_orders = ""; if (!empty($_server['query_string'])) {   $params = explode("&", $_server['query_string']);   $newparams = array();   foreach ($params $param) {     if (stristr($param, "pagenum_orders") == false &&          stristr($param, "totalrows_orders") == false) {       array_push($newparams, $param);     }   }   if (count($newparams) != 0) {     $querystring_orders = "&" . htmlentities(implode("&", $newparams));   } } $querystring_orders = sprintf("&totalrows_orders=%d%s", $totalrows_orders, $querystring_orders);  $paid = $row_orders['paid'];  mysql_select_db($database_reps, $reps); $query_customers = "select name, id customers status = 'active'"; $customers = mysql_query($query_customers, $reps) or die(mysql_error()); $row_customers = mysql_fetch_assoc($customers); $totalrows_customers = mysql_num_rows($customers);  mysql_select_db($database_reps, $reps); $query_products = "select * products"; $products = mysql_query($query_products, $reps) or die(mysql_error()); $row_products = mysql_fetch_assoc($products); $totalrows_products = mysql_num_rows($products);   ?>  <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <?php   require_once("models/config.php"); if (!securepage($_server['php_self'])){die();} require_once("models/header.php");  ?> </head>  <body> <div id='wrapper'> <div id='top'>   <div id='logo'></div> </div> <div id='content'>   <div id='left-nav'>     <?php include("left-nav.php"); ?>   </div>   <div id='main'> <?php echo " hi, $loggedinuser->displayname. here able view orders managers perspective. " ?>   <div id = "filtertable"> <form method="get" action="<?php   ?>">   <table border="10" cellpadding="5" cellspacing="5">   <tr>       <td align="center">order by</td>       <td> <select name="customername"> <option value="false">select how order by</option> <option value="id">order id</option> <option value="customername">customer name</option> <option value="product">product</option> <option value="orderdate">order date</option> <option value="paid">paid?</option> </select></td>     </tr>     <tr>       <td align="center">customer name</td>             <td><select name="customername2">                <option value="false" >select customer name</option>                  <?php  {   ?>               <option value="<?php echo $row_customers['id']?>" ><?php echo $row_customers['name'];?></option>               <?php } while ($row_customers = mysql_fetch_assoc($customers)); ?>             </select></td>     </tr>     <td align="center">product</td>     <td><select name="product">   <option value="false" >select product</option>         <?php  {   ?>       <option value="<?php echo $row_products['id']?>" >       <?php echo $row_products['productname']?>       </option>       <?php } while ($row_products = mysql_fetch_assoc($products)); ?>     </select></td>   </tr>     <td align="center">order date</td>     <td><input type="text" name="email" value="" size="25" /></td>   </tr>     <td align="center">paid?</td>     <td><input type="text" name="email" value="" size="25" />  </td>   </tr>      <td colspan="2" align="center"> <input type="submit" value="submit filter" name="b1" />    </td>     </tr>   </table>   <p>&nbsp;</p> </form>    </div>       <div id ="orderstable">       <table border="10" cellpadding="5" cellspacing="5">         <tr>           <th>order id</th>           <th>customer name</th>           <th>product</th>           <th>quantity</th>           <th>sales price (€)</th>           <th>total sale (€)</th>           <th>list price (€)</th>           <th>price difference (€)</th>           <th>% difference</th>           <th>sold by</th>           <th>order date</th>           <th>age of payment</th>           <th>paid?</th>           <th>bonus %</th>           <th>bonus amount (€)</th>         </tr>         <?php { ?>           <tr>             <td align="center" valign="middle"><?php echo $row_orders['id']; ?></td>             <td align="center" valign="middle"><?php echo $row_orders['customername']; ?></td>             <td align="center" valign="middle"><?php echo $row_orders['product']; ?></td>             <td align="center" valign="middle"><?php echo $row_orders['quantity']; ?></td>             <td align="center" valign="middle"><?php echo "€"; echo number_format($row_orders['salesprice'], 2); ?></td>             <td align="center" valign="middle"><?php $first_multiply = $row_orders['quantity'];                                                $second_multiply = $row_orders['salesprice'];                                                $multiply_total = $first_multiply *  $second_multiply;                                                echo "€"; echo number_format(($multiply_total) , 2); ?></td>             <td align="center" valign="middle"><?php echo "€"; echo number_format($row_orders['price'] ,2); ?></td>             <td align="center" valign="middle"><?php echo "€"; $first_number = $row_orders['price'];                                                  $second_number = $row_orders['salesprice'];                                                 $sum_total = $second_number - $first_number;                                                 echo number_format(($sum_total) ,2); ?></td>             <td align="center" valign="middle"><?php $percent = getpercent($sum_total, $row_orders['price']);             echo $percent."%"; ?></td>             <td align="center" valign="middle"><?php echo $row_orders['staffmember']; ?></td>             <td align="center" valign="middle"><?php echo date('d/m/y', $row_orders['orderdate']); ?></td>             <td align="center" valign="middle"><?php        $currentorderdate = $countdate - $row_orders['orderdate'];       $age = ($currentorderdate / 86400);       $ageoforder = round($age, 0);       echo $ageoforder;       ?> day(s)</td>             <td align="center" valign="middle"><?php              if ($row_orders['paid'] == yes) {                 echo $row_orders['paid'];             } else {                 echo "no";             }             ?></td>             <td align="center" valign="middle"><?php              echo $row_orders['bonus']; ?>%</td>             <td align="center" valign="middle"><?php             $bonustotal = (($multiply_total/100) * ($row_orders['bonus']));         if ($percent > 0 && $ageoforder-1 < 90 ) {          echo "€"; echo number_format($bonustotal ,2);         } else {           echo "€"; echo 0;         }       ?></td>           </tr>           <?php } while ($row_orders = mysql_fetch_assoc($orders));?>       </table> <table border="0">   <tr>     <td><?php if ($pagenum_orders > 0) { // show if not first page ?>         <a href="<?php printf("%s?pagenum_orders=%d%s", $currentpage, 0, $querystring_orders); ?>">first</a>         <?php } // show if not first page ?></td>     <td><?php if ($pagenum_orders > 0) { // show if not first page ?>         <a href="<?php printf("%s?pagenum_orders=%d%s", $currentpage, max(0, $pagenum_orders - 1), $querystring_orders); ?>">previous</a>         <?php } // show if not first page ?></td>     <td><?php if ($pagenum_orders < $totalpages_orders) { // show if not last page ?>         <a href="<?php printf("%s?pagenum_orders=%d%s", $currentpage, min($totalpages_orders, $pagenum_orders + 1), $querystring_orders); ?>">next</a>         <?php } // show if not last page ?></td>     <td><?php if ($pagenum_orders < $totalpages_orders) { // show if not last page ?>         <a href="<?php printf("%s?pagenum_orders=%d%s", $currentpage, $totalpages_orders, $querystring_orders); ?>">last</a>         <?php } // show if not last page ?></td>   </tr> </table>     </div>   </div>  <div id ="bottompart">   <div id='bottom'></div> </div> </div> </div> </body> </html><?php mysql_free_result($orders);  mysql_free_result($staff);  mysql_free_result($products); ?> 

i think want show selected value of user in select list can use if condition echo selected

<?php    $dynamic_val = /* save value if call db, or using $_post */; ?> <select>    <option value="1" <?php echo sel_opt(1, $dynamic_val); ?>></option>    <option value="2" <?php echo sel_opt(2, $dynamic_val); ?>></option>    <option value="3" <?php echo sel_opt(3, $dynamic_val); ?>></option> </select>  <?php    function sel_opt($opt, $val) {      if($opt == $val) {        return 'selected';      }    } ?> 

this won't literally shift option top of list, instead select user selected option


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -