Everyone knows backups are important, not just for restoring from a system failure, but also for fixing user/admin/developer mistakes. Here’s an efficient way to automatically backup files on your Linux/CentOS server using a spare Windows or Linux machine. You should already have a basic grasp of the Linux shell prompt for this guide.
(Read more…)
I love databases because I always feel there is a way for retrieving my data in any shape or form I desire. Trying to find the correct syntax can sometimes be frustrating, however. Here are two tricks that have helped me during those special times:
- Custom "ORDER BY"
It's easy to order by ASC or DESC, but what if we want a custom order returned? This is when the FIELD() function comes in handy.
-
SELECT * FROM table_name ORDER BY FIELD(field_name, 'Small','Medium','Large');
The disadvantage is that this will slow down your query. For maximum performance, store the field as an ENUM with the values defined in the correct order.
- Retrieving values from multiple rows as a single comma-delimited field
Scenario: We have multiple values spread out across rows a table, but need to retrieve them together in a single field without performing multiple queries. I'm doing this for my archive page--I want to retrieve a list of topic tags for each article, but I want to do it with the same query that selects all my articles. GROUP_CONCAT() is the answer for string concatenation.
-
SELECT GROUP_CONCAT(wp_terms.slug) AS topic, p.post_date, p.post_title, p.post_count, p.post_name, p.comment_count
-
FROM wp_posts p
-
LEFT JOIN wp_term_relationships r ON (r.object_id = p.ID)
-
LEFT JOIN wp_term_taxonomy t ON (r.term_taxonomy_id = t.term_id)
-
LEFT JOIN wp_terms ON (wp_terms.term_id = t.term_id)
-
WHERE t.count> 0 AND t.taxonomy = 'category' AND p.post_status = 'publish'
-
GROUP BY p.ID
-
ORDER BY post_date DESC
The "topic" column could then return values such as "php,mysql,css".
What if you need to grab a string from text, such as the name of the department below:
Position #: 08-26
Position: Lab Assistant II
Department: Black Mesa Research Facility
Status: Full-time
Here's a quick way to pick out that string:
-
-
$deptName =
trim($matches[0][1]);
There may be a time when a visitor wants to print a page from your website. It's worth spending five minutes to create "printer-friendly" pages using CSS. (Tip: navigate to "Print Preview" on your browser to see how this page looks when printed out.) Some of you may already know about the <link rel="stylesheet" type="text/css" href="print.css" media="print" /> directive, however, it's better to place your print CSS within your main CSS file. Instead of having a separate CSS file for printing (which can increase the initial load time since the browser must make an additional HTTP request to the server before rendering the page), place @media print {... } at the bottom of your CSS file. It must be at the bottom to override your previous declarations.
(Read more...)
How do you write your HTML/CSS? Do you use unordered lists for links? Lots of whitespace in your CSS file? Don't know when to use an id selector or a class selector? Learn how and why to change your coding style.
(Read more...)