Part 1
This lesson's goals
- Set up a MySQL database
- Check every message for incoming commands
- Update commands dynamically
Yes, that's quite a challenge. Unlike the previous tutorial, I'd call this an average tutorial. Not too hard, but not truly easy either. Full tutorial after the jump.
Good, so you decided to follow up from the previous tutorial and make it do what you want? The bot will be controlled through the IRC window itself by sending it different queries.
Let's start by laying out a work plan, things we want it to do:
- Add your own functions (!hai => Say something back; !slap => Slap someone, etc)
- Track user stats (Words per line, #of lines, #of words, avg. #of characters per word, etc.)
That's what we'll be covering this tutorial. We'll keep the functions and user data stored in a MySQL database.
Read more...
Popularity: 100% [?]
PHP, Tutorials
Ever wanted to make an upload script, but don't want the page to refresh? Try AJAX.
In this tutorial we'll use an iframe to simulate AJAX behaviour.
Firstly, we "might" want to create a simple upload form:
<p id="f1_upload_process">Loading...<img src="loader.gif" />
<p id="result">
<form action="admin/addpost.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
File:
<input name="myfile" type="file" />
<input type="submit" name="submitBtn" value="Upload" />
</form>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
The important part here is the enctype, always have the enctype specified if you're uploading files. The first paragraph is the little Loading... box that'll show up when we press the submit button. The second <p> "result" is to display the results (We've uploaded or We failed). And lastly the iframe that is actually submitting the data, specified through the target="upload_target" in the form. Also, the forms onsubmit triggers the startUpload(); function.
Now we want to add some Javascript functions:
Read more...
Popularity: 22% [?]
PHP, Tutorials
$string = preg_replace("/(http:\/\/)?([a-zA-Z0-9\-.]+\.[a-zA-Z0-9\-]+([\/]([a-zA-Z0-9_\/\-.?&%=+])*)*)/", '<a href="http://$2">$2</a>', $string);
That little gem right there'll take any length of text and turn the urls in it into hyperlinks. Cool or what? :-)
Popularity: 17% [?]
PHP, Web development
regex, regular expressions
Hey,
So, yeah, I've found out a new trick in PHP! The function I'm talking about is parse_str(). To quote the PHP page:
Description
void parse_str ( string $str [, array &$arr ] )
Which means it takes in arguments such as
a=1&b=2&c=3
and turns it into an array in which
array(3) {
["a"]=>
string(1) "1"
["b"]=>
string(1) "2"
["c"]=>
string(1) "3"
}
Note the ampersand (&) in front of the second variable, which is a variable pointer. This means that it takes in the address of the second variable and is able to edit the exact variable you passed to the function in real-time. So, if you pass an empty variable called $foo to it. The $foo variable will be an array with the results from the function. Why it doesnt just return an array is a mystery that'll remain unanswered, but I guess you could just call that PHP.
Read more...
Popularity: 7% [?]
PHP, Tutorials
After reviewing some of my code, I found out I had been doing quite a lot of stuff in deprecated or just plain bad coding. The methods / functions described are PHP examples only.
Ternary Operator
Huh what does that do?!
$a = ($b >= 0) ? "b is positive" : "b is negative";
The basic markup of this 'method' is as follows:
$variable = (expression) ? "value if true" : "value if not";
So the example above would set $a to "b is positive" if ($b ?= 0) and would set it to "b is negative" if $b < 0.
So that means it's shorthand for:
if ($b > 0)
$a = "b is positive";
else
$a = "b is negative";
Another way I've seen myself do something similar is explained by the following code:
$a = "b is negative";
if ($b >= 0) $a = "b is positive"
Comparison operators
Everybody knows about using a double equals sign to compare two values. But have you ever heard of the triple-=? The triple equals-sign is to compare the values AND the types of the variables, cough:
if ("1" == true) echo '1 == true'; // Outputs 1 == true
if ("1" === true) echo '1 === true'; // Not true
if ("1" === 1) echo '"1" === 1'; // Not true
I think you understand ;)
Popularity: 7% [?]
PHP, Tutorials
Recent comments!