Jump to content

cyprus

Members
  • Posts

    68
  • Joined

  • Last visited

    Never

Everything posted by cyprus

  1. Thanks Barand, hope this makes it a bit clearer. The select query is user selected in what columns data is required. Its done before it reaches the code thats giving me grief. 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"> [color=blue]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)[/color] <?     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')";     } -------------------------------------------------- [color=blue]THIS WAS PUT HERE TO CHECK THE QUERY SELECT STATEMENT LOOKED CORRECT.[/color]     //$sql = "SELECT $fields FROM ORDERS $where";     //echo "<p>$sql</p>"; } -------------------------------------------------- [color=blue]THIS IS JUST FOR THE START OF OPENING THE DB CONNECTION [/color] $username="root"; // $password="password"; $database=""; mysql_connect(localhost,$username); -------------------------------------------------- [color=blue]THIS PART IS TO BYPASS OPENING THE DATABASE WITH THE QUERY WHEN THE PAGE IS FIRST OPENED, WITH NO BUILT UP SELECT QUERY[/color] if($fields == "" || $where == "") { // Show the page when nothing is defined for $fields and $where } else { -------------------------------------------------- [color=blue]RUN THE QUERY WITH DB[/color] $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); } -------------------------------------------------- ?> [color=blue]THIS PART PUTS CHECKBOXES ON HTML PAGE, PLUS TEXT, AND REINSTATES THE STATUS OF THE CHECKBOXES (IE KEEPS THEM CHECKED OR UNCHECKED AFTER SUBMITTING) [/color] <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>";             } -------------------------------------------------- [color=blue]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)[/color] if($fields == "" || $where == "") { // nothing defined for $fields and $where } else { echo '</select name>';   for ($i=0; $i<=$numrows-1; $i++):   [color=red]$_rows[] = array("Orderdate"=>mysql_result($result,$i,"Orderdate"), "Ordernumber"=>mysql_result($result,$i,"Ordernumber"), "Value"=>$i);[/color]   endfor;   foreach ($_rows as $row): [color=red]  if (strlen($row['Ordernumber']) > $max['Ordernumber']) {$max['Ordernumber'] = strlen($row['Ordernumber']);}[/color]   [color=red]  //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']);}[/color]  $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']}\">"; [color=red]$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']);[/color] [color=blue]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[/color]. $contents .= "</option>\r\n"; endforeach; $contents .= "</select>"; $contents = str_replace("#", "&nbsp;", $contents); ?> <p>&nbsp;</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.
  2. Thought it might help to post the code being used for the listbox. As you can see I have had to rem out parts of it to run without error when certain column information was not passed to it. echo '</select name>';   for ($i=0; $i<=$numrows-1; $i++):   //print $numrows;   $_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']); $contents .= "</option>\r\n"; endforeach; $contents .= "</select>"; $contents = str_replace("#", "&nbsp;", $contents); ?>
  3. Many thanks. I think its best for me to explain a bit more. I have a page/form which contains checkboxes allowing a user to select columns from a table which will find there way into a dropdown list. So the select query will not contain columns that are not wanted. However, when it gets to the stage of filling the list, which is designed/coded to give column spacing, the arrays error if some table column data does not get to them. What I am trying to do is modify my dropdown list code to be dynamically built before the data flows to it. Hope I am making sense. One of the lines of code in the listbox creation is: $_rows[] = array("Orderdate"=>mysql_result($result,$i,"Orderdate"), "Ordernumber"=>mysql_result($result,$i,"Ordernumber"), "Value"=>$i); This so far is hard coded, but if a user does not select the Ordernumber column to be in the list, the above codeline is unhappy. Hope this makes it easier to understand where my problems are. Thanks again
  4. Thanks, however how can I break up the line below as I have to include or exclude column names. Thanks $_rows[] = array("Orderdate"=>mysql_result($result,$i,"Orderdate"), "Ordernumber"=>mysql_result($result,$i,"Ordernumber"), "Value"=>$i);
  5. The code below I think builds an array of rows. It goes on to be used in a dropdown list. The data of Orderdate comes from a select query to a table. However the table column Orderdate is one of many other columns in the table. It, like other columns gets selected by the user as to what he wants to appear in the list. 2 questions - (1) how do I programatically add in other column names into the line of code below. (2) How do I omit the column Orderdate if its not one of the columns selected by the user? $_rows[] = array("Orderdate"=>mysql_result($result,$i,"Orderdate"), "Value"=>$i); Many thanks
  6. Thanks for the comments on @, I just picked up my colleages line of code. Should I remove @, as I don't want to suppress seing any errors. Thanks
  7. How can I bypass the code below if my $fields and $where have not been executed yet? The first time the page appears its empty. Thanks $username="root"; $password=""; $database=""; mysql_connect(localhost,$username); $query = "SELECT $fields FROM ORDERS $where"; @mysql_select_db($database) or die( "Unable to select database");      $result = mysql_query($query) or die('Problem with query: ' . $query . '<br>'. mysql_error()); $numrows=mysql_numrows($result);
  8. Absolutely perfect. I have just modified the other checkboxes the same way and all is fine. So pleased I joined this forum, with people around like you out there. Thanks again
  9. Thanks a million, absolutely perfect. I will modify the other checkboxes in the same way. So glad I moved over to this site with people like you willing to help. The last forum was filled with "Look in the manual", very greatful for all your help. Thanks
  10. Many thanks, I tried to put it in my existing code, but had no luck. My current code is: <? session_start(); ?> <?     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 GGPost IN ('$prodlist')";     }     $sql = "SELECT $fields FROM ORDERS $where";     echo "<p>$sql</p>"; } ?> <FORM method='post'> Included Data<br> <input type="checkbox" name="showfield[]" value="OrderNumber">Order Number<br> <input type="checkbox" name="showfield[]" value="Orderdate">Order Date<br> <br> Select Product<br> <input type="checkbox" name="Product[]" value="1">Digital Betacam<br> <input type="checkbox" name="product[]" value="2">Betacam SP<br> <input type="checkbox" name="product[]" value="3">DVCPro<br> <input type="checkbox" name="product[]" value="4">HDCAM<br> <input type="checkbox" name="product[]" value="5">Mini DV<br> <input type="submit" name="submit" value="Submit"> </FORM> Would be very appreciative if you could advise where to put the code to retain checkbox positions after submit. Many thanks
  11. Thanks. I have a page with checkboxes on, also a dropdown list. The list fills itself from a database query based on the selections of the checkboxes. In order for the list to get filled/updated, the form has to be submitted and returned. However after submission the checkboxes are cleared. Hope that explains it better.
  12. Many thanks, you are correct. I put it back and it works. Is there a way to hold checkbox values that are selected after a sumbission without using a table? Also is there a points award on this site for showing gratitude of help. Many thanks again
  13. I have been playing around with Barand's code, not that I have ignored the others, in fact Destructions code covers my question, but have not had the chance to try it. However I am trying to modify the WHERE statement below. if (count($_POST['product'])) {         $prodlist = join("','", $_POST['product']);         $where = "WHERE GGPost = ('$prodlist')";     }     $sql = "SELECT $fields FROM ORDERS $where"; I am trying to get it to build - Where GGPost=1 or GGPost=2 etc. Any idea how to modify it. Many thanks again to al
  14. Many Many thanks for all the help folks, I'm astounded by the effort you all make in this forum. I will try all the options out and hopefully get there.
  15. Thanks, no I did not go down that road. I am posting the values through and using session variables. I got as far as: $query = "SELECT Orderdate, " .$op1. " FROM Orders"; There is an $op1,$op2,$op3,$op4,$op5,$op6 which carry the names of six columns to get displayed. There is an $T1,$T2,$T3,$T4,$T5,$T6 which each carry a value of 1 if checked being for the WHERE statement: WHERE  $T1=1 or $T2=1 etc. I have been tying myself in knots, especially how to include the "OR" when there maybe only one box selected or its the last of the selections. Its been a real minefield. Thanks
  16. I have been struggling for ages on this. I have a page with 12 checkboxes, which get posted to a page with a dropdown list on. 6 checkboxes choose which table columns the user wants to see in the list. 6 checkboxes select the products he wants to see in the list, products are categorised in 6 groups, so he might choose 1,3,5. How do I get from the checkboxes to building a Select from database line of code? eg Select a,b,c from database where x=1 or x=2 etc. Would be very appreciated if someone could help, my knowledge is small.Thanks
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.