Jump to content

mac_gabe

Members
  • Posts

    58
  • Joined

  • Last visited

Everything posted by mac_gabe

  1. It's really hard to see where those extra blank lines are coming from! This is a sample of the first few lines from my complete list: ( [0] => Acanthisittidae [1] => Acanthizidae [2] => Accipitridae [3] => Acrocephalidae [4] => Aegithalidae and here is a sample from the partial list ( [0] => Accipitridae [1] => Acrocephalidae [2] => Aegithalidae [3] => Alaudidae [4] => Alcedinidae Both as viewed in Firefox 5 -> view source Any ideas what could be creating that extra line, or how I can see what's really going on?
  2. Well that's weird, using $difference = array_diff($complete, $partial); sort($difference); echo "<pre>";print_r($difference);echo "</pre>"; I get the full complete list, double spaced and if I switch it round to $difference = array_diff($partial, $complete); I get the partial list, single spaced Either way, no matches, and yet the two lists look like they have the same items. Must be formatted different somehow. Will delve deeper…
  3. Thank you both - I'm going to use your methods to check to see what's matching, which may help me to see what's going wrong. BRB.
  4. Hi, I'm trying to use in_array to match elements in 2 arrays, but I can't get it to work. To simplify, I have 2 arrays, each contain a list (in fact the lists have about 250 items) $complete - good order $partial - bad order Horse Cat Dog Elephant Cat Dog Elephant I want to print the complete list, in the good order, but if the item is also in the partial list, to append a x (in the real example I'll actually be making html links with preg_replace instead of appending x) So, final product: Horse Dog x Cat x Elephant x I have this: $y= count($complete) -1; for ($u=1; $u < $y; $u++) { if (in_array ($complete[$u], $partial)) { print "<li>".$complete[$u].' x</li>'; } else { print "<li>".$complete[$u]."</li>"; } But it seems to print the complete list with no changes - i.e. it's finding no matches. Thanks for any help - if there's a better method than in_array(), I'd be happy to use it.
  5. Sure, I would be greatful for the help, and would love to learn how to do this. I did learn quite a bit over the last few days thanks to your help and links - even managed to insert extra divs etc (not really necessary) the proper DOM way with append etc - but I had to cut my losses as I could see no end in sight. Meanwhile I've sorted out a PHP script which does all the finding and replacing - then I go back to Javascript (JQuery) to open the menus. But I can see that in theory pure Javascript would be a more elegant way of manipulating the elements.
  6. I'm giving up on this. I've spent all day looking at online manuals and trying to decipher examples but I'm still light years away. This is as far as I got: <script type="text/javascript"> window.onload = function() { var oldHTML = document.getElementById('blog-categories').innerHTML; var newHTML1 = '<h2 id="header_builders">BUILDERS</h2><div id="cat_builders"></div>'; var newHTML2 = '<h2 id="header_events">EVENTS</h2><div id="cat_events"></div>'; var newHTML3 = '<h2 id="header_other">OTHER</h2><div id="cat_other"></div>'; var newHTML4 = '<h2 id="header_all">ALL</h2><div id="cat_all">'+ oldHTML + '</div>'; var newHTML5 = newHTML1 + newHTML2 + newHTML3 + newHTML4; document.getElementById('blog-categories').innerHTML = newHTML5; var x01 = document.getElementById('cat_all'); } </script> It looks like I would have to make arrays, pull out the relevant child elements with an if clause and stick them back in the divs, but that's pretty complex stuff, and way over my head. Since I've already worked out a solution using PHP by manpulating the text, I'll just stick to that method.
  7. OK, I'm guessing it's the find and replace terms that are breaking everything. Because I've tried with a simple word replace (e.g. changing "Unknown" for "Other") and that seems to work OK. I suppose you can't add div elements with find and replace because then it breaks the DOM tree or something. I'll have a go with the children method … (gulp)
  8. OK I've got the first part of html in OK. This works: <script type="text/javascript"> // this adds an inner div named cat1 with FRUIT header window.onload = function() { var oldHTML = document.getElementById('blog-categories').innerHTML; var newHTML = '<div class="subcategory_title">FRUIT</div><div id="cat1">' + oldHTML + "</div>"; document.getElementById('blog-categories').innerHTML = newHTML; } </script> But, if I try to add a find and replace on the newly created "cat1" div, I seem to stop even the first part working. This doesn't work: <script type="text/javascript"> window.onload = function() { var oldHTML = document.getElementById('blog-categories').innerHTML; var newHTML = '<div class="subcategory_title">FRUIT</div><div id="cat1">' + oldHTML + "</div>"; document.getElementById('blog-categories').innerHTML = newHTML; // this should add a second div named "cat2", after the line with the "Unknown" category var oldcat1 = document.getElementById('cat1').innerHTML; var find1 = 'class="blog-category-link-enabled">Unknown</a><br />'; var replace1 = 'class="blog-category-link-enabled">Unknown</a><br /></div><div class="subcategory_title">FURNITURE</div><div id="cat2">'; var newcat = oldcat1.replace(find1, replace1) + "</div>"; document.getElementById('cat1').innerHTML = newcat; } </script> Any ideas where I may be going wrong. (I know I'm not using the children method, but I thought this might be easier for a beginner)
  9. Success! Many thanks MrAdam - I was trying to work out how to execute that code, but it turns out I didn't need to ! I've inserted a "Hello" already, like so: <script type="text/javascript"> window.onload = function() { var oldHTML = document.getElementById('blog-categories').innerHTML; var newHTML = "Hello" + oldHTML; document.getElementById('blog-categories').innerHTML = newHTML; } </script> so I'm pretty confident with a bit of perserverance I should be able to get the other html embedded too. Will report back later…
  10. Hi MrAdam. In real life what I'm trying to do is add subcategory headers to a list of blog categories in a blog sidebar. I can't modify the <div id="categories"></div> at the source, because it is generated and styled automatically by my blogging programme. It is exported by the programme as an html page (not seen directly by users), and then that page is placed on the user-visible index.php page (and all the other pages on the site) with a "php include". I can style this index.php page and also change the html template for it. I think I have worked out where I may be going wrong - I've now read somewhere the javascript has to come after the div has loaded - which seems to make sense - so I'm going to try that and see how I get on. I'll put your function in the head and then execute it at the bottom of the page. Thanks also for the links.
  11. Hi, I want to change a web page by nesting extra div tags inside a div which is contained in my html - so that I can format subsections. I don't want to do this with an onclick button, but as the page loads . I'm a beginner at javascript and am struggling with the syntax, so would really appreciate any help. This is an example of what I want to do. Change this: <div id="categories"> <a href="files/apple.php" class="link-enabled">Apple</a><br /> <a href="files/banana.php" class="link-enabled">Banana</a><br /> <a href="files/coconut.php" class="link-enabled">Coconut</a><br /> <a href="files/chair.php" class="link-enabled">Chair</a><br /> <a href="files/lamp.php" class="link-enabled">Lamp</a><br /> <a href="files/table.php" class="link-enabled">Table</a> </div> To this: <div id="categories"> <div class="subcategory_title">FRUIT</div> <div id="fruit"> <a href="files/apple.php" class="link-enabled">Apple</a><br /> <a href="files/banana.php" class="link-enabled">Banana</a><br /> <a href="files/coconut.php" class="link-enabled">Coconut</a><br /> </div> <div class="subcategory_title">FURNITURE</div> <div id="furniture"> <a href="files/chair.php" class="link-enabled">Chair</a><br /> <a href="files/lamp.php" class="link-enabled">Lamp</a><br /> <a href="files/table.php" class="link-enabled">Table</a> </div> </div> The actual list is much longer, with about 6 different subcategories, so I expect to be using the first and last items of each subcategory (ie Apple/Coconut and Chair/Table) to identify the start and end point of each find and replace. If someone can just show me the mechanism of find and replace and adding the new html to the document automatically, I can probably fill in the details. All the examples I've seen so far use buttons, or either change the tag or the text but not both. I also need to know where to place the code. Many thanks for any help.
  12. OK - no worries, it seems my web host has an automated interface for doing this, and there's probably no directory I can access anyway. I'll just try doing it through their interface.
  13. Hi, I'm looking for information on how to do a cron job or crontab. I.e. a web tutorial somewhere. I've never done one before so will need all the basics including where to put it on my host's server, how to check what's running on my web host etc, and of course what code to write. I'm planning to use it for a website to make a once-daily index of blog posts at an off-peak time (3 a.m.) The PHP script I'm going to run is a fairly simple preg replace on an xml page (which contains all the blog posts). The find and replace will find all the titles and links in the xml document and convert them into a long list of html links. I have written the PHP preg replace already, but I don't want to load it every time a visitor views a web page as the xml is a big file and it would bog down the website. I am at beginner level for most coding, though I'm OK at regex.
  14. I have a similar problem - I want to create an index from a large xml file (about 2 Mb). I have worked out how to use strreplace and pregreplace to achieve this, but the problem is the xml file will always take a very long time to load, no matter what method I use. So I was wondering (I am a beginner) if there is a way to do this on a website only intermittently - say once a day - and store the result, since the index doesn't need to be "in real time" So "document.xml" (2Mb - changes sometimes daily, sometimes weekly) -> somehow… -> "a_daily_index.html" (80 Kb) -> accessed by php include to "my_website_index_page.php" hundreds of times a day.
  15. Noob question. I've learned so much in the last couple of weeks that I've actually found two ways of doing the same thing! I was wondering which was the better method, if there is any difference. They are being used to print somewhere between 10 and 200 lines of text. Either: $categories_count= (count($categories[1])) -1; for ($u=0; $u < $categories_count; $u++) {echo "<li>".$categories[1][$u]."</li>";} Or: foreach ($categories[1] as $line => $cat) {echo "<li>".$cat."</li>";};
  16. OK I've done it with a for loop $original_array_count= count($original_array); for ($u=1; $u < $original_array_count; $u++) { $new_array[$u] = explode('<br />', $original_array[$u]); } print_r ($new_array); I thought maybe there was a simpler way of doing it but I guess this works fine.
  17. Hi, I'm trying to explode an array into a multidimensional array using <br /> tags as splitters but just can't get the syntax right! I can do lines of the array one by one, but not the whole lot at once. I'd really appreciate a quick example of an explode inside an array. $myarray looks something like this with print_r: ( [0] => [1] => some_html<br />some_html<br />some_html<br /> [2] => some_html<br />some_html<br />some_html<br /> [3] => some_html<br />some_html<br />some_html<br /> ) and I want to turn it into this: ( [0] => [1] => [0]some_html [1]some_html [2]some_html [2] => [0]some_html [1]some_html [2]some_html [3] => [0]some_html [1]some_html [2]some_html ) I've tried this $myarray = explode ("<br />", $myarray); but it doesn't work … It doesn't matter too much if there's are a few empty lines in the array from the final <br /> tags. Many thanks, as ever, for any help.
  18. 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.
  19. Argg - false alarm, it still doesn't work. My server had logged me off, so changes weren't being uploaded.
  20. 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.
  21. Indeed some of my links start with <a class="something" href="url.php" /> !! Thanks :-) Good point! Didn't need that :-) Thanks, that has really cleaned things up. I just tried this, but for some reason it seems to be inserting the curly brackets into the HTML?? Works OK in this instance without. Will investigate this and the "negative character class" which is also new for me. Thanks for all your help - I've learnt so much.
  22. Wow many thanks for the replies - I've just seen this (screwed up my notifications somehow so thought this post hadn't been replied to), so will now check to see if what you say explains it - have a feeling that it will!
  23. 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".
×
×
  • 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.