PHP Archives

Using MooTools ScrollSpy to Load More Items via JSON/AJAX

Published by David Walsh on Tuesday, February 9, 201013 Comments
MooTools ScrollSpy Load More

Last July I wrote an epic dominant unbelievable fantastic outstanding awesome NetTuts post called Create a Twitter-Like “Load More” Widget Using CSS, HTML, JSON, and jQuery or MooTools where I used a MooTools/Ajax/JSON system for loading additional items when the user clicks a “Load More” link. The functionality is sweet but I see room for improvement. If a user scrolls down toward the end of the container, why not load more items for them automatically? Armed with the premier MooTools scrolling plugin, ScrollSpy, we can do so.

ID Your Body Using PHP

Published by David Walsh on Wednesday, February 3, 201023 Comments

It seems like every part of a website needs to be dynamic. Dynamic, database-driven pages, dynamic javascript, dynamic CSS, etc. All this dynamism can make it difficult to style or script a page easily. One way I’ve learned to combat the rigidness of using CMS’ is to give the BODY element a unique ID based on the URL. Doing so allows me to add special styling to any page.

Replace Content in PRE Tags with HTML Entities

Published by David Walsh on Thursday, January 28, 20109 Comments

If you have a website that relies heavily on PRE tags, you know the important of converting PRE tag content to their HTML entities. Doing so prevents worlds of possible rendering issues. The following PHP snippet HTML-Entitizes (?) any code within PRE tags:

//replaces pre content with html entities
function pre_entities($matches) {
	return str_replace($matches[1],htmlentities($matches[1]),$matches[0]);
}
//to html entities;  assume content is in the "content" variable
$content = preg_replace_callback('/<pre.*?>(.*?)<\/pre>/imsu',pre_entities, $content);

I use this for just about every post I write. You could also stick this in your CMS if you have clients that may have use for PRE tags.

PHP Function: Remove a Query String Key=>Value

Published by David Walsh on Friday, January 15, 201013 Comments

I was recently coding a website which relied heavily on query string variables. It was absolutely imperative that I keep the query string clean and not duplicate keys. I needed to find a PHP function that would allow me to remove keys (and their values) easily and reliably. Enter a PHP function I found at Added Bytes:

/* the function */
function remove_querystring_var($url, $key) { 
	$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); 
	$url = substr($url, 0, -1); 
	return $url; 
}

/* usage */
//pretending this page is 

All I needed to do was provide the URL (in my case I wanted the current page’s URI) and the key to remove — so simple!

Using the GitHub API and PHP to Get Repository Information

Published by David Walsh on Wednesday, December 30, 20097 Comments

GitHub is an awesome place to host your open source project code. MooTools, Prototype, and jQuery all use GitHub. As you probably know, the MooTools Forge requires your plugins be hosted on GitHub. The only problem with hosting all my MooTools plugins is that I lose traffic when I want people to see my code. Problem solved: use PHP, the GitHub API, and PHP Markdown to display files of my choice on my website.

Our goals with this code will be to:

  • Connect to GitHub via the API to retrieve repository information.




© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.