Forum Moderators: coopster
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Search <input type="text" name="search"><br>
<select size="1" name="dropdown">
<option value="" selected>Search By...</option>
<option value="Manufacturer">Manufacturer</option>
<option value="Model">Model</option>
<option value="Socket">Socket</option>
<option value="Hyperthread">Hyperthread</option>
<option value="Cores">Cores</option>
</select>
<input type="Submit" value="Submit" name="Submit">
</form>
<?php
}
else {
// form submitted
// set server access variables
$host = "#*$!X";
$user = "#*$!X";
$pass = "#*$!X";
$db = "#*$!X";
$search = empty($_POST['search'])? die ("ERROR: Enter Search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from Dropdown") : mysql_escape_string($_POST['dropdown']);
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
//Create Query
$query = "SELECT * FROM CPU WHERE $dropdown='$search'" or die (mysql_error());
$result = mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);
mysql_close($connect);
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$Manufacturer=mysql_result($result,$i,"Manufacturer");
$Model=mysql_result($result,$i,"Model");
$Socket=mysql_result($result,$i,"Socket");
$Hyperthread=mysql_result($result,$i,"Hyperthread");
$Cores=mysql_result($result,$i,"Cores");
echo "<br><b>$Manufacturer</b></br>$Model</b></br>Socket: $Socket</br>Hyperthread: $Hyperthread</br>Cores: $Cores<br>";
$i++;
}
}
?>
</body>
</html> $sql = 'SELECT DISTINCT cpu FROM cpu_table ORDER BY cpu ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
$sel = '<select name="cpu">
<option value=""></option>';
for($i = 0; $i < $rows; $i++) {
$cpu = mysql_result($result, $i, "cpu");
$sel .= "\n" . '<option value="' . $cpu . '">' . ucfirst($cpu) . '</option>';
}
$sel .= "\n" . '</select>';
echo $sel;
function select_menu($cell, $table) {
$sql = 'SELECT DISTINCT `' . $cell . '` FROM `' . $table . '` ORDER BY `' . $cell . '` ASC';
$result = mysql_query($sql);
if($rows = mysql_num_rows($result)) {
$sel = '<select name="' . $cell . '">
<option value=""></option>';
for($i = 0; $i < $rows; $i++) {
$cpu = mysql_result($result, $i, $cell);
$sel .= "\n" . '<option value="' . $cpu . '">' . ucfirst($cpu) . '</option>';
}
$sel .= "\n" . '</select>';
} else {
$sel = ' ';
}
return $sel;
}
echo select_menu('cpu', 'cpu_table');
<?php
<?php
function select_menu($cell, $table) {
echo select_menu('COLUMN HEADER', 'TABLE NAME');
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
function select_menu($cell, $table) {
$sql = 'SELECT DISTINCT `' . $cell . '` FROM `' . $table . '` ORDER BY `' . $cell . '` ASC';
$result = mysql_query($sql);
if($rows = mysql_num_rows($result)) {
$sel = '<select name="' . $cell . '">
<option value=""></option>';
for($i = 0; $i < $rows; $i++) {
$cpu = mysql_result($result, $i, $cell);
$sel .= "\n" . '<option value="' . $cpu . '">' . ucfirst($cpu) . '</option>';
}
$sel .= "\n" . '</select>';
} else {
$sel = ' ';
}
return $sel;
}
// form submitted
// set server access variables
$host = "----";
$user = "====";
$pass = "----";
$db = "-----";
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
//Create Query
echo select_menu('CPU', 'CPU');
$i++;
?>
</body>
</html>
mysql_select_db($db) or die ("Unable to connect to database");
//Create Query
?>
<form method="post" action="/somefile.php">
<?php
echo select_menu('CPU', 'CPU');
?>
<input type="submit" name="subbut" value="Submit" />
</form>
<?php
$i++;
?>
Though I'm not sure what that $i++; is supposed to be doing :/
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
function select_menu($cell, $table) {
$sql = 'SELECT DISTINCT `' . $cell . '` FROM `' . $table . '` ORDER BY `' . $cell . '` ASC';
$result = mysql_query($sql);
if($rows = mysql_num_rows($result)) {
$sel = '<select name="' . $cell . '">
<option value=""></option>';
for($i = 0; $i < $rows; $i++) {
$cpu = mysql_result($result, $i, $cell);
$sel .= "\n" . '<option value="' . $cpu . '">' . ucfirst($cpu) . '</option>';
}
$sel .= "\n" . '</select>';
} else {
$sel = ' ';
}
return $sel;
}
// set server access variables
$host = "-";
$user = "-";
$pass = "-";
$db = "-";
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
// form submitted
?>
<form method="post" action="/search.php">
<?php
echo select_menu('Cores', 'CPU');
?>
<input type="Submit" name="Submit" value="Submit" />
</form>
<?php
?>
</body>
</html>
Tried to execute a search and no result, the page refreshes back to pre- select condition.
echo select_menu('Cores', 'CPU');
$_POST['Cores']
echo select_menu('Cores', 'CPU') . select_menu('Processors', 'CPU');
<input type="text" name="hello">
echo $_POST['hello'];
$sql = 'SELECT `Cores`, `Processors` FROM `CPU` WHERE 1';
if(isset($_POST['Cores']) && $_POST['Cores'] != "") {
$sql .= ' AND `Cores` = "' . mysql_real_escape_string(strip_tags($_POST['Cores'])) . '"';
}
$sql .= ' ORDER BY `Processors` ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
echo '<table cellspacing="2">
<tr>
<td>#</td>
<td><b>Processor</b></td>
<td><b>Cores</b></td>
</tr>';
for($i = 0; $i < $rows; $i++) {
$rc = ($i%2)? 'ffff' : 'cccc';
echo "\n" . '<tr style="background-color: #' . $rc . 'ff;">
<td>' . ($i + 1) . '</td>
<td>' . mysql_result($result, $i, "Processors") . '</td>
<td>' . mysql_result($result, $i, "Cores") . '</td>
</tr>';
}
echo "\n" . '</table>';
We're building the base SQL query here. Note that "WHERE 1" is kind of like saying "Where there is anything to select" - In lay man's terms, it doesn't do anything aside from get the WHERE clause up there (so we can add additional WHERE clauses with ease).$sql = 'SELECT `Cores`, `Processors` FROM `CPU` WHERE 1';
If a value has been provided for $_POST['Cores'] this will add an additional WHERE clause to the existing SQL query. Note that there is a space before the "AND".if(isset($_POST['Cores']) && $_POST['Cores'] != "") {
$sql .= ' AND `Cores` = "' . mysql_real_escape_string(strip_tags($_POST['Cores'])) . '"';
}
The functions being applied here are to protect you from users with malicious intent.mysql_real_escape_string(strip_tags($_POST['Cores']))
Here we run the query, and find out how many rows of data are returned.$result = mysql_query($sql);
$rows = mysql_num_rows($result);
This line creates a variable $i, and sets it as 0. Everything contained within the curly braces will loop over and over, and each time $i will increment by 1. This continues until $i is equal to $rows (will not run when $i is exactly equal to $rows)for($i = 0; $i < $rows; $i++) {
This pulls the information contained within a specific cell. In this case, whatever row number $i is equal to, under the column "Cores".mysql_result($result, $i, "Cores")
This line is just there to alternate the table row colour. If $i is a multiple of 2 will determine if $rc is equal to 'ffff' or 'cccc'.$rc = ($i %2)? 'ffff' : 'cccc';
if($i %2) {
$rc = 'ffff';
} else {
$rc = 'cccc';
}
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// set server access variables
$host = "#*$!x";
$user = "#*$!x";
$pass = "#*$!x";
$db = "#*$!x";
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
$sql = 'SELECT `Cores`, `Model` FROM `CPU` WHERE 1';
if(isset($_POST['Cores']) && $_POST['Cores'] != "") {
$sql .= ' AND `Cores` = "' . mysql_real_escape_string(strip_tags($_POST['Cores'])) . '"';
}
$sql .= ' ORDER BY `Model` ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
echo '<table cellspacing="2">
<tr>
<td>#</td>
<td><b>Processor</b></td>
<td><b>Cores</b></td>
</tr>';
for($i = 0; $i < $rows; $i++) {
$rc = ($i%2)? 'ffff' : 'cccc';
echo "\n" . '<tr style="background-color: #' . $rc . 'ff;">
<td>' . ($i + 1) . '</td>
<td>' . mysql_result($result, $i, "Model") . '</td>
<td>' . mysql_result($result, $i, "Cores") . '</td>
</tr>';
}
echo "\n" . '</table>';
?>
</form>
</body>
</html>
// form submitted
?>
<form method="post" action="return.php">
<?php
echo select_menu('Manufacturer', 'CPU') . select_menu('Cores', 'CPU') . select_menu('Socket', 'CPU') . select_menu('Average Price (USD)', 'CPU');
?>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>
I'm not sure how to get the multiple fields to work in a query for return.php however.
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
$sql = 'SELECT `Cores`, `Model` FROM `CPU` WHERE 1';
if(isset($_POST['Manufacturer']) && $_POST['Manufacturer'] != "") {
$sql .= ' AND `Manufacturer` = "' . mysql_real_escape_string(strip_tags($_POST['Manufacturer'])) . '"';
}
if(isset($_POST['Cores']) && $_POST['Cores'] != "") {
$sql .= ' AND `Cores` = "' . mysql_real_escape_string(strip_tags($_POST['Cores'])) . '"';
}
$sql .= ' ORDER BY `Model` ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
echo '<table cellspacing="2">
<tr>
<td>#</td>
<td><b>Manufacturer</b></td>
<td><b>Processor</b></td>
<td><b>Cores</b></td>
</tr>';
for($i = 0; $i < $rows; $i++) {
$rc = ($i%2)? 'ffff' : 'cccc';
echo "\n" . '<tr style="background-color: #' . $rc . 'ff;">
<td>' . ($i + 1) . '</td>
<td>' . mysql_result($result, $i, "Manufacturer") . '</td>
<td>' . mysql_result($result, $i, "Model") . '</td>
<td>' . mysql_result($result, $i, "Cores") . '</td>
</tr>';
}
echo "\n" . '</table>';
?>
</form>
</body>
</html>
$sql = 'SELECT `Cores`, `Model` FROM `CPU` WHERE 1';
$sql = 'SELECT `Cores`, `Model`, `Manufacturer` FROM `CPU` WHERE 1';
SELECT `a` FROM `b` WHERE `c` = "d"
// form submitted
?>
<form method="post" action="return.php">
<?php
echo select_menu('Manufacturer', 'CPU') . select_menu('Cores', 'CPU') . select_menu('Socket', 'CPU') . select_menu('64 Bit CPU', 'CPU') . select_menu('Average Price (USD)', 'CPU');
?>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
$sql = 'SELECT `Cores`, `Model`, `64 Bit CPU`, `Manufacturer`, `Average Price (USD)` FROM `CPU` WHERE 1';
if(isset($_POST['Manufacturer']) && $_POST['Manufacturer'] != "") {
$sql .= ' AND `Manufacturer` = "' . mysql_real_escape_string(strip_tags($_POST['Manufacturer'])) . '"';
}
if(isset($_POST['Cores']) && $_POST['Cores'] != "") {
$sql .= ' AND `Cores` = "' . mysql_real_escape_string(strip_tags($_POST['Cores'])) . '"';
}
if(isset($_POST['64 Bit CPU']) && $_POST['64 Bit CPU'] != "") {
$sql .= ' AND `64 Bit CPU` = "' . mysql_real_escape_string(strip_tags($_POST['64 Bit CPU'])) . '"';
}
if(isset($_POST['Average Price (USD)']) && $_POST['Average Price (USD)'] != "") {
$sql .= ' AND `Average Price (USD)` = "' . mysql_real_escape_string(strip_tags($_POST['Average Price (USD)'])) . '"';
}
$sql .= ' ORDER BY `Model` ASC';
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
function select_menu($cell, $table) {
$sql = 'SELECT DISTINCT `' . $cell . '` FROM `' . $table . '` ORDER BY `' . $cell . '` ASC';
$result = mysql_query($sql);
if($rows = mysql_num_rows($result)) {
$sel = '<select name="' . $cell . '">
<option value=""></option>';
for($i = 0; $i < $rows; $i++) {
$cpu = mysql_result($result, $i, $cell);
$sel .= "\n" . '<option value="' . $cpu . '">' . ucfirst($cpu) . '</option>';
}
$sel .= "\n" . '</select>';
} else {
$sel = ' ';
}
return $sel;
}
// set server access variables
$host = "zz";
$user = "zz";
$pass = "zz";
$db = "zz";
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
// form submitted
?>
<form method="post" action="return.php">
<?php
echo select_menu('Manufacturer', 'CPU') . select_menu('Cores', 'CPU') . select_menu('Socket', 'CPU') . select_menu('x64', 'CPU') . select_menu('Price', 'CPU');
?>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// set server access variables
$host = "zz";
$user = "zz";
$pass = "zz";
$db = "zz";
// Open Connection
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
//Select Database
mysql_select_db($db) or die ("Unable to connect to database");
$sql = 'SELECT `Cores`, `Model`, `x64`, `Manufacturer`, `Price` FROM `CPU` WHERE 1';
if(isset($_POST['Manufacturer']) && $_POST['Manufacturer'] != "") {
$sql .= ' AND `Manufacturer` = "' . mysql_real_escape_string(strip_tags($_POST['Manufacturer'])) . '"';
}
if(isset($_POST['Cores']) && $_POST['Cores'] != "") {
$sql .= ' AND `Cores` = "' . mysql_real_escape_string(strip_tags($_POST['Cores'])) . '"';
}
if(isset($_POST['x64']) && $_POST['x64'] != "") {
$sql .= ' AND `x64` = "' . mysql_real_escape_string(strip_tags($_POST['x64'])) . '"';
}
if(isset($_POST['Price']) && $_POST['Price'] != "") {
$sql .= ' AND `Price` = "' . mysql_real_escape_string(strip_tags($_POST['Price'])) . '"';
}
$sql .= ' ORDER BY `Model` ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
echo '<table cellspacing="2">
<tr>
<td>#</td>
<td><b>Manufacturer</b></td>
<td><b>Processor</b></td>
<td><b>Cores</b></td>
<td><b>64 Bit CPU</b></td>
<td><b>Average Price (USD)</b></td>
</tr>';
for($i = 0; $i < $rows; $i++) {
$rc = ($i%2)? 'ffff' : 'cccc';
echo "\n" . '<tr style="background-color: #' . $rc . 'ff;">
<td>' . ($i + 1) . '</td>
<td>' . mysql_result($result, $i, "Manufacturer") . '</td>
<td>' . mysql_result($result, $i, "Model") . '</td>
<td>' . mysql_result($result, $i, "Cores") . '</td>
<td>' . mysql_result($result, $i, "x64") . '</td>
<td>' . mysql_result($result, $i, "Price") . '</td>
</tr>';
}
echo "\n" . '</table>';
?>
</form>
</body>
</html>