What's supposed to happen: User selects a state, city, and/or radius.
The application then pulls the relevant info and displays it in a table.
The users have the option of selecting available check boxes and clicking submit.
This should mark them as sold.

The problem: I've used foreach to loop the query in each table cell. Now when the user makes selections and clicks submit only one $id (restaurant_id) is being sent to the mark_sold.php page.

select_area.php

<?
include('include/db_con.php');
?>
<html>
<head>
<script type="text/javascript" src="jquery-142.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#city").hide();
	$("#state").change( function() {
        $("#city").hide();
        $("#result").html('Retrieving ...');
        $.ajax({
            type: "POST",
            data: "state=" + $(this).val(),
            url: "get_city.php",
            success: function(msg){
                if (msg != ''){
                    $("#city").html(msg).show();
                    $("#result").html('');
                }
                else{
                    $("#result").html('<em>No item result</em>');
                }
            }
        });
    });
});
</script>

</head>
<form name="FormName" action='available_ads.php' enctype="multipart/form-data" method="post">
<p>
<select id="state" name"state">
    <option value="">Choose a State</option>
    <option value="AK">Alaska</option>
    <option value="AL">Alabama</option>
    <option value="AR">Arkansas</option>
    <option value="AZ">Arizona</option>
    <option value="CA">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
    <option value="DC">District of Columbia</option>
    <option value="DE">Delaware</option>
    <option value="FL">Florida</option>
    <option value="GA">Georgia</option>
    <option value="HI">Hawaii</option>
    <option value="IA">Iowa</option>
    <option value="ID">Idaho</option>
    <option value="IL">Illinois</option>
    <option value="IN">Indiana</option>
    <option value="KS">Kansas</option>
    <option value="KY">Kentucky</option>
    <option value="LA">Louisiana</option>
    <option value="MA">Massachusetts</option>
    <option value="MD">Maryland</option>
    <option value="ME">Maine</option>
    <option value="MI">Michigan</option>
    <option value="MN">Minnesota</option>
    <option value="MO">Missouri</option>
    <option value="MS">Mississippi</option>
    <option value="MT">Montana</option>
    <option value="NC">North Carolina</option>
    <option value="ND">North Dakota</option>
    <option value="NE">Nebraska</option>
    <option value="NH">New Hampshire</option>
    <option value="NJ">New Jersey</option>
    <option value="NM">New Mexico</option>
    <option value="NV">Nevada</option>
    <option value="NY">New York</option>
    <option value="OH">Ohio</option>
    <option value="OK">Oklahoma</option>
    <option value="OR">Oregon</option>
    <option value="PA">Pennsylvania</option>
    <option value="PR">Puerto Rico</option>
    <option value="RI">Rhode Island</option>
    <option value="SC">South Carolina</option>
    <option value="SD">South Dakota</option>
    <option value="TN">Tennessee</option>
    <option value="TX">Texas</option>
    <option value="UT">Utah</option>
    <option value="VA">Vermont</option>
    <option value="WA">Washington</option>
    <option value="WI">Wisconsin</option>
    <option value="WV">West Virginia</option>
    <option value="WY">Wyoming</option>
</select>
</p>
<p>
&nbsp;<select id="city" name="city"></select><br>
</p>
<p id="result">&nbsp;</p>
<input type="text" name="radius" class="formTextbox" size="24"><br>
<input type="submit" name="submit" class="formTextbox" value="Submit">
</form>
</html>

available_ads.php (I originally had it set to live update every 5 seconds but it kept clearing my checkboxes)

<?php
// prevent caching (php)
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Expires: ' . gmdate(DATE_RFC1123, time()-1));
session_start();
include('include/db_con.php');
?>
<html>
<head>
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<title>Available Ads</title>
<style type="text/css">
 td.padded {
 padding:-5px;
 }
</style>
<script type="text/javascript">
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHttp");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();
var httpjob = createRequestObject();

function sndReq(action) {
  http.open('get', 'display_area_updater.php?city=<?=$city?>&state=<?=$state1?>&radius=<?=$radius?>');
  http.onreadystatechange = handleResponse;
  http.send(null);
}
function handleResponse() {
  if(http.readyState == 4) {
    var response = http.responseText;
	document.getElementById('tablesold').innerHTML = response;
  }
}
setInterval('sndReq()');
<!--setInterval('sndReq()', 5000);-->
</script>
</head>
<body>
<div id='tablesold'>
Retrieving data
</div>
</body>

    display_area_updater.php

    <?php
    include('include/db_con.php');
    
    if (!$radius){
    $radius = '1';
    }
    $get_lat = "SELECT AVG(latitude) FROM zipcodes WHERE city = '$city' AND state = '$state'";
    $got_lat = mysql_query($get_lat);
    $rowlat = mysql_fetch_array($got_lat);
    $lat = $rowlat[0];
    
    $get_lon = "SELECT AVG(longitude) FROM zipcodes WHERE city = '$city' AND state = '$state'";
    $got_lon = mysql_query($get_lon);
    $rowlon = mysql_fetch_array($got_lon);
    $lon = $rowlon[0];
    
    $coords = array('latitude' =>  $lat, 'longitude' =>   $lon);
    
    $sql = "SELECT DISTINCT city, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM zipcodes HAVING distance <= {$radius} ORDER BY distance";
    $query = mysql_query($sql);
    //while($row = mysql_fetch_assoc($query)){
    //$selected_cities = $row['city'];
    //echo "$$" . $selected_cities . "$$<br />";
    $names = array();
    while ($row = mysql_fetch_array($query)) {
    $names[] = $row[0];
    }
    $selected_cities = implode("' or '", array_unique($names));
    //echo "{$row['city']} {$row['zipcode']} ({$row['distance']})<br>\n"; 
    //echo $selected_cities;
    $Get_City = mysql_query("SELECT rest_id FROM restaurant_info WHERE state = '$state' AND city = ('$selected_cities') ORDER BY rest_id");
    $rest_ids = array();
    while ($got_city = mysql_fetch_array($Get_City)) {
    $rest_ids[] = $got_city[0];
    }
    print_r(array_values($rest_ids));
    echo "<form name='mark_sold' action='mark_sold.php' enctype='multipart/form-data' method='post'><table width='1100' border='1'><tr><td><h2>Spot 1</h2>";
    	foreach ($rest_ids as $id){
    	//$rest_ids = implode("' or '", $rest_ids);
    	//$rest_ids = "'" . $rest_ids . "'";
    	$Get_Ad = mysql_query("SELECT rest_id, rest_name, city, ad_space1 FROM restaurant_info WHERE rest_id = $id");
    	$got_ad = mysql_fetch_assoc($Get_Ad);
    	$is_sold = $got_ad['ad_space1'];
    	$rest_id = $got_ad['rest_id'];
    	$rest_name = $got_ad['rest_name'];
    	$city = $got_ad['city'];
    	if($is_sold){
    	$is_sold = "<font color='red'>sold</font>";
    	}else{
    	$is_sold = "<font color='green'>available</font><input name='ad1' id='ad1' type='checkbox' value='1'/><input name='rest_id' type='hidden' value='" .$rest_id."' />";
    	}
    	echo $rest_name." in ". $city." is ".$is_sold."<br />";
    	}
        echo "</td><td><h2>Spot2</h2>";
    	foreach ($rest_ids as $id){
    	//$rest_ids = implode("' or '", $rest_ids);
    	//$rest_ids = "'" . $rest_ids . "'";
    	echo $id;
    	$Get_Ad = mysql_query("SELECT rest_id, rest_name, city, ad_space2 FROM restaurant_info WHERE rest_id = $id");
    	$got_ad = mysql_fetch_assoc($Get_Ad);
    	$is_sold = $got_ad['ad_space2'];
    	$rest_id = $got_ad['rest_id'];
    	$rest_name = $got_ad['rest_name'];
    	$city = $got_ad['city'];
    	if($is_sold){
    	$is_sold = "<font color='red'>sold</font>";
    	}else{
    	$is_sold = "<font color='green'>available</font><input name='ad2' type='checkbox' value='1' /><input name='rest_id' type='hidden' value='" .$rest_id."' />";
    	}
    	echo $rest_name." in ". $city." is ".$is_sold."<br />";
    	}
    	echo "</td><td><h2>Spot 3</h2>";
    	foreach ($rest_ids as $id){
    	//$rest_ids = implode("' or '", $rest_ids);
    	//$rest_ids = "'" . $rest_ids . "'";
    	$Get_Ad = mysql_query("SELECT rest_id, rest_name, city, ad_space3 FROM restaurant_info WHERE rest_id = $id");
    	$got_ad = mysql_fetch_assoc($Get_Ad);
    	$is_sold = $got_ad['ad_space3'];
    	$rest_id = $got_ad['rest_id'];
    	$rest_name = $got_ad['rest_name'];
    	$city = $got_ad['city'];
    	if($is_sold){
    	$is_sold = "<font color='red'>sold</font>";
    	}else{
    	$is_sold = "<font color='green'>available</font><input name='ad3' type='checkbox' value='1' /><input name='rest_id' type='hidden' value='" .$rest_id."' />";
    	}
    	echo $rest_name." in ". $city." is ".$is_sold."<br />";
    	}
    	echo "</td><td><h2>Spot 4</h2>";
    	foreach ($rest_ids as $id){
    	//$rest_ids = implode("' or '", $rest_ids);
    	//$rest_ids = "'" . $rest_ids . "'";
    	$Get_Ad = mysql_query("SELECT rest_id, rest_name, city, ad_space4 FROM restaurant_info WHERE rest_id = $id");
    	$got_ad = mysql_fetch_assoc($Get_Ad);
    	$is_sold = $got_ad['ad_space4'];
    	$rest_id = $got_ad['rest_id'];
    	$rest_name = $got_ad['rest_name'];
    	$city = $got_ad['city'];
    	if($is_sold){
    	$is_sold = "<font color='red'>sold</font>";
    	}else{
    	$is_sold = "<font color='green'>available</font><input name='ad4' type='checkbox' value='1' /><input name='rest_id' type='hidden' value='" .$rest_id."' />";
    	}
    	echo $rest_name." in ". $city." is ".$is_sold."<br />";
    	}
    	echo "</td><td><h2>Spot 5</h2>";
    	foreach ($rest_ids as $id){
    	//$rest_ids = implode("' or '", $rest_ids);
    	//$rest_ids = "'" . $rest_ids . "'";
    	$Get_Ad = mysql_query("SELECT rest_id, rest_name, city, ad_space5 FROM restaurant_info WHERE rest_id = $id");
    	$got_ad = mysql_fetch_assoc($Get_Ad);
    	$is_sold = $got_ad['ad_space5'];
    	$rest_id = $got_ad['rest_id'];
    	$rest_name = $got_ad['rest_name'];
    	$city = $got_ad['city'];
    	if($is_sold){
    	$is_sold = "<font color='red'>sold</font>";
    	}else{
    	$is_sold = "<font color='green'>available</font><input name='ad5' type='checkbox' value='1' /><input name='rest_id' type='hidden' value='" .$rest_id."' />";
    	}
    	echo $rest_name." in ". $city." is ".$is_sold."<br />";
    	}
    	echo "</td></tr><tr><td><h2>Spot 6</h2>";
    	foreach ($rest_ids as $id){
    	//$rest_ids = implode("' or '", $rest_ids);
    	//$rest_ids = "'" . $rest_ids . "'";
    	$Get_Ad = mysql_query("SELECT rest_id, rest_name, city, ad_space6 FROM restaurant_info WHERE rest_id = $id");
    	$got_ad = mysql_fetch_assoc($Get_Ad);
    	$is_sold = $got_ad['ad_space6'];
    	$rest_id = $got_ad['rest_id'];
    	$rest_name = $got_ad['rest_name'];
    	$city = $got_ad['city'];
    	if($is_sold){
    	$is_sold = "<font color='red'>sold</font>";
    	}else{
    	$is_sold = "<font color='green'>available</font><input name='ad6' type='checkbox' value='1' />";
    	}
    	echo $rest_name." in ". $city." is ".$is_sold."<br />";
    	}
    //blah blah blah
    	echo "</td></tr><tr><td colspan='5'><center><input type='submit' name='submit' value='Submit'></center></td></tr></table></form>";
    mysql_close();		
    ?>

      Right now I just have ad_space1 and ad_space2 in the query but I will be adding all of them once it starts working.

      mark_sold.php

      <?
      include('include/db_con.php');
      ?>
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Credit Card Processing Goes Here</title>
      <script type="text/javascript">
      <!--
      function delayer(){
          window.location = "upload_copy.php"
      }
      //-->
      </script>
      </head>
      <!--<body onLoad="setTimeout('delayer()', 5000)">
      <body onLoad="setTimeout('delayer()')">-->
      Processing Credit Card Info<br />
      <img src="animation_processing.gif" />
      </body>
      </html>
      <?
      $arrayObj = new ArrayObject(get_defined_vars());
      for($iterator = $arrayObj->getIterator(); $iterator->valid(); $iterator->next())
              {
              echo $iterator->key() . ' => ' . $iterator->current() . '<br />';
              }
      mysql_query("UPDATE restaurant_info SET ad_space1='$ad1', ad_space2='$ad2' WHERE rest_id='$rest_id'") or die(mysql_error());
      ?>

        To simplify, my problem is that I have a loop running in each table cell. The first time is runs $id may = 1 the next time $id might = 3. That's fine for simply echoing info. I need to use $id in my processing page. How do I loop a query and assign a different variable name to the results each time? Then, how do I use those results in my processing page?

          How do I do this?
          loop a query for the restaurant_id's and related info and assign the results to variables named from the array
          example loop 1:$0rest_id = rest_id, $0related_info = related_info
          example loop 2:$1rest_id = rest_id, $1related_info = related_info

          then, on my processing page take those variables and loop UPDATE table SET info = $0stuff_from_form WHERE restaurant_id = $0rest_id
          then UPDATE table SET info = $1stuff_from_form WHERE restaurant_id = $1rest_id
          and so on

            Write a Reply...