mac_gabe Posted November 17, 2010 Share Posted November 17, 2010 I have an HTML list of locations, which I want to separate out into countries and cities, then put in an HTML menu with cities as submenu items of countries. My method is to turn the locations into two arrays, the original one with HTML links and the second one with just the name of the location, see if location matches an array of country name, and parse accordingly. I've done it successfully with a test array of locations $countries= array('Angola','Australia','Brazil','Croatia','France','Germany','Iceland','Italy','Kenya','Monaco','Panama','Romania','Switzerland','Tanzania','USA'); $test_location= array('Angola','Luanda','Australia','Brazil','Rio','Minas','Sao Paulo','Croatia','France', 'Paris'); // used in test, and works But my script doesn't work with the real html data.eg <li><a href="http://mysite.com/category-brazil.php" class="blog-category-link-enabled">Brazil (10)</a></li> <li><a href="http://mysite.com/category-igua00e7u.php" class="blog-category-link-enabled">Iguaçu (9)</a></li> <li><a href="http://mysite.com/category-rio-grande-do-sul.php" class="blog-category-link-enabled">Rio Grande do Sul (4)</a></li> <li><a href="http://mysite.com/category-germany.php" class="blog-category-link-enabled">Germany (12)</a></li> <li><a href="http://mysite.com/category-france.php" class="blog-category-link-enabled">France (12)</a></li> <li><a href="http://mysite.com/category-cote-d0027azur.php" class="blog-category-link-enabled">Cote d'Azur (2)</a></li> This part of the script is to identify two consecutive countries and print the first. It works with $test_location but doesn't with $location_short in the final if clause. If I can get this to wotk then I think the rest (not included here) will work too. <?php //$locations_categories is a long string of html links, separated by commas $location = explode(",", $locations_categories); // turns locations into an array of links - works, tested by print $location_count= count ($location); //works, tested by print $countries= array('Angola','Australia','Brazil','Croatia','France','Germany','Iceland','Italy','Kenya','Monaco','Panama','Romania','Switzerland','Tanzania','USA'); $test_location= array('Angola','Luanda','Australia','Brazil','Rio','Minas','Sao Paulo','Croatia','France', 'Paris'); // used in test, and works $test_location_count= count ($test_location); //works //// finds two consecutive countries and prints first //// for ($u=0; $u < $location_count; $u++) { $v = $u+1; $w = $u-1; $location_link[$u] = $location[$u]; //keeps copy of array with links and numbers, works //make array with just country names. No html. For matching against $countries. Retains escaped chars, strips final space. //looks OK and can print out a list of locations but doesn't work in if clause below $location_short[$u] = preg_replace('/(.*)enabled"\>([\w\d\'&#; ]+)( \(.*)/', '$2', $location[$u]); //works, tested, but has string "Cote d'Azur" $location_short[$u] = str_replace("'",'',$location_short[$u]); // Remove apostrophe, so Cote dAzur, just in case, tested //if ((in_array ($test_location[$u], $countries)) && (in_array ($test_location[$v], $countries))) //simple test works if ((in_array ($location_short[$u], $countries)) && (in_array ($location_short[$v], $countries))) // doesn't work {print $location_link[$u] ;} //works when using $test_location above but not with $location_short } ?> Many thanks for any help Quote Link to comment Share on other sites More sharing options...
mac_gabe Posted November 17, 2010 Author Share Posted November 17, 2010 The central term $2 in the preg_replace above hasn't displayed properly. It should be: ([\w\d\'&#; ]+) The digits, ampersand, hash and semicolon are to catch escaped "foreign" letters, and the apostrophe is for apostrophes, like "Cote d'Azur". The space is for longer names like "New York". Quote Link to comment Share on other sites More sharing options...
mac_gabe Posted November 17, 2010 Author Share Posted November 17, 2010 This is really wierd, now it seems to work I made some very minor changes - removed an unnecessary escape before > in the preg search term (but I've since tested this by putting the escape back and it still works) - so I can't believe that was the problem. I'm wondering if there isn't some caching of variables going on that might be causing a problem, as I made a few changes higher up on the page. Very confused, but at least it seems to work now. I'm now going to put back the rest of the script and see if it works in its entirety. By the way, in the original problem I wasn't getting an error message, it was just not showing the desired matched countries. Quote Link to comment Share on other sites More sharing options...
mac_gabe Posted November 17, 2010 Author Share Posted November 17, 2010 Argg - false alarm, it still doesn't work. My server had logged me off, so changes weren't being uploaded. Quote Link to comment Share on other sites More sharing options...
mac_gabe Posted November 19, 2010 Author Share Posted November 19, 2010 I've solved the problem with a workaround - my goodness these little things can take a very long time! I've spent the best part of two weeks working on this problem from PHP scratch! Hopefully I've learnt a few skills along the way which will come in useful for other projects. In the end I did away with the && condition altogether - there seemed to be less that could go wrong that way. I made a few changes to how the HTML would be printed out which meant I needed far fewer conditional clauses, so again less room for error. In the end it looked something like <ul><ul> if (matches a country ) {print </ul></li><li>country html<ul>} else {print <li>city html</li>} </ul></ul> I also removed many of the unneccessary extra variables I had, unneccessary escaping, and unneccessary commenting. I think the problem was probably something very small, like a closing tag missing in the html or one too many brackets, and by cleaning up the code it made it easier to get it right. I still don't know how you're meant to be able to count the brackets in an if clause though, I stare at the code over and over and actually have to place my finger on the screen to be able to work out whether I need an extra bracket or not. That and forgetting ; are currently my biggest timewasters. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.