NachoLabs.includable es?

Grab it!
Last updated on 1.ii.2010

Ok, so what is it again?

It's a bunch of functions, variables, whatevers that I use in all my projects and will probably save you some troubles when it comes to PHP coding. Mind you, this will be constantly updated, since new opportunities of expanding/fixing show up rather often.

How do I get it working?

Easy. Download inc.txt and rename it as inc.php. Then, just insert the following line at the top of each of your pages.

<?php include_once 'inc.php'; ?>
This is, of course, assuming you name the file inc.php and that it's located in the same folder.

What's in it?

Let's break it down, shall we?

// MySql vars
$inchost = 'localhost'; // MySQL host address
$incuser = 'root'; // MySQL username
$incpass = ''; // MySQL password
$incname = ''; // MySQL database name
If you're using MySql in your project, you want to adjust the connection settings here. Inc.php will automatically connect to MySql and wait for your queries.
@session_start();
This beautiful line makes sure that a session is always open. You can read more about it here, but the bottom line is that you can store variables along pages, and these will be always available via $_SESSION, until the user closes the browser.
// Sanitize $_post
foreach($_POST as $post) mysql_real_escape_string($_POST[$post]);
Yummy. Now you won't ever have to worry over SQL injection. Any data sent through POST will be escaped. This could be done to GET also, but usually the variables that rely on GET are less sensitive, and chances are escaping them will cause trouble.
// Fetching and dumping in multiarray
function fetch($q=false,$row=false,$field=false) {
Ah, so we get to the really juicy one. fetch() will execute a MySQL query for you, and then put the results in a multiarray. Let's see an example:
$r = fetch('SELECT name, email FROM users LIMIT 1;',0);
echo 'First user is called ' . $r['name'] . ' (' . $r[1] . ').';
So in other words, fetch() gives you an array which contains arrays, each of one is filled with data from one record (indexed both by number of row and by name of row). Alternatively, you may pass a second and third argument if you wish to recieve only an array with a given column or only the value of a given column and row. Also, if you don't pass any argument, not even the query, fetch() will return the last ID inserted.
// Querying without fetching
function query($q) {
Instead, if you need to send information rather than recovering it, you should use query(). Like this:
$s = query('DELETE FROM users WHERE id = 1;');
if($s) {
     echo 'Success!';
}
query() will return true on success and false on fail. As simple as that. Let's move on.
// Upload & resize
function upload($datos, $ruta, $ancho, $alto, $proporcion=true, $recortar=true) {
BEHOLD! I have written the ultimate image uploading and resizing routine. Well, probably not. Let's examine the parameters.
// Reading dirs
function listdir($ruta='.', $ord='date', $path=false ,$omitir=array('.','..'), $full=false) {
This is the last function to the date. listdir() will read a directory for you, and tell you what's in it.

Conclusion

Take it. Use it. Pass it along. Improve it. I don't mind. If you come across a bug, a security issue, or an addendum you'd like to report, I'd be pleased to hear from you. My e-mail adress is right at the top of inc.php.