Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, October 2, 2012

Lazy mysql

I fucking hate MySQL.

is it just me or do the letters 'sql' take far too long to type? They are in complete opposite ends of the keyboard and chafe my ass. Add in 'my' and you now have a word that requires you visit all compass points of your keyboard to type it. What's worse, every mysql function in php has "mysql" at the start of it!

Because I am a lazy bastard I made a little function that eases my pain.

function fetch($str){
    $q = mysql_query($str);
    $a = mysql_fetch_assoc($q);
    if(!$a){
         echo 'e: Fetch() returned an error! '.mysql_error();
         return false;
    }
    else return $a;
}

Now I can just call fetch("SELECT * FROM `my_ass` WHERE `mysql` = 'shit'"); whenever I require an array from a mysql string. I don't have to type "mysql" once!

Seriously, try typing mysql ten times fast.

Sunday, July 31, 2011

toggleSlide() and while loops.

I came across a problem tonight

That I thought I'd share a solution to. I was mysql_fetch_array()ing in a while loop to output a bunch of data in seperate divs, and since it was getting bulky and cluttered I decided to add a toggleSlide() so I could collapse them individually.

So I decided to supply an argument in the form of the entry id returned by the loop. I appended the id to the end of the divs in the loop and to the argument of my slide() function using a PHP echo. And this is where I hit a snag.

When you give toggleSlide() an argument and put it in the onclick selector, it accumulates your clicks. This must have something to do with the + sign you need to use when you add the argument. What this means is that

Function slide(arg) {
$('document').ready(function() {
$('#button'+arg).onClick(function() {
$('#slider'+arg).toggleSlide();
});
});

Will have an odd and unexpected effect. The first click won't have an effect at all, the second click will slide up and then back down, the thid click will move three times and so on. The trick here is to add an unbind() command to the button element. So

Function slide(arg) {
$('document').ready(function() {
$('#button'+arg).onClick(function() {
$('#slider'+arg).toggleSlide();
$('#button').unbind('click');
});
});

Will have your loop returned divs sliding around nicely!

Just to clarify, the div returned by your loop would look like this:

<div id="button<?php echo $id; ?>" onclick="slide(<?php echo $id ?>)"><img src="someimage.png" /></div>
<div id="slider<?php echo $id; ?>">Some content</div>

Look at me, gettin all tutorialicious and junk.