All pastes #976627 Raw Edit

Untitled

public php v1 · immutable
·published 2008-04-08 10:06 UTC
rendered paste body
<?phpdefine('SELF', basename(__FILE__));if (isset($_GET['by']) && isset($_GET['sort'])) {		switch ($_GET['by']) {		case 'ProductName': $by = 'ProductName'; break;		case 'CategoryName': $by = 'CategoryName'; break;		case 'UnitPrice': $by = 'UnitPrice'; break;		case 'UnitsInStock': $by = 'UnitsInStock'; break;		default: $by = 'ProductID';	}		if ($_GET['sort'] == 'desc') {		$sort = ' desc';		$sortURL = 'asc';	} else {		$sort = ' asc';		$sortURL = 'desc';	}} else {	$by = 'ProductID';	$sort = ' asc';	$sortURL = 'desc';}mssql_connect('.', 'student', 'student') or die('Polaczenie nie powiodlo sie');mssql_select_db('Northwind');$result = mssql_query('select CategoryID, CategoryName	from Categories	order by CategoryID');$categoriesArray = array();while ($category = mssql_fetch_assoc($result)) {	$categoriesArray[$category['CategoryID']] = $category['CategoryName'];}echo '<?xml version="1.0" encoding="iso-8859-2"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl"><head>	<title>Test</title>	<meta http-equiv="content-type" content="text/html; charset=iso-8859-2" /></head><body>	<form method="get" action="' . SELF . '">	<p>		Sortuj wg.				<select name="by" size="1">			<option value="ProductID">ID</option>			<option value="ProductName">Nazwa</option>			<option value="CategoryName">Kategoria</option>			<option value="UnitPrice">Cena</option>			<option value="UnitsInStock">Magazyn</option>		</select>				<select name="sort" size="1">			<option value="asc">&#9650; Rosnaco</option>			<option value="desc">&#9660; Malejaco</option>		</select>				<select name="category" size="1">			<option value="0">-- Wszystkie kategorie --</option>';foreach ($categoriesArray as $CategoryID => $CategoryName) {	$selected = (isset($_GET['category']) && $_GET['category'] == $CategoryID) ? ' selected="selected"' : null;	echo '			<option value="' . $CategoryID . '"' . $selected . '>' . $CategoryName . '</option>';}if (isset($_GET['category']) && array_key_exists($_GET['category'], $categoriesArray)) {	$whereCategory = 'where p.CategoryID = ' . $_GET['category'];	$category = $_GET['category'];} else {	$whereCategory = null;	$category = 0;}echo '		</select>				<input type="submit" />	</p>	</form>';if (isset($_GET['edit']) && (int) $_GET['edit'] > 0) {	$result = mssql_query('select ProductID, ProductName, CategoryID, UnitPrice, UnitsInStock		from Products		where ProductID = ' . $_GET['edit']);	$editData = mssql_fetch_assoc($result);		$editProductID = $editData['ProductID'];	$editProductName = $editData['ProductName'];	$editUnitPrice = $editData['UnitPrice'];	$editUnitsInStock = $editData['UnitsInStock'];	$editCategoryID = $editData['CategoryID'];} else {	$editProductID = 0;	$editProductName = null;	$editUnitPrice = null;	$editUnitsInStock = null;	$editCategoryID = null;}if (isset($_POST['ProductID']) && (int) $_POST['ProductID'] > 0) {	$result = mssql_query('update Products		set ProductName = "' . $_POST['ProductName'] . '", CategoryID = ' . $_POST['CategoryID'] . ', UnitPrice = ' . $_POST['UnitPrice'] . ', UnitsInStock = ' . $_POST['UnitsInStock'] . '		where ProductID = ' . $_POST['ProductID']);}echo '	<form method="post" action="' . SELF . '">		<p style="background-color: #ccc; width: 540px; padding: 5px;">		<input type="hidden" name="ProductID" value="' . $editProductID . '" />				Nazwa:				<input type="text" name="ProductName" value="' . $editProductName . '" /><br />				Cena:				<input type="text" name="UnitPrice" value="' . $editUnitPrice . '" /><br />				Na magazynie:				<input type="text" name="UnitsInStock" value="' . $editUnitsInStock . '" /><br />				Kategoria:				<select name="CategoryID" size="1">			<option value="0">-- Wszystkie kategorie --</option>';foreach ($categoriesArray as $CategoryID => $CategoryName) {	$selected = (isset($editCategoryID) && $editCategoryID == $CategoryID) ? ' selected="selected"' : null;	echo '			<option value="' . $CategoryID . '"' . $selected . '>' . $CategoryName . '</option>';}echo '		</select><br />				<input type="submit" />	</p>	</form>		<table border="1">		<tr><th><a href="' . SELF . '?by=ProductID&amp;sort=' . $sortURL . '&amp;category=' . $category . '">ID</a></th><th><a href="' . SELF . '?by=ProductName&amp;sort=' . $sortURL . '&amp;category=' . $category . '">Nazwa</a></th><th><a href="' . SELF . '?by=CategoryName&amp;sort=' . $sortURL . '&amp;category=' . $category . '">Kategoria</a></th><th><a href="' . SELF . '?by=UnitPrice&amp;sort=' . $sortURL . '&amp;category=' . $category . '">Cena</a></th><th><a href="' . SELF . '?by=UnitsInStock&amp;sort=' . $sortURL . '&amp;category=' . $category . '">Na magazynie</a></th><th>Edycja</th></tr>';$result = mssql_query('select ProductID, ProductName, CategoryName, UnitPrice, UnitsInStock	from Products p		join Categories c on p.CategoryID = c.CategoryID	' . $whereCategory . '	order by ' . $by . $sort);while ($product = mssql_fetch_assoc($result)) {	$hilight = !$product['UnitsInStock'] ? ' style="color: #fff; background: #f00;"' : null;	echo '		<tr' . $hilight . '><td>' . $product['ProductID'] . '</td><td>' . $product['ProductName'] . '</td><td>' . $product['CategoryName'] . '</td><td>' . $product['UnitPrice'] . '</td><td>' . $product['UnitsInStock'] . '</td><td><a href="' . SELF . '?edit=' . $product['ProductID'] . '&amp;by=' . $by . '&amp;sort=' . ltrim($sort) . '&amp;category=' . $category . '">Edycja</a></td></tr>';}echo '	</table></body></html>';?>