Apr
30
2009
0

PHP’s function Code Highlighting

Just recently i discovered something very usefull. It could be very helpfull in any website or forum regarding programming.

There is a function in PHP called Code Highlighting. This would result in a very easy way of displaying php code onto a website. Just let me give an example.

<?php
highlight_string('<?php echo "test"; ?>');
?>

This outputted this in the displayed HTML:

<span style=”color: rgb(0, 0, 187);”>&lt;?php&nbsp;</span><span style=”color: rgb(0, 119, 0);”>echo&nbsp;</span><span style=”color: rgb(221, 0, 0);”>”test”</span><span style=”color: rgb(0, 119, 0);”>;&nbsp;</span><span style=”color: rgb(0, 0, 187);”>?&gt;</span>
</span>

Which looks just like this:
<?php echo “test”?>

As you can see, this can be very helpful.

Of course you need to be careful with this function as soon as you use it in “public”. You need to make sure you escape every quotes in the string with addslashes. This will prevent anything from happening.
Of course i can’t guarantee 100% protection. That’s pretty tricky. Good luck!
PS: http://nl2.php.net/manual/en/function.highlight-file.php

Written by Rene Pot in: Sugestions, general | Tags: , , , , , ,
Apr
27
2009
0

PHP 5 Recipes: A Problem-Solution Approach

Recently I bought this PHP book, and i must say, it’s a good one! There is more than just some examples. They show you how to fix a problem the best way! It is really worth buying.

Just want to recommend a good book!

PHP 5 Recipes: A Problem-Solution Approach


Apr
24
2009
0

Mysql Full Text Search - Speed up those results

I just ran into the problem of a very big database, and a very slow search function..

At first i was a little confused, but soon i came to the conclusion of building a Full Text Search function.

The first problem on building this i encountered was the database coalition. You have to have a MyISAM database. So, that’s what i did.

  • Create a table:

CREATE TABLE news (content TEXT, title VARCHAR(250), id INT(11) NOT NULL auto_increment, PRIMARY KEY(id);

  • Add a fulltext index:

ALTER TABLE articles ADD FULLTEXT(content, title);

This FULLTEXT index makes an index on both content and title, so you can’t search just one of them.

  • Create a Query:
$query = "SELECT title,content,id, MATCH(title,content) AGAINST ('my search') AS score FROM news WHERE MATCH(title,content) AGAINST ('my search')";

Let me explain:

SELECT MATCH(title,content) AGAINST (’my search’) means you select the result of a MATCH. The result is a score. The higher the number, the better the result.

AGAINST (’my search’) searches in the selected fields in the MATCH (title,content) in my example. The search can be multiple words. The search searches for all words individually. You can also add a minus (-) and a plus (+) to a word, in order to change the priority. A minus tries searching a row without the word, a plus makes a word more imporant.

The reason the MATCH AGAINST is double in my query is just because i want to order my results (2nd time) and i want the score visible (1st time). I rename my MATCH AGAINST as Score, in order to get the score back out of the query. This would be something you could show on your page. Or do something with in your code.

If you want to limit the result by score, you have to add the following to the query:

HAVING score > 2 (you can change the number in anything you want, but this will filter the worst results)

Mar
02
2009
0

New function url_exists()

We all know the function file_exists() to check if a file exists on the local server.
But, if you want to check if an url is correct, this isn’t possible with this function.

Therefor i searched the web, and found this on php.net:

        function url_exists($url) {
            $hdrs = @get_headers($url);
            return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
        }

Didn’t think it would be that easy! Good luck with it ;)

Written by Rene Pot in: PHP Problems, Sugestions | Tags: , , , ,
Dec
05
2008
0

Custom new function mkfile()

Since i needed to write a script that would make a file if it doesn’t excists, i thought mkfile() would be a perfect name for it.

The only thing mkfile does is check if a file excists on the server, and if it doesn’t it would create it, and chmod it. 

Let me show you the code:

<?php 
    function mkfile($filename,$mode) { 
        if (!file_exists($filename)) { 
             $handle = fopen($filename,'w+'); 
             fclose($handle);
             chmod($filename,$mode); 
        } 
    } 
?>

This code simply creates a function that can easily be used by calling it this way:

<?php 
    mkfile('pages/myfile.php',0777); 
?>

Just simply insert the filename and the rights (mode) and you can create a file with your own function.
You can also add your own features to this function, but in my opinion it is already complete.

Note: Don’t forget to check the input in the mkfile() function. If you let users create files, you should at least know they wont enter scripts in it! But i made this function only for you’re own use!

Written by Rene Pot in: Sugestions | Tags: , , ,
Oct
14
2008
3

MySQL issue with character set

Today i stumbled upon an issue with inserting text into a database, and the character set wasn’t taking it. No mather what i changed in the database, it was inserted wrong time after time.

Characters like â and é are shown wrong. In the variable it is correct, but in database it isn’t.

This is what i had:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

But when i looked into the database it wasn’t correct. Wrong encoding. The tabel was set UTF-8 so that couldn’t be wrong.

This was my solution:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" SET CHARACTER SET utf8 ");
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

Now it was correct inserted, so i could use it properly.

Written by Rene Pot in: PHP Problems, Sugestions | Tags: , , , , ,
Sep
27
2008
0

Smarty — Good Template engine for PHP

Just when i wrote my previous post, I needed it myself already. Had to install ezSQL onto my new project.
When installing this i figured that it would be nice to use a template engine. Since i have experience with Smarty I downloaded it and installed it also to my new project.

What is Smarty?

Smarty is a very easy to use template engine. I’ll show you some examples below:

// At regular sites you would use this code:
<?php
 
$row = array("1","2","3","4");
 
foreach ($row as $newrow) {
   echo('this is no. '.$newrow);
}
?>
//in Smarty you would write this: 
// At index file
<?php
$smarty->assign('row',array("1","2","3","4"));
?>
//At template file:
 
{foreach from=$row item=item}
    This is no. {$item}
{/foreach}

It looks like a lot of extra work, but you can use ths array everywhere. Also Smarty forces you to use functions. And with the assign key you can enter a function in it.

This makes Smarty very usefull for OOP applications. And always easy to extend without a lot of nonsense.

I recon you don’t know a lot about this Smarty. But I’ll show you new examples in the near future.

Information and download at www.smarty.net

Written by Rene Pot in: Sugestions | Tags: , , , , ,
Sep
26
2008
1

Work with function ezSQL

Since a couple of weeks i work with ezSQL. And i came to the conclusion this function is much more usefull than the normal mySQL tags. It is very easy in ezSQL to get 1 row, or multiple. And the output is in a simple array!

I’ll just show you with some code below.

<?php
 
// First connect to database
 
$db = new ezSQL_mysql(DB_USER,DB_PASSWORD,DB_NAME,DB_HOST); 
 
// Get items from database
 
$playlists = $db->get_results("SELECT * FROM playlist ORDER BY id DESC");
 
// And now just print it.
 
foreach ($playlists as $item) {
    echo('Name is: '. $item->name .'.');
}
 
?>

As you can see it works very easy with a database. Besides get_results ezSQL has some other functions.

<?php
 
$row = $db->get_row(" SELECT * FROM playlist WHERE id=5"); // Just get 1 row
 
$var = $db->get_var(" SELECT name FROM playlist WHERE id=5"); // Just get 1 variable
 
?>

Ofcourse there are many other features in ezSQL. I would say try it for yourself! Go get it at http://www.woyano.com/jv/ezsql

Thanks for reading this post! I hope it has been of good use.

Examples and documentation can be found Here

Written by Rene Pot in: Sugestions | Tags: , , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes