2013-04-18 22 views
0

我希望有人可以提供幫助。用PHP自動重新選擇下拉選項

我的代碼如下,我希望的是當用戶提交表單時,他們選擇的選項值保留在下拉列表的頂部。

任何幫助,將不勝感激。

<?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 AS ID, 
customers.`Name` AS customerName, 
products.ProductName AS product, 
orders.Quantity AS Quantity, 
orders.SalesPrice AS SalesPrice, 
orders.Price AS Price, 
orders.paid AS paid, 
orders.AgeOfPayment AS AgeOfPayment, 
orders.orderDate AS orderDate, 
products.Price AS productPrice, 
staff.StaffName AS staffMember, 
orders.bonus AS bonus 
FROM 
orders 
INNER JOIN staff AS staff ON orders.staffMember = staff.ID 
INNER JOIN products AS products ON orders.product = products.ID 
INNER JOIN customers AS customers ON orders.customerName = customers.ID 
WHERE 
1 = 1 "; 

//The above where makes the where true 

// The below takes the customer name from the select input and filters based on selection. Filters the order by 

if (isset($_GET['customerName2']) && $_GET['customerName2'] != 'FALSE') 
{ 
$query_orders .= "AND customerName = '" .$_GET['customerName2']."' "; 
} 

// This makes sure the product is not false and filters 

if (isset($_GET['product']) && $_GET['product'] != 'FALSE') 
{ 
$query_orders .= "AND product = '" .$_GET['product']."' "; 
} 

// The below filters hte Customer Name 

if (isset($_GET['customerName'])&& $_GET['customerName'] != 'FALSE') { 
$query_orders .= "ORDER BY '".$_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 as $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 FROM customers WHERE 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 * FROM 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 is where you will be able to view orders from a 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 to 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 the Customer Name</option> 
       <?php 
do { 
?> 
       <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 a Product</option> 
     <?php 
do { 
?> 
     <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 do { ?> 
      <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); 
?> 
+0

爲什麼你打開和關閉頁面頂部的這麼多'<?php'標籤?無論如何,你應該真的把你的代碼拆分到不同的層上,它現在的方式很難讀取/重用/維護。 – moonwave99

+0

它如何用於。在一分鐘學習,所以當我在這個即時消息得到更好的時候,病情好轉減少了很多 –

回答

0

我想你想顯示在選擇列表中的用戶的選擇的價值比你可以使用if條件呼應selected

<?php 
    $dynamic_val = /* Save the value if you call from db, or by 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'; 
    } 
    } 
?> 

這不是字面上轉移選項到列表頂部,而不是 它會選擇用戶選擇的選項

+0

完美,像魅力 –

+0

@RichieAnderson;)你可以使用該功能與任何選擇在您的網站 –

0

您可以在此添加此項

<option value="FALSE" <?php echo isSelected("FALSE"); ?>>Select how to Order By</option> 
<option value="ID" <?php echo isSelected("ID"); ?>>Order ID</option> 
//so on.... 

function isSelected($term) { 
    if(isset($_GET['customerName']) && $term == $_GET['customerName']) { 
     return 'selected="selected"'; 
    } 
}