Jump to content

question on loops


garry27

Recommended Posts

i'm hoping someone can help me with this as its difficult to debug. i'm looping through an array inside another loop and i need to know if i'm doing it right.

$row and $row2 are arrays pulled out of a db with mysql_fetch_assoc(). what i'm trying to do with the second loop is to check if the value in key pub_id (now $pub_id as its been extracted) is equal to any of the values in $row2. if the loop comes across an equal value, i want it to add a 'no' value to $add_pub_link and exit the second loop.


[code]while($row)
  {
    $i++;
    extract($row);

/***check if pub in $row is in the current crawl order************************/
while (list(, $val) = each($row2)) {
if ($val == $pub_id) {
  $add_pub_link = 'no'; //assigns 'no' value to $add_pub_link if exists in crawl
  break 2;
}
}
[/code]
         
Link to comment
Share on other sites

This sounds like something you'd be better off using a SQL join for, but I could be wrong.  If you're aware of joins and how they work, nevermind.  If you're not, why don't you tell us a little about your database or show us the two relevant queries.
Link to comment
Share on other sites

i prefer to use where clauses to using joins as i find them much easier to read and simpler syntax. below is the main bulk of the code which i'm working on right now.

[code]
  db_connect();

  $sql = mysql_query ("SELECT Pubs.Latitude AS lat, Pubs.Longitude AS lng, Pubs.pubID AS pub_id, Pubs.pubName AS name,
                      Pubs.pubStreetNo AS streetNo, Pubs.pubStreet AS street, Pubs.pubCity AS city,
  Pubs.pubCounty AS county, Pubs.pubPostcode AS postcode, Pubs.pubPhone AS phone
  FROM Pubs, Amenities
  WHERE Pubs.pubID=Amenities.pubID $amenity_query
  AND (Pubs.Longitude > $swlng AND Pubs.Longitude < $nelng)
  AND (Pubs.Latitude <= $nelat AND Pubs.Latitude >= $swlat)
                      ORDER BY lat") or die( 'Invalid query: ' . mysql_error() );
   
  $row = mysql_fetch_assoc($sql);
 
 
  $crawl_id = $_GET['crawlID']; 
  $sql2 = mysql_query ("SELECT * FROM Pub_Crawl_Order
                        WHERE crawlID = $crawl_id")
                        or die( 'Invalid query: ' . mysql_error() );
  $row2 = mysql_fetch_assoc($sql2);
 
 
 
  $list = $distanceList = array();
  $i = 0;
 
  while($row)
  {
    $i++;
    extract($row);

/***check if pub in $row is in the current crawl order************************/
while (list(, $val) = each($row2)) {
if ($val == $pub_id) {
  $add_pub_link = 'no'; //assigns 'no' value to $add_pub_link if exists in crawl
  break 2;
}
}
if (!isset ($add_pub_link) ) {
$add_pub_link = 'yes';  //assigns 'yes' value to $add_pub_link if not
}
         
    $list[$i] = "p{$i}:  { lat:{$lat}, lng:{$lng}, name:'{$name}', streetNo:'{$streetNo}', street:'{$street}',       
                      city:'{$city}', county:'{$county}', postcode:'{$postcode}', phone:'{$phone}',
  addPubLink:'{$add_pub_link}', pubID:'{$pub_id}' }";// streetNo:{$streetNo},
$distanceList[$i] = surfaceDistance($lat,$lng,$knownLat,$knownLng,'km');
$row = mysql_fetch_assoc($sql);
  }
[/code]

the first query is querying my db for pubs within the the map boundaries of my google map, filtered by amenities. the first while loop loops through each record and creates a javascript data structure of info regarding the pub for later use.

the second query and the second loop, i've introduced. i want to add a new element to each javascript object that asigns a 'no' value to add_pub_link if the current pub in the loop is not in the current pub crawl list.

thanks for the help guys. you can see an example of my site at http://www.nl-webspace.co.uk/~unn_p921847/test3/findpubs.php (logon using 'personal' for email and 'account' for password.  ;)

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.