Need some help with this code that I haven't code.

Discussion in 'PHP' started by co.ador, Nov 28, 2009.

  1. #1
    Below is a form which search restaurants by state, zip code, name, type of food and offering, the code is working perfectly. It pulls all the states, zip code, type of food and offering data from the database, as you will see in the queries in the script. There is a query that pull the restaurant list according to the user's selections in the form. When the user get the selection in the form it suppose to display the list of restaurants names and images but I can't get to display it. Help!

    This is the query that search for the list of restaurants according to the user's selection in the form.

    Query that search for restaurants
    
    
    ?<php 
    // Build search query and embed filters
        $strSQL = sprintf(
        'SELECT
              r.restaurants_id
              ,r.restaurantname
              ,r.image
           FROM
              restaurants r
             %s
             %s
             %s'
         ,$boolIncludeZipCodes === true?'INNER JOIN zip_codes z ON r.restaurants_id = z.restaurants_id':''
         ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters)  
         ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':''
        );
        
        
        
        $arrResult = mysql_query($strSQL);
        while($arrRow = mysql_fetch_assoc($arrResult)) {
            $arrRestaurants[] = $arrRow;
        }
            
    }
    ?>
    PHP:
    html frame i have set up that will display the restaurants name and image
    <div id="container4">
      <div class="wrap">
    <?php
    
    // print search query
    if(!empty($strSQL)) { printf('<p>%s</p>',$strSQL);
    foreach($arrRestaurants as $arrRestaurant) {
    {
      echo "<div class=\"shoeinfo1\">
       <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" />
          <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2>
          <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div>
    
          
        </div>";
    $i++; 
    }
    if ($i > 1 && $i % 3 == 0 ) 
    {
      echo "<div class=\"clearer\"></div>";
    
    }
    
    }
    
    }?>
    </div>
    </div>
    
    PHP:

    the whole code
    <?php
    $strSQL = '';
    $arrRestaurants = array();
    
    // place holder form data variables
    $strName = '';
    $strZipCode = '';
    $strState = '';
    $arrFoodTypes = array();
    $arrOfferings = array();
    
    // food types and offerings drop down build arrays
    $arrRestaurantsFoodTypes = array('values'=>array(),'output'=>array());
    $arrStates = array('','AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY',);
    $arrRestaurantsOfferings = array();
    
    $arrResult = mysql_query('SELECT restaurant_food_types_id,name FROM RESTAURANT_FOOD_TYPES');
    while($arrRow = mysql_fetch_assoc($arrResult)) {
        $arrRestaurantsFoodTypes['values'][] = $arrRow['restaurant_food_types_id'];
        $arrRestaurantsFoodTypes['output'][] = $arrRow['name'];
    }
    
    
    $arrResult = mysql_query('SELECT restaurant_offerings_id,name FROM RESTAURANT_OFFERINGS');
    while($arrRow = mysql_fetch_assoc($arrResult)) {
        $arrRestaurantsOfferings[] = $arrRow;
    }
    
    
    
    
    // reset form
    $boolReset = isset($_POST['frmSearch']) && isset($_POST['frmSearch']['reset'])?true:false;
    
    if($boolReset === false && isset($_POST['frmSearch'])) {
    
        // Extract POST variables and escape
        $strName = isset($_POST['frmSearch']['name'])?/*mysql_real_escape_string(*/$_POST['frmSearch']['name']/*)*/:'';
        $strZipCode = isset($_POST['frmSearch']['zipcode'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['zipcode']/*)*/:'';
        $strState = isset($_POST['frmSearch']['state'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['state']/*)*/:'';
        $arrFoodTypes = isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['food_types']:array();
        $arrOfferings = isset($_POST['frmSearch']['offerings'])?$_POST['frmSearch']['offerings']:array();
    
        // WHERE clause filters
        $arrSQLFilters = array();
        
        // whether or not zip codes table needs to be included
        $boolIncludeZipCodes = false;
    
        // Zipcode filter
        if(!empty($strZipCode)) {
            $boolIncludeZipCodes = true;
        
            $arrSQLFilters[] = sprintf(
                "z.zip LIKE '%s'"
                ,"%$strZipCode%"
            );
        }
        
        // State filter
        if(!empty($strState)) {
            $boolIncludeZipCodes = true;
        
            $arrSQLFilters[] = sprintf(
                "z.state = '%s'"
                ,$strState
            );
        }
    
        // Restaurants name filter
        if(!empty($strName)) {
            $arrSQLFilters[] = sprintf(
                "r.restaurantname LIKE '%s'"
                ,"%$strName%"
            );
        }
    
        // Food types filter
        if(!empty($arrFoodTypes) && !empty($arrFoodTypes[0])) {
        $arrSQLFilters[] = sprintf(
              'r.restaurants_id IN
                   (SELECT
                         DISTINCT restaurants_id
                      FROM
                         restaurants_restaurant_food_types
                     WHERE
                         restaurants_food_types_id IN (%s)
                     GROUP
                        BY
                         restaurants_id
                    HAVING 
                         COUNT(DISTINCT restaurants_id) = %u)'
                ,/*mysql_real_escape_string(*/ implode(',',$arrFoodTypes) /*)*/
                ,count($arrFoodTypes)
            );
        }
    
        // Offerings Filter ie. eat-in, lunch, dinner, etc
        if(!empty($arrOfferings)) {
          $arrSQLFilters[] = sprintf(
              'r.restaurants_id IN
                   (SELECT
                         DISTINCT restaurants_id
                      FROM
                         restaurants_to_restaurant_offerings
                     WHERE
                         restaurant_offerings_id IN (%s)
                     GROUP
                        BY
                         restaurants_id
                    HAVING 
                         COUNT(DISTINCT restaurants_id) = %u)'
                ,/*mysql_real_escape_string(*/ implode(',',$arrOfferings) /*)*/
                ,count($arrOfferings)
            );
        }
    
        // Build search query and embed filters
        $strSQL = sprintf(
        'SELECT
              r.restaurants_id
              ,r.restaurantname
              ,r.image
           FROM
              restaurants r
             %s
             %s
             %s'
         ,$boolIncludeZipCodes === true?'INNER JOIN zip_codes z ON r.restaurants_id = z.restaurants_id':''
         ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters)  
         ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':''
        );
        
        
        
        $arrResult = mysql_query($strSQL);
        while($arrRow = mysql_fetch_assoc($arrResult)) {
            $arrRestaurants[] = $arrRow;
        }
            
    }
    ?>
    <div id="wrapper">
    <form class="abajo"  name="frmSearch" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    
    	<fieldset class="primero">
    		<legend class="primerosub">Find A Restuarant</legend>
            <fieldset class="segundo1" style="border-color:#FFFFFF" >  
    <legend>Location Details</legend>
            <ol>
                <li class="restaurants-name">
                    <label for="restaurants-name">Name</label>
                    <input type="text" name="frmSearch[name]" value="<?php echo $strName; ?>" id="restaurants-name">
                </li>
                <li class="restaurants-zipcode">
                    <label for="restaurants-zipcode">Zip</label>
                    <input type="text" name="frmSearch[zipcode]" value="<?php echo $strZipCode; ?>" maxlength="5" id="restaurants-name">
                </li>
                
                <?php if(!empty($arrStates)) { ?>
                    <li class="restaurants-state">
                        <label for="restaurants-state">State</label>
                        <select name="frmSearch[state]" id="restaurants-state">
                            <?php
                            foreach($arrStates as $strStateAbb) {
                                printf(
                                    '<option value="%s"%s>%s</option>'
                                    ,$strStateAbb
                                    ,strcmp($strState,$strStateAbb) == 0?' selected="selected"':''
                                    ,strcmp($strStateAbb,'')==0?'--':$strStateAbb
                                );
                            }    
                            ?>
                        </select>
                    </li>
                <?php } ?>
                
                <?php if(!empty($arrRestaurantsFoodTypes)) { ?>
                    <li class="restaurants-food-types">
                        <label for="restaurants-food-types">Food Type</label>
                        <select name="frmSearch[food_types][]" id="restaurants-food-types">
                            <?php
                            foreach($arrRestaurantsFoodTypes['values'] as $intIndex=>$intFoodTypesId) {
                                printf(
                                    '<option value="%s"%s>%s</option>'
                                    ,$intFoodTypesId
                                    ,in_array($intFoodTypesId,$arrFoodTypes)?' selected="selected"':''
                                    ,$arrRestaurantsFoodTypes['output'][$intIndex]
                                );
                            }    
                            ?>
                        </select>
                    </li>
                    </ol>
                <?php } ?>
                </fieldset>
                
                <?php if(!empty($arrRestaurantsOfferings)) { ?>
     <fieldset class="tercero" style="" >
    <legend>Services</legend>
    
             
                    
                        <?php
                        foreach($arrRestaurantsOfferings as $arrRestaurantsOffering) {
                            printf(
                                '<ol><li class="restaurants-offerings-%u">
                                      <input type="checkbox" name="frmSearch[offerings][]" value="%u" id="restaurants-offerings-%u"%s>
                                      <span for="restaurants-offerings-%u" class="checkboxes23">%s</span>
                                </li></ol>'
                                ,$arrRestaurantsOffering['restaurant_offerings_id']
                                ,$arrRestaurantsOffering['restaurant_offerings_id']
                                ,$arrRestaurantsOffering['restaurant_offerings_id']
                                ,in_array($arrRestaurantsOffering['restaurant_offerings_id'],$arrOfferings)?' checked="checked"':''
                                ,$arrRestaurantsOffering['restaurant_offerings_id']
                                ,$arrRestaurantsOffering['name']
                            );
                        }
                    ?>
                    
             
                            <?php } ?></fieldset>
                
                <li class="submit">
                    <input type="submit" value="Submit" name="frmSearch[submit]">
                </li>
                
                <li class="reset">
                    <input type="submit" value="reset" name="frmSearch[reset]">
                </li>
                
            </ul>
        </fieldset>
    </form>
    </div>
    <div id="container4">
      <div class="wrap">
    <?php
    
    // print search query
    if(!empty($strSQL)) { printf('<p>%s</p>',$strSQL);
    foreach($arrRestaurants as $arrRestaurant) {
    {
      echo "<div class=\"shoeinfo1\">
       <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" />
          <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2>
          <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div>
    
          
        </div>";
    $i++; 
    }
    if ($i > 1 && $i % 3 == 0 ) 
    {
      echo "<div class=\"clearer\"></div>";
    
    }
    
    }
    
    }?>
    </div>
    </div>
    
    PHP:
    I have been trying for a while to figure out how to display the data, I can see all the queries and php codes inside the script but seem to lack some organization of my part to get it to display.

    Any suggestion and help will be greatly appreciated
     
    co.ador, Nov 28, 2009 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    dO you have any data in DB

    Also not clear what you want to achieve?

    Regards

    Alex
     
    kmap, Nov 29, 2009 IP
  3. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes I have data in all the tables. Tables restaurants_to_restaurant_offerings and restaurants_restaurant_food_types are pivot tables or relational table in between two tables, I am not sure if that's suppose to have data in it since they are link of a many-to-many relationship.

    Now the restaurants table does has data in it.

    <?php $strSQL = sprintf(
        'SELECT
              r.restaurants_id
              ,r.restaurantname
              ,r.image
           FROM
              restaurants r
             %s
             %s
             %s'
         ,$boolIncludeZipCodes === true?'INNER JOIN zip_codes z ON r.restaurants_id = z.restaurants_id':''
         ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters)  
         ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':''
        );
    
      $arrResult = mysql_query($strSQL);
        while($arrRow = mysql_fetch_assoc($arrResult)) {
            $arrRestaurants[] = $arrRow;
    ?>
        
    PHP:
    Now I don't know if the problem that is causing the data not to display is at that query arrangement or if it is at the html frame where the data is displayed..

    If you notice in the while loop the variable $arrRestaurants should contain the array values or list of restaurants to be display in the html frame below. notice at the html frame the variable $arrRestaurants is use at the foreach loop to obtain the array values or list of restaurants obtained at the query above through the while loop.


    <div id="container4">
      <div class="wrap">
    
    
    <?php
    // print search query
    if(!empty($strSQL)) { printf('<p>%s</p>',$strSQL);
    foreach($arrRestaurants as $arrRestaurant) { // foreach loop using the $arrRestaurants variable containing the list of restaurants extracted from the query above.
    {
      echo "<div class=\"shoeinfo1\">
       <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" />
          <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2>
          <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div>
    
          
        </div>";
    $i++; 
    }
    if ($i > 1 && $i % 3 == 0 ) 
    {
      echo "<div class=\"clearer\"></div>";
    
    }
    
    }
    
    }
    
    ?>
    </div>
    </div>
    
    
    PHP:
     
    co.ador, Nov 29, 2009 IP