Jump to content

PHP Switch Problems


DSTR3

Recommended Posts

I have a Switch case that is not loading a dropdown list. Any help in finding a solution, I would be grateful.

 

<?php
$dbc = mysql_connect('','','')
 or die('Error connecting to MySQL server.');

mysql_select_db('MyDB');
$list['place']   =  mysql_query("select * from tblRestaurants order by RestName ASC");
$list['cuisine'] =  mysql_query("select * from tblCuisines order by CuisineName ASC");
foreach($list as $key=>$value){
while ($nt = mysql_fetch_assoc($list[$key]))
 $list_array[$key] = $nt;
}
if(isset($_GET["ajax"]))
{
  switch($_GET['case']){
   case 'place':
	  echo json_encode($list_array['place']);
	  break;
   case 'cuisine':
	  echo json_encode($list_array['cuisine']);
	  break;
}
  die();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
   <script type="text/javascript">
function displayPlace()
{
   $.getJSON("Saturday.php?ajax=true", function(data) {
    $.each(data, function(index, objRecord) {
	    var option=document.createElement("option");
	    option.value=objRecord.RestID;
	    option.text=objRecord.RestName;
	    $("#Doggie").append('<option value="' + objRecord.RestID + '">' + objRecord.RestName + '</option>');
    });
   });

}
function displayCuisine()
{
   $.getJSON("Saturday.php?ajax=true", function(data) {
    $.each(data, function(index, objRecord) {
	    var option=document.createElement("option");
	    option.value=objRecord.CuisineID;
	    option.text=objRecord.CuisineName;
	    $("#Doggie").append('<option value="' + objRecord.CuisineID + '">' + objRecord.CuisineName + '</option>');
    });
   });

}

   </script>
   <title>SEARCH</title>
</head>
<body>
    <form>
    <button type="button"  onclick="javascript:displayPlace();">Place</button>
    <button type="button"  onclick="javascript:displayCuisine();">Cuisine</button>
    <button type="button"  onclick="javascript:displayCity();" >City</button>
    <button type="button"  onclick="javascript:displayState();">State</button>
    <button type="button"  onclick="javascript:displayZipCode();">Area</button>
    <br />

    <select name="Doggie" id="Doggie"></select>

    <br />
   </form>
</body>
</html>

Link to comment
Share on other sites

$.getJSON("Saturday.php?ajax=true", function(data) {

You are not passing the "case" value to tell the script which value to return.

Try

$.getJSON("Saturday.php?ajax=true&case=place", function(data) {

 

But, why, oh, WHY are you retrieving all of the data from two tables, just to return one of them. You are wasting a lot of resources:

 

if (isset($_GET['ajax'])) {
   $case = (isset($_GET['case']) ? $_GET['case'] : false);

   switch($case) {
       case 'place':   $sql = 'select * from tblRestaurants order by RestName ASC';
               break;
       case 'cuisine': $sql = 'select * from tblCuisines order by CuisineName ASC';
               break;
   }

   if (isset($sql)) {
       $res = mysql_query($sql);
       while ($row = mysql_fetch_assoc($res)) {
           $date[] = $row;
       }
       echo json_encode($data);
   }
   die();
}

Or something like that.

(untested, off the top of my head)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.