Others
WordPress: MySQL Random posts
0While working on my webslice, I wanted to add some content to let the users know what’s happening on the website. So I was thinking to provide some random posts from my blog. Most of us know how to use the WordPress default methods to get the posts, to ask for random ones, but how to do it outside the warm WordPress environment?
I started digging in MySQL help (soon I should become a guru
) and find out that it is quite simple if all you want to do is to return some random rows from a table, let’s say a collection of random posts in your blog. Easily, MySQL’s base functions like ORDER BY RAND() come to the rescue. I found lots of examples, so I asume that everybody’s doing it. At least on my search on Google, all the PHP programmers did it:
SELECT * FROM wp_posts ORDER BY RAND() LIMIT 3
I set the limit to 3 because I don’t want to bother my users with all my posts, three are more than enough.
So I did it. But wait, there’s a catch! It really does what it’s supposed to. But what will happen when you reach high levels of users? Your webserver probably will suffer a lot from this. OK, it is not the case in articles,
Your social network keeps growing and growing and after about a year and 50000 new users you realize a slow-down on the page where you show random users. You’re thinking of caching it, but what’s the point? It wouldn’t be random. You break it down with EXPLAIN and realize with horror that your fancy query doesn’t use the nice index you placed on the table ages ago. And why would it? It’s calling RAND() for every row in your table, there’s just no way around it. So what to do? One alternative is to fetch random IDs first and then join the IDs found with the USERS table to fetch the real data. How do we do that? Why, using ORDER BY RAND(), of course. Wait, didn’t I just say you’re not supposed to use it? Well, I did say that, but the difference is that we’ll run the ORDER BY RAND() on the best-indexed column there is, the primary key. Let’s break it down and get our hands dirty:
SELECT USERS.* FROM (SELECT ID FROM USERS WHERE IS_ACTIVE = 1 ORDER BY RAND() LIMIT 20) AS RANDOM_USERS JOIN USERS ON USERS.ID = RANDOM_USERS.ID
And with a little bit of thinking we got ourselves a nice and fast way to fetch random data. Most likely there are other ways out there (sometimes I do miss Oracle’s ROWID), but this one worked fairly well for me. It probably won’t scale forever though, so be prepared to get back to it every once in a while.
How to add a Webslice
1
It is very nice and user-friendly to have Webslices on your website. It improves the user experience by allowing visitors to be up to date with the latest content that you provide, assuming that it is fitted to this presentation style. Since my BNR exchange rates page was this kind, I pursued in adding a Webslice especially for it.
Adding a webslice to your website is simple. After you read the main specifications or my previous article, you’ll understand the concept behind: first you signal to the browser that you have a special part of your website by setting to a container element the class “hslice”:
<div id="BNRExchange" class="hslice">
<div style="display: none" class="entry-title">
<img id="image" alt="Radu Poenaru's weblog gives you BNR Exchange rates" src="http://www.radupoenaru.com/bnr/images/radupoenaru.jpg" width="128" height="128" />
<p>
Cursul oficial BNR
</p>
</div>
<p>
<a style="display: none" href="http://www.radupoenaru.com/bnr/bnr.php" rel="entry-content">
<a style="display: none" href="http://www.radupoenaru.com/bnr/bnr.php" rel="Bookmark" target="_blank">
<span style="display: none" class="ttl">5</span>
</p>
<div id="updateTrigger" class="entry-content" runat="server" />
........ your content that you want surrounded by the green webslice part ..............
</div>
WordPress – Database backup and restore
0
From time to time, you’ll need to get a backup of your MySQL database and install it on a different machine – as I needed when reinstalling my Windows 7. I wanted to do something simple, as the last time. But now the database sql file is 11 Mb as opposed to merely 2 MB last time.
But I thought that this would be a breeze for the MySQL interface that I use and enjoy – HeidiSQL. It’s free, easy to use and user friendly. I personally don’t like too much the phpMyAdmin interface from the Vertrigo webserver installation that I use for local development.
Add Webslice to your website
1
You might ask: what is a Webslice? It is a chunk of information, allowing you to stay up-to-date with the latest news without actually needing open a website. They are added to special toolbars (special because they usually require a specific version of a browser, like IE 8). But otherwise, the content inside itâ??s just plain old HTML. When a WebSlice is added to the toolbar, it will be represented as a clickable drop-down area, allowing you to click on it and then see a specially designed small-scale version of the website. For example, my website provides a webslice for BNR exchange rates , allowing you to keep up with latest information available.
Usually, while hovering to an area webslice-enabled, the browser will show a specific icon â?? developed by Microsoft and symbolizing this technology. There are few design guidelines that you need to follow, quoted from Microsoft website :
WordPress – How to Delete Existing Post Revisions
0
This is the second part of managing WordPress feature of post revisions, for the users who had disabled or turned off post revisions tracking or versions history feature. As you already know, this feature exists in WordPress (starting with WordPress 2.6). After disable it (of course, if you really need to do that), it also make sense to also delete and remove all existing stored post revisions. By doing so, you’ll remove changes made on pages stored in the database such that you’ll slim down the wp_posts table size´. This is especially recommended when there is already lots of revisions or changes inside.
WordPress – managing Post Revisions Tracking
1
A very interesting feature added in WordPress blog publishing system since version 2.6 is the post revisions tracking, like in version control systems. It simply adds a Wiki-like style tracking of edits that had been made to the posts or pages. This allows bloggers and authors to view, control and moderate – knowing who and when made what changes to any post or page. It also includes the ability to compare for differences between each saved versions, or revert back to older version.
SyntaxHighlighter – how to use it and brushes available
0![]()
The brushes used by SyntaxHighlighter are in separate syntax files named brushes. The following can be used to format the <pre class=”brush: brushname”> that you want to use.
Brushes () in alphabetical order:
| Brush name | Brush aliases | File name |
|---|---|---|
| ActionScript3 | as3, actionscript3 | shBrushAS3.js |
| Bash/shell | bash, shell | shBrushBash.js |
| C# | c-sharp, csharp | shBrushCSharp.js |
| C++ | cpp, c | shBrushCpp.js |
| CSS | css | shBrushCss.js |
WordPress- how to implement Syntax Highlighting
1
Some while ago, I started publishing code on this website. On that days I used the free WordPress webhosting. But I wasn’t content with the representation of it. After some time, I moved my site to a payed hosting, where I could implement any plugin that I liked.
URL shortening too long? j.mp is the answer
0First of all, what’ an URL Shortening service? URL shortening is a technique used very much lately on the World Wide Web allowing people to create a very short URL and make a web page available on it, in addition to the original address. For example, the page http://www.radupoenaru.com/handle-xml-with-namespaces/ has a length of 54 characters and resulted in the following TinyURL which has a length of 25 characters: http://tinyurl.com/mpjxqb .Half the size, same page pointing to!
But hey! Twitter allows only 140 characters – Need to save two characters in a tweet? You either have to use the SMS language – rework your wording by changing “people” to “ppl” or “for” to “4” for example, or, if you want to include a link, you can use a shorter URL. The natural evolution of shortening websites also followed the shrinking idea : Starting with tinyurl.com who had on its own 11 characters, going to bit.ly 6 characters( only half!!!) and finishing with the newest addition, making URLs look like they were getting just a little too long, when Bit.ly introduced j.mp –only 4 characters wasted.
cPanel: using Cron Jobs
0
First – what is a cron job? It is a piece of software which can be run at specific times by the cron service. By example, one could set a cron job to update his application exchange rates from a server every day at a specific hour so its application will be up to date.
I used it because the Romanian National Bank doesn’t have a proper page to check for exchange rates – they present just the numbers, but no trends, no charts – nothing visual. Too bad for them! For me it is actually a good way to refresh my PHP / MySQL and apache management skills. Because this Cron stuff is actually new for me, I composed a small tutorial in order to be easy for me to remember and for you, my dear readers, to get quickly the information required for a quick start. So let’s get going with our ..