All pastes #992076 Raw Edit

Something

public text v1 · immutable
#992076 ·published 2008-04-21 16:24 UTC
rendered paste body
<?php

define('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');
kródko 
$categoriesArray = array();

while ($category = mssql_fetch_assoc($result)) {
	$categoriesArray[$category['CategoryID']] = $category['CategoryName'];
}

echo '<?xml version="1.0" encoding="utf-8"?>
<!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=utf-8" />
</head>
<body>

	<form method="get" action="' . SELF . '">
		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' : 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" />
	</form>

	<table border="1">
		<tr><th><a href="' . SELF . '?by=ProductID&sort=' . $sortURL . '&category=' . $category . '">ID</a></th><th><a href="' . SELF . '?by=ProductName&sort=' . $sortURL . '&category=' . $category . '">Nazwa</a></th><th><a href="' . SELF . '?by=CategoryName&sort=' . $sortURL . '&category=' . $category . '">Kategoria</a></th><th><a href="' . SELF . '?by=UnitPrice&sort=' . $sortURL . '&category=' . $category . '">Cena</a></th><th><a href="' . SELF . '?by=UnitsInStock&sort=' . $sortURL . '&category=' . $category . '">Na magazynie</a></th><th><b>Edycja</b></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="zmien.php?id=' . $product['ProductID'] . '"> Edytuj</a></td></tr>';
}

echo '	</table>

</body>
</html>';

?>