garry27 Posted December 13, 2006 Share Posted December 13, 2006 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 https://forums.phpfreaks.com/topic/30436-question-on-loops/ Share on other sites More sharing options...
trq Posted December 13, 2006 Share Posted December 13, 2006 You can't use a while for either of these loops, it will never stop. You'll need to use a foreach(), other than that, the logic looks pretty good. Link to comment https://forums.phpfreaks.com/topic/30436-question-on-loops/#findComment-140142 Share on other sites More sharing options...
roopurt18 Posted December 13, 2006 Share Posted December 13, 2006 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 https://forums.phpfreaks.com/topic/30436-question-on-loops/#findComment-140146 Share on other sites More sharing options...
garry27 Posted December 13, 2006 Author Share Posted December 13, 2006 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 https://forums.phpfreaks.com/topic/30436-question-on-loops/#findComment-140164 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.