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
https://forums.phpfreaks.com/topic/273069-php-switch-problems/
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
https://forums.phpfreaks.com/topic/273069-php-switch-problems/#findComment-1405291
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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