Sorry Sleipnir, a lot going on over here, misread into you last posting.
The form/page is very basic at present, just getting the basics together. It.s a form that submits back to itself only to display a dropdown list of users selection of what table columns are displayed, and what product groups of 6 are held in the table. Its basically to display past customer orders. One day I will learn how to use the code display methods of this forum.
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Date</title>
</head>
View Archive Orders
<body bgcolor="#C0C0C0">
THIS BIT LOOKS AT WHAT GETS POSTED BACK AFTER SUBMISSION FROM THE USER CHECKBOXES BEING CHECKED. FROM THESE VALUES THE SELECT STATEMENT IS CONSTRUCTED (IE THE SELECT XXXXX AND THE WHERE XXXX)
<?
if (isset($_POST['submit'])) {
//$fields = 'op1'; // default
$where = '';
if (count($_POST['showfield']))
$fields = join(',', $_POST['showfield']);
if (count($_POST['product'])) {
$prodlist = join("','", $_POST['product']);
$where = "WHERE GGroup IN ('$prodlist')";
}
--------------------------------------------------
THIS WAS PUT HERE TO CHECK THE QUERY SELECT STATEMENT LOOKED CORRECT.
//$sql = "SELECT $fields FROM ORDERS $where";
//echo "<p>$sql</p>";
}
--------------------------------------------------
THIS IS JUST FOR THE START OF OPENING THE DB CONNECTION
$username="root";
// $password="password";
$database="";
mysql_connect(localhost,$username);
--------------------------------------------------
THIS PART IS TO BYPASS OPENING THE DATABASE WITH THE QUERY WHEN THE PAGE IS FIRST OPENED, WITH NO BUILT UP SELECT QUERY
if($fields == "" || $where == "")
{
// Show the page when nothing is defined for $fields and $where
}
else
{
--------------------------------------------------
RUN THE QUERY WITH DB
$query = "SELECT $fields FROM ORDERS $where";
echo "<p>$query</p>";
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query($query) or die('Problem with query: ' . $query . '
'. mysql_error());
$numrows=mysql_numrows($result);
}
--------------------------------------------------
?>
THIS PART PUTS CHECKBOXES ON HTML PAGE, PLUS TEXT, AND REINSTATES THE STATUS OF THE CHECKBOXES (IE KEEPS THEM CHECKED OR UNCHECKED AFTER SUBMITTING)
<FORM method='post'>
Include Data<br>
<?
$showfields = array (
Orderdate => 'Order Date',
Ordernumber => 'Order Number',
Description => 'Caption',
Duration => 'Duration',
);
foreach ($showfields as $id =>$prod) {
if ($_POST['showfield']) {
// was value of id in those posted?
$chk = in_array($id, $_POST['showfield']) ? 'checked' : '';
}
else $chk = '';
echo "<input type='checkbox' name='showfield[]' value='$id' $chk>$prod<br>";
}
?>
<br>
Include Products<br>
<?
$products = array (
1 => 'Digital Betacam',
2 => 'Betacam SP',
3 => 'DVCPro',
4 => 'HDCAM',
5 => 'Mini DV'
);
foreach ($products as $id =>$prod) {
if ($_POST['product']) {
// was value of id in those posted?
$chk = in_array($id, $_POST['product']) ? 'checked' : '';
}
else $chk = '';
echo "<input type='checkbox' name='product[]' value='$id' $chk>$prod<br>";
}
--------------------------------------------------
THIS PART CREATES THE DROPDOWN LIST. EARLY PART OF THE CODE PREVENTS LIST BEING DONE IF THERE IS AN INCOMPLETE SELECT QUERY, DATABASE HAS BEEN BYPASSED, NO DATA YET FOR LIST (IE PAGE FIRST LOAD)
if($fields == "" || $where == "")
{
// Show the page when nothing is defined for $fields and $where
}
else
{
echo '</select name>';
for ($i=0; $i<=$numrows-1; $i++):
$_rows[] = array("Orderdate"=>mysql_result($result,$i,"Orderdate"), "Ordernumber"=>mysql_result($result,$i,"Ordernumber"), "Value"=>$i);
endfor;
foreach ($_rows as $row):
if (strlen($row['Ordernumber']) > $max['Ordernumber']) {$max['Ordernumber'] = strlen($row['Ordernumber']);}
//if (strlen($row['Orderdate']) > $max['Orderdate']) {$max['Orderdate'] = strlen($row['Orderdate']);}
//if (strlen($row['Caption']) > $max['Caption']) {$max['Caption'] = strlen($row['Caption']);}
//if (strlen($row['Description']) > $max['Description']) {$max['Description'] = strlen($row['Description']);}
$rows[] = $row;
endforeach;
$contents = "<select style=\"font-family:monospace;\" name=\"selectbox\" multiple size=\"$numrows\">\r\n";
foreach ($rows as $row):
$contents .= "<option value=\"{$row['Value']}\">";
$contents .= sprintf("%-'#".($max['Ordernumber'] + 2)."s", $row['Ordernumber']);
//$contents .= sprintf("%-'#". ($max['Orderdate'] + 2)."s", $row['Orderdate']);
//$contents .= sprintf("%-'#". ($max['Caption'] + 4)."s", $row['Caption']);
//$contents .= sprintf("%-'#". ($max['Description'] + 2)."s", $row['Description']);
THE STATEMENTS/CODE ABOVE ALL WORKS IF ORDERNUMBER/ORDERDATE/CAPTION/DESCRIPTION WERE SELECTED BY THE USER. HOWEVER IF THEY WERE NOT, THEN UNDERSTANDABLY THE CODE ERRORS.
$contents .= "</option>\r\n";
endforeach;
$contents .= "</select>";
$contents = str_replace("#", " ", $contents);
?>
<p> </p>
<?
echo "<div>Something Here</br>$contents</div>";
}
?>
<input type="submit" name="submit" value="Submit">
</FORM>
I don't know how ligible this will all be until I hit the button, only hope it makes it a bit clearer, thanks.