473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dropdown Menu Item to GET All in a database?

Hi all!
I am a newbie and unfamiliar with PHP and MySQL... I am trying to create a dropdown menu that defaults to all items in my database while still allowing one to also filter to certain products. Right now, I have a database named Products and am trying to base my dropdown off of the three different Product_Type items (Chips, Dips & Marinades, Rubs). Below is my code that I have so far which gets the individual Product_Type items, but I cannot get the dropdown to get ALL items in my db. Can someone help me out?

Expand|Select|Wrap|Line Numbers
  1. <code>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. $currentPage = $_SERVER["PHP_SELF"];
  35.  
  36. $maxRows_Test = 10;
  37. $pageNum_Test = 0;
  38. if (isset($_GET['pageNum_Test'])) {
  39.   $pageNum_Test = $_GET['pageNum_Test'];
  40. }
  41. $startRow_Test = $pageNum_Test * $maxRows_Test;
  42.  
  43. mysql_select_db($database_WPhoto, $WPhoto);
  44. $query_Test = "SELECT * FROM Products WHERE Product_Type='$Product_Type' ORDER BY Product_Title ASC";
  45. $query_limit_Test = sprintf("%s LIMIT %d, %d", $query_Test, $startRow_Test, $maxRows_Test);
  46. $Test = mysql_query($query_limit_Test, $WPhoto) or die(mysql_error());
  47. $row_Test = mysql_fetch_assoc($Test);
  48.  
  49. if (isset($_GET['totalRows_Test'])) {
  50.   $totalRows_Test = $_GET['totalRows_Test'];
  51. } else {
  52.   $all_Test = mysql_query($query_Test);
  53.   $totalRows_Test = mysql_num_rows($all_Test);
  54. }
  55. $totalPages_Test = ceil($totalRows_Test/$maxRows_Test)-1;
  56.  
  57. $queryString_Test = "";
  58. if (!empty($_SERVER['QUERY_STRING'])) {
  59.   $params = explode("&", $_SERVER['QUERY_STRING']);
  60.   $newParams = array();
  61.   foreach ($params as $param) {
  62.     if (stristr($param, "pageNum_Test") == false && 
  63.         stristr($param, "totalRows_Test") == false) {
  64.       array_push($newParams, $param);
  65.     }
  66.   }
  67.   if (count($newParams) != 0) {
  68.     $queryString_Test = "&" . htmlentities(implode("&", $newParams));
  69.   }
  70. }
  71. $queryString_Test = sprintf("&totalRows_Test=%d%s", $totalRows_Test, $queryString_Test);
  72. ?>
  73.  
  74. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  75. <html xmlns="http://www.w3.org/1999/xhtml">
  76. <head>
  77. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  78. <title>Callaloo Foods - Products</title>
  79.  
  80. <link rel="stylesheet" href="animated-menu/animated-menu.css"/>  
  81.  
  82. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
  83.     <script src="animated-menu/js/jquery.easing.1.3.js" type="text/javascript"></script>
  84.     <script src="animated-menu/animated-menu.js" type="text/javascript"></script>
  85. <!-- JavaScripts-->
  86.  
  87. <script type="text/javascript" src="s3Slider.js"></script>
  88. <script type="text/javascript">
  89.     $(document).ready(function() {
  90.         $('#slider1').s3Slider({
  91.             timeOut: 10000
  92.         });
  93.     });
  94. </script>
  95. </head>
  96.  
  97. <body>
  98. <div id="container">
  99.    <div id="header">
  100.    <img src="http://bytes.com/images/header_logo_top.jpg" width="898" height="95" border="0" usemap="#Map" />
  101.       <map name="Map" id="Map">
  102.       <area shape="rect" coords="224,1,673,89" href="index.php" alt="home" />
  103.       </map>
  104.  
  105.       <ul class="nav">
  106.       <img src="http://bytes.com/images/header_logol_bottom.jpg" align="left"/> 
  107.          <li class="green-home">  
  108.             <p><a href="index.php">Home</a></p>  
  109.             <p class="subtext"></p>  
  110.          </li>  
  111.          <li class="green-background">  
  112.             <p><a href="background.php">Background</a></p>  
  113.             <p class="subtext"></p>  
  114.          </li>  
  115.          <li class="green-products">  
  116.             <p><a href="products.php">Products</a></p>  
  117.             <p class="subtext"></p>  
  118.          </li>  
  119.          <li class="green-contact">  
  120.             <p><a href="contact.php">Contact</a></p>  
  121.             <p class="subtext"></p>  
  122.          </li>   
  123.          <li class="green-locations">  
  124.             <p><a href="locations.php">Locations</a></p>  
  125.             <p class="subtext"></p>  
  126.          </li>  
  127.       <img src="http://bytes.com/images/header_logor_bottom.jpg" align="right"/> 
  128.       </ul>
  129.    </div>
  130.  
  131.    <div id="thankyou">
  132.       <div id="gallery">
  133.          <div id="content">
  134.             <h1>Products</h1>
  135.  
  136.             <form id="form1" name="form1" method="get" action="products.php">
  137.   <p>
  138.     <label>Narrow Your Search:
  139.       <select name="Product_Type" method="post">
  140.       <option value="Product_Type" selected="selected">All</option>
  141.         <option value="Chips">Chips</option>
  142.         <option value="Dips, Marinades">Dips, Marinades</option>
  143.         <option value="Rubs">Rubs</option>
  144.       </select>
  145.     </label>
  146.     <label>
  147.       <input type="submit" name="button" id="button" value="Submit" />
  148.     </label>
  149.   </p>
  150. </form>
  151.  
  152.          </div>
  153.          <div id="contentbody">
  154.             <?php do { ?>
  155.             <p><img src = "http://bytes.com/images/<?php echo $row_Test['Product_Image_Small']; ?>" align="left"/><br />
  156.                <a href="productdetails.php?recordID=<?php echo $row_Test['Product_ID']; ?>" >
  157.                <?php echo $row_Test['Product_Title']; ?></a><br />
  158.                <?php echo $row_Test['Product_Type']; ?><br />
  159.                <?php echo $row_Test['Product_Description']; ?><br />
  160.                <?php } while ($row_Test = mysql_fetch_assoc($Test)); ?>
  161.             </p>
  162.          </div>
  163.             <p>&nbsp;</p>
  164.             <h2><br />
  165.                <table border="0">
  166.                   <tr>
  167.                      <td><?php if ($pageNum_Test > 0) { // Show if not first page ?>
  168.                             <p><a href="<?php printf("%s?pageNum_Test=%d%s", $currentPage, 0, $queryString_Test); ?>">First</a>
  169.                             </p>
  170.                          <?php } // Show if not first page ?></td>
  171.                      <td><?php if ($pageNum_Test > 0) { // Show if not first page ?>
  172.                             <p><a href="<?php printf("%s?pageNum_Test=%d%s", $currentPage, max(0, $pageNum_Test - 1), $queryString_Test); ?>">Previous</a>
  173.                             </p>
  174.                          <?php } // Show if not first page ?></td>
  175.                      <td><?php if ($pageNum_Test < $totalPages_Test) { // Show if not last page ?>
  176.                             <p><a href="<?php printf("%s?pageNum_Test=%d%s", $currentPage, min($totalPages_Test, $pageNum_Test + 1), $queryString_Test); ?>">Next</a>
  177.                             </p>
  178.                          <?php } // Show if not last page ?></td>
  179.                      <td><?php if ($pageNum_Test < $totalPages_Test) { // Show if not last page ?>
  180.                             <p><a href="<?php printf("%s?pageNum_Test=%d%s", $currentPage, $totalPages_Test, $queryString_Test); ?>">Last</a>
  181.                             </p>
  182.                          <?php } // Show if not last page ?></td>
  183.                   </tr>
  184.                </table>
  185.             </h2>
  186.       </div>
  187.       <p>    Records <?php echo ($startRow_Test + 1) ?> to <?php echo min($startRow_Test + $maxRows_Test, $totalRows_Test) ?> of <?php echo $totalRows_Test ?> <br />
  188.       </p>
  189.       <p>&nbsp;</p>
  190.    </div>
  191. </div>
  192.  
  193.    <div id="footer">
  194.        <ul>
  195.              <li><a href="index.php">home</a></li>
  196.           <li><a href="background.php">background</a></li>
  197.           <li><a href="products.php">products</a></li>
  198.           <li><a href="contact.php">contact</a></li>
  199.           <li><a href="locations.php">locations</a></li>
  200.       </ul>
  201.    </div>
  202. </div>
  203. </body>
  204. </html>
  205. <?php
  206. mysql_free_result($Test);
  207. ?>
  208. </code>
Dec 3 '12 #1
3 2526
zmbd
5,501 Expert Mod 4TB
reigns7703
As I've said in other posts... I'm a tad out my depth in PHP. I normally work w/vba and dabble in little C++.

By way of general trouble shooting...
Using the formatted code block in your post :)
- Is line 44 where you set the record source for your list box?
- Have you obtained what the actual value of the SQL that is feeding your list box at runtime? In VBA I'd set a debug.print variableofinterest I'm sure that there is something similar, or you could simply send it to stdout.
- How many records were you expecting on an unfiltered (return everything) query?
-- In relation to the afore, how many records did you return?

I'm sure those with more PHP experience than mine will have other questions... :)
Dec 9 '12 #2
Rabbit
12,516 Expert Mod 8TB
Use an if statement to decide whether or not to put in the WHERE clause.
Dec 9 '12 #3
Im a bit confused with your script, but what I would have done is when i create select html element, I will add onchange='jsFunction()' and that will call my javascript function when user selects something from a dropdown list, from javascript I will use ajax request to my php script pass in the get or post values and get data back, use jquery to populate the select dropdown, Here is silimar script that i use
Expand|Select|Wrap|Line Numbers
  1.  <select onchange="loadAssignments()" class="" id="language">
  2.                             <option>Select a language</option>
  3.                             <option>C++</option>
  4.                             <option>Java</option>
  5.                         </select>
Now from my javascript i do this

Expand|Select|Wrap|Line Numbers
  1. function loadAssignments(){
  2.         var req = new XMLHttpRequest();
  3.         var lang = $('#language>option:selected').text();
  4.         if (lang == "C++"){
  5.             lang = "Cplusplus";
  6.         }
  7.         if(lang == 'Cplusplus' || lang == 'Java'){
  8.             var url = 'checkCode.php'+'?action=getAssignments'+'&language='+lang;
  9.             var out = '';
  10.             req.open('GET',url,true);
  11.             req.send(null);
  12.             req.onreadystatechange = function () {
  13.                 if (req.readyState == 4) {
  14.                     out = req.responseText;
  15.                 }
  16.                 data = JSON.parse(out);                
  17.                 var html = '';
  18.                 for (var i=0;i<data.length;i++){
  19.                     html += '<option>'+data[i]['name']+'</option>';
  20.                 }
  21.                 $('#assignments').html(html);
  22.             }
  23.         }
Not the php script is something like this

Expand|Select|Wrap|Line Numbers
  1. if ($_GET['action'] == 'getAssignments'){
  2.     $language = $_GET['language'];
  3.     $recieved = $Login->getAssignments($language);
  4.     echo(json_encode($recieved));
I have another class which does the interaction with database but you can connect to database here and request what ever you want. Remember that on you select html element to put first option as <option>Select something below<option> and below you put

Expand|Select|Wrap|Line Numbers
  1. <option>All elements<option>
  2. <option>THis element</option> 
Sorry for this long a.s.s answer :D
Dec 11 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: djharrison | last post by:
I'm new to ASP and database programming but I am trying to connect to a database using a concatenation of a strings and a menu item from a form. Ok here's what I have... an asp form that has a...
1
by: Dennis M. Marks | last post by:
Is there a javascript alternative to the dropdown menu in a form (SELECT OPTION). I would like the ability to pass a selected item to a function but I would like more control in the design of the...
3
by: Matthew | last post by:
in a page, I have a dropdown menu, and below the menu there is a dropDownList control. when the menuItem is pulled down, the DropDownList control always on top of the menu div, what cause this? I...
2
by: Ian Davies | last post by:
Hello Ive been informed that I need to do the following in javascript. I have two drop down menus in my php script. The items displayed in the second is dependent on which item is choosen from the...
7
by: Bertman105 | last post by:
Hey all, Heres the situation: I have a form page that needs to have multiple drop down menus that is pulling their options from a database. Right now I have one working out of 3. So that is one...
4
torquehero
by: torquehero | last post by:
Hi all :) I have created a horizontal navbar using Xara Menumaker. The Menu items have several dropdown menus. Its a javascript. When the mouse cursor is moved over any menu item, a dropdown...
0
by: plunder | last post by:
Hi all what I'm trying to do is a flash menu which in some points has a dropdown sub-menu which comes out on mouseover and contains elements which if clicked link to other pages. Point is that these...
1
by: dittu | last post by:
I want to design dropdown menu.This menu contains two item(save,saveContent).When mouse over on Menu then display below like that ____________ Menu | -------------| Save | SaveContent |...
1
by: micropos | last post by:
hey,i m newbie to php.i wan ask that how to pass value from drop down menu into database .after click ''go'' button ,it will out.thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.