Automatic Checkbox Save w/ jQuery
Posted on Fri 14th May 2010 · Development · 3 Comments
While making stuff I always try to put in little short cuts to make things go faster, whether its browsing though pages or submitting forms, everything needs sped up.
While making the back-end of a discussion form, I used this to save settings without having to submit a form. It just saves everything onclick :D
PHP/HTML/JS
## CONNECT TO DATABASE
$connection = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename", $connection);
## GET CURRENT SETTINGS
$is_q = mysql_query("SELECT * FROM `settings` WHERE `id` = '1'");
$is = mysql_fetch_array($is_q);
class="ajax" />
Refresh the page to see that its still checked/unchecked
save.php
## CONNECT TO DATABASE
$connection = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename", $connection);
if($_GET['checked'] == "true") {
mysql_query("UPDATE `settings` SET `checkbox` = '1' WHERE `id` = '1'");
} else {
mysql_query("UPDATE `settings` SET `checkbox` = '0' WHERE `id` = '1'");
}
The JS
$(document).ready(function(){
$('input#crazycheckers').click(function() {
$.get("save.php", { checked: $(this).attr("checked") },
function(data){
});
});
});
Simples :)
A little help from The Shaun on this aswell!
Demo
Posted on Thu 13th May 2010 · Development · 10 Comments
I love sharing code & reading other peoples code to grasp a better understanding of how things can be done in different ways. I also like to read other peoples codes to pick-up on the functions they use to get things done.
Heres a list of my favourite, starting with a new addition to my fav list!
end() - Set the internal pointer of an array to its last element.
$ext = $_FILE['name'];
if(end(explode(".", $ext)) == 'jpg') {
// DO THIS
} else {
// DO THIS
}
unset() - Unset a variable - Good for after forms are submitted, stops people from submitting twice if you have the $_POST['name'] echo'd in the field.
if($_POST) {
// BLAH
unset($_POST['name']);
unset($_POST['message']);
}
sort() - Sorts the elements of an array in ascending order.
sort($arr); // a // b // j // l
strtolower() - Change entire string to lower case.
$str = 'HELLO WORLD'; echo strtolower($str); // hello world
str_replace() - My all time fav and most used :D
$str = 'Hello little cat!';
echo str_replace('cat!', 'dog!, $str); // Hello little dog!
wordwrap() - Wrap the string to a given number of characters.
$text = "I am sooo cool!"; echo wordwrap($text, 8, "\n", true); // I am so
oo cool!
Thats all i can remember for now, please share your favourites :)
Posted on Wed 12th May 2010 · Video · 1 Comments
This popped up on just about every social bookmarking / news / network site that you can think of today. I wanted to air my thoughts on it.
I wish the music industry would take a far more serious look at websites like YouTube, pruning the utter rubbish and ending with people like this. I hold a grudge paying £12.99 (or whatever it is these days) for albums or even their lesser expensive counter-parts of singles, the main reason being is that what I’m buying isn't a true reflection of the music that is out at that point in time, it’s just music packaged up - which will sell - and sent off to the shops to make its impact (or not) in the charts.
Finding music like this though gives me hope, I’d be quite happy to pay for the flight to his home town and hand over my £12.99 for him to play me 1 song. This boy is epic.
Youtube: Grayson97s Youtube Channel
Setting CSS rules on Wordpress parent elements
Posted on Wed 12th May 2010 · Development · 5 Comments
One of the first problems I was faced with when using Wordpress & WooThemes was the fact that you cannot highlight the parent item in the navigation - while on a child page.
Alot of designs use breadcrumbs or change the background colour of an elemt to highlight what page you are on, with Wordpress & WooThemes, this isnt built in. Heres how to solve it:
functions/admin-custom-nav.php
Around line 1015, you'll see // GET MENU ITEMS - after the backend part, paste in this:
$parentId = null;
if($wp_query->post->ID) {
$parent_res = $wpdb->get_results("SELECT post_parent from wp_posts where id = ".$wp_query->post->ID);
$ parentId = $parent_res[0]->post_parent;
}
And then further down that page, around line 1160 - Youll see // FRONTEND LINKS in the link anchor, have something like this:
if($woo_custom_nav_menu_items->post_id == $parentId) { echo 'id="active"'; }
Then all you need to do is set the css rules on #active and your parent elements are styled while on their child pages.
This is thanks to @enru aswell :]