While using SalesForce API to upsert data to SalesForce I’ve got an error “The entity “Atilde” was referenced, but not declared.”. I found out that the problem causing this error was htmlentities php function. htmlentities converts all applicable characters to HTML entities. html_entity_decode php function solved the problem so don’t use htmlentities, try to change this function with htmlspecialchars if you want to sanitize html for salesforce.
PHP recursive menu with 1 query
There are a lot of methods how to fetch menu with submenus. But I will show you how to get all menu with submenu data from database in one query and make recursive display with PHP.
Table “menu” schema should look like this:
| id | parent_id | title |
|---|---|---|
| 73 | 0 | MENU_73 |
| 74 | 73 | MENU_73_74 |
| … | ||
When we fill the database with menu/submenu records we can get all records by query:
$query = mysql_query("SELECT * FROM menu");
After printing query results we should see:
Array
(
[0] => Array
(
[id] => 73
[parent_id] => 0
[title] => MENU_73
)
[1] => Array
(
[id] => 74
[parent_id] => 73
[title] => MENU_73_74
)
[2] => Array
(
[id] => 75
[parent_id] => 74
[title] => MENU_73_74_75
)
[3] => Array
(
[id] => 76
[parent_id] => 74
[title] => MENU_73_74_76
)
[4] => Array
(
[id] => 77
[parent_id] => 74
[title] => MENU_73_74_77
)
[5] => Array
(
[id] => 78
[parent_id] => 73
[title] => MENU_73_78
)
[6] => Array
(
[id] => 79
[parent_id] => 78
[title] => MENU_73_78_79
)
[7] => Array
(
[id] => 80
[parent_id] => 73
[title] => MENU_73_80
)
[8] => Array
(
[id] => 84
[parent_id] => 80
[title] => MENU_73_80_84
)
[9] => Array
(
[id] => 85
[parent_id] => 80
[title] => MENU_73_80_85
)
[10] => Array
(
[id] => 81
[parent_id] => 0
[title] => MENU_81
)
[11] => Array
(
[id] => 82
[parent_id] => 81
[title] => MENU_81_82
)
[12] => Array
(
[id] => 83
[parent_id] => 81
[title] => MENU_81_83
)
)
We want to get those result with assigned childrens to parent, so use this function to do that:
//$tree - menu data array
//$parent - 0
function formatTree($tree, $parent){
$tree2 = array();
foreach($tree as $i => $item){
if($item['parent_id'] == $parent){
$tree2[$item['id']] = $item;
$tree2[$item['id']]['submenu'] = formatTree($tree, $item['id']);
}
}
return $tree2;
}
And the result of using recursive function:
Array
(
[73] => Array
(
[id] => 73
[parent_id] => 0
[title] => MENU_73
[submenu] => Array
(
[74] => Array
(
[id] => 74
[parent_id] => 73
[title] => MENU_73_74
[submenu] => Array
(
[75] => Array
(
[id] => 75
[parent_id] => 74
[title] => MENU_73_74_75
[submenu] => Array
(
)
)
[76] => Array
(
[id] => 76
[parent_id] => 74
[title] => MENU_73_74_76
[submenu] => Array
(
)
)
[77] => Array
(
[id] => 77
[parent_id] => 74
[title] => MENU_73_74_77
[submenu] => Array
(
)
)
)
)
[78] => Array
(
[id] => 78
[parent_id] => 73
[title] => MENU_73_78
[submenu] => Array
(
[79] => Array
(
[id] => 79
[parent_id] => 78
[title] => MENU_73_78_79
[submenu] => Array
(
)
)
)
)
[80] => Array
(
[id] => 80
[parent_id] => 73
[title] => MENU_73_80
[submenu] => Array
(
[84] => Array
(
[id] => 84
[parent_id] => 80
[title] => MENU_73_80_84
[submenu] => Array
(
)
)
[85] => Array
(
[id] => 85
[parent_id] => 80
[title] => MENU_73_80_85
[submenu] => Array
(
)
)
)
)
)
)
[81] => Array
(
[id] => 81
[parent_id] => 0
[title] => MENU_81
[submenu] => Array
(
[82] => Array
(
[id] => 82
[parent_id] => 81
[title] => MENU_81_82
[submenu] => Array
(
)
)
[83] => Array
(
[id] => 83
[parent_id] => 81
[title] => MENU_81_83
[submenu] => Array
(
)
)
)
)
)
Now it is easy to print this array to menu with submenu.
PHP tips and tricks
There are a lot of php tips or tricks that you may don’t know. So I want to share with you with tips that I know. Some of them can help a lot and some of them are to write less code. Here is a list of tips and tricks:
1. Did you know you can write a variable in a string with double quote and php will print it as variable?
$variable = 'ADD ME';
$text = "So here is $variable and some text";
echo $text; // output: So here is ADD ME and some text
//other method with brackets
$variable['add'] = 'ADD ME';
$text = "So here is {$variable['add']} and some text";
echo $text; // output: So here is ADD ME and some text
2. Did you know what is difference between quote (‘) and double quote (“) in php (look at $text)?
$variable = 'ADD ME'; $text = 'So here is $variable and some text'; echo $text; // output: So here is $variable and some text $variable = 'ADD ME'; $text = "So here is $variable and some text"; echo $text; // output: So here is ADD ME and some text
3. Did you know how to write shorter IF/ELSE ?
//original
if($hey){
echo 'hey';
}else{
echo 'bye';
}
//short method
echo ($hey)?'hey':'bye';
4. Did you know you can assign variable and check in IF statement at the same time with one = ?
if($ok = $this->getOk()){
echo $ok; //if $ok is not empty or false it will echo $this->getOk() value
}
5. Did you know you can create a function on the fly ( http://lt.php.net/create_function )?
$sumfunction = create_function('$a,$b', 'return $a + $b;');
echo $sumfunction(3, 2); //output: 5
6. Do you know that true === 1 is equal to false, because === is not the same as == ?
if(true === 1) //will result in false if(true == 1) //will result in true //because === will check type also
7. Do you know that @ near variable will suppress php warning messages ?
echo $variable; //will result in warning message, because $variable is not defined echo @$variable; //no warning message
8. Do you know that echo is faster than print and quote is faster than double quote ?
I will add some more tips and tricks later.
How to get youtube image/thumbnail
I’ve got a spare time of half a day and thought about making a website of funny videos for myself. I love my custom CMS, because all coding goes fast like saying to a computer do that, do that and voila! But this post is not about my CMS.
Most of videos will be posted from YouTube, so I thought why not to make YouTube video image/thumb uploading automatically, to save some time while adding content.
First of all I need to get YouTube video ID from URL like this http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=relate. Fastest and the best method in my opinion is:
$url = "http://www.youtube.com/watch?v=oHg5SJYRHA0&feature=relate"; parse_str( parse_url( $url, PHP_URL_QUERY ) ); echo $v; // this will output oHg5SJYRHA0
OK so now we have YouTube video ID. Each YouTube video has 4 generated thumbnails. You can find them at:
http://img.youtube.com/vi/videoID/0.jpg
http://img.youtube.com/vi/videoID/1.jpg
http://img.youtube.com/vi/videoID/2.jpg
http://img.youtube.com/vi/videoID/3.jpg
Just put video id that we got it before, to the place where videoID is. The URL with my example should look like http://img.youtube.com/vi/oHg5SJYRHA0/0.jpg.
First image is always default and biggest image.
encoding ASCII to 7bit strings and decoding
I am working now with Lithuanian mobile operators to implement Unstructured Supplementary Service Data (USSD). Messages must be encoded in utf7, so I would like to share encoding ASCII to 7bit strings and decoding 7bit to ASCII functions that my colleague helped to write.
/**
* encoding ASCII to 7bit strings
* @param string $text
* @return string
*/
function encode7bit($text){
$ret = '';
$data = str_split($text);
$mask = 0xFF;
$shift = 0;
$len = count($data);
for ($i = 0; $i < $len; $i++) {
$char = ord($data[$i]) & 0x7F; // only 7bits
$nextChar = ($i+1 < $len) ? (ord($data[$i+1]) & 0x7F) : 0; // only 7bits
if ($shift == 7) { $shift = 0; continue; }
$carry = ($nextChar & ((($mask << ($shift+1)) ^ 0xFF) & 0xFF));
$digit = (($carry << (7-$shift)) | ($char >> $shift) ) & 0xFF;
$ret .= chr($digit);
$shift++;
}
$str = unpack('H*', $ret);
return strtoupper($str[1]);
}
/**
* decoding 7bit strings to ASCII
* @param string $text
* @return string
*/
function decode7bit($text){
$ret = '';
$data = str_split(pack('H*', $text));
$mask = 0xFF;
$shift = 0;
$carry = 0;
foreach ($data as $char) {
if ($shift == 7) {
$ret .= chr($carry);
$carry = 0;
$shift = 0;
}
$a = ($mask >> ($shift+1)) & 0xFF;
$b = $a ^ 0xFF;
$digit = ($carry) | ((ord($char) & $a) << ($shift)) & 0xFF;
$carry = (ord($char) & $b) >> (7-$shift);
$ret .= chr($digit);
$shift++;
}
if ($carry) $ret .= chr($carry);
return $ret;
}
Again IE! session refresh
Again problems with IE (Internet Explorer). Grrr how I hate it. Can we count IE as browser? I never say it is browser, I just say that website works on browsers and IE… This time the problem with IE is about sessions. On IE refresh every time session_id changes. Few hours of headbanging to keyboard and I have found a solution.
There’s a bug in Internet explorer in which sessions do not work if the name of the server is not a valid name. For example…if your server is called web_server ( _ isn’t a valid character), if you call a page which uses sessions like http://web_server/example.php your sessions won’t work but sessions will work if you call the script like this
[IP NUMBER]/example.php
The problem was “_”.
Create a Fancy Search Box using CSS
Good technique to make a fancy search form.
jquery/php ColoXY game
About 2 years ago I’ve made my first game with jquery. Almost forgot about it. It was made in about 1 day. The game is very simple and have some similarities to cubic rubic (the magic cubic of rubic), just it is in 2D and have 5 planes. Because I couldn’t find a game like this, I’ve named it ColoXY. There was a better, nicer version of game with highscores, but after hard drive format I was unable to find a latest version.
The game is made with php/javascript/jquery/html/css. Graphics are made in photoshop. It is using client/server architecture, when backend making all calculations and client gets only data where to move, so there are no posibility to cheat on this game
Communication is done with ajax.
Instructions
To win this game, you need to mix the lines in order to make all 5 boxes the same color. Click arrows around the middle box with your mouse.
Where to find
You can find it here http://jugbit.com/jquery/coloxy/
Forgot to mention that I’ve made a flash version of this game also. You can find it on kongregate http://www.kongregate.com/games/kasp3r/coloxy
Where is source?
You can grab all source code by going to http://jugbit.com/jquery/coloxy/coloxy.zip
License?
No license or some other restrictions like always. Use it, modify it how you like, but at least it would be cool if you leave original author comment in source code.
How to invoke with red5
I’ve got an appointment to make a video chat game like charades that will be played in a browser. So yeah it will be made in flash and server side – red5. I’ve never worked with red5 before, so I had to surf all over the net to find things that I need, because red5 documentation is not one of those big and I always prefer to learn from examples. So I want to share with you my findings.
Invoke flash function from red5
IConnection current = Red5.getConnectionLocal();
IClient client = current.getClient();
Collection<Set<IConnection>> conCollection = current.getScope().getConnections();
for (Set<IConnection> cons : conCollection) {
for (IConnection con : cons) {
if (con != null) {
//you can check here to send invoke only for connection you want to. Now it sends to all connections in a scope
IServiceCapableConnection sc = (IServiceCapableConnection) con;
sc.invoke("yourFunctionInFlash", new Object[]{ argument1, argument2, andSoOn});
}
}
}
//in AS3 red5 will call this function
public function yourFunctionInFlash(argument1:String, argument2:String, andSoOn:String):void{ }
In flash (AS3) you can invoke red5 method by this:
//connection = new NetConnection();
connection.call("functionInRed5",new Responder(function(result:Object){}), param);
//flash will call this function in red5
public Room goToCreate(Object[] params) { }
There are shared objects, but it is used more for real time games, so I’ve chosen to use invoke
My top jquery plugins that I use a lot
Here is my list of jquery plugins that I used in a lot of my projects.
JForm (http://malsup.com/jquery/form/)
This is the most used plugin by me. It helps me so much to make ajax forms and it is so easy, that when I need to do some form I use this plugin. Also it can send a file!
Quote from site
The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. The main methods,
ajaxFormandajaxSubmit, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allows you to have full control over how the data is submitted. Submitting a form with AJAX doesn’t get any easier than this!
jGrowl (http://stanlemon.net/projects/jgrowl.html)
Another little plugin that helps to show errors or some usefull information to users with some cool animation.
Quote from site
Similar to the popular OS X system message framework called Growl, jGrowl provides a simple way for developers to raise unobtrusive messages to the end-user.
jquery flash (http://jquery.lukelutman.com/plugins/flash)
If you want that your site validates and don’t want to write a long html to embed flash, then you should use this plugin. It is easy as
$('#hello').flash({
src: 'hello.swf',
width: 320,
height: 240
});
qTip (http://craigsworks.com/projects/qtip/)
Every tooltip that I make is using this very customizable plugin for tooltips creation. You can change style too look as you want, change positions, directions and everything you imagine that tooltip can do.
Quote from site
qTip is an advanced tooltip plugin for the ever popular jQuery JavaScript framework.
Built from the ground up to be user friendly, yet feature rich, qTip provides you with tonnes of features like rounded corners and speech bubble tips
blockUI (http://malsup.com/jquery/block/)
When you are making ajax request and want that your visitor couldn’t click any links or submit button again and don’t interupt request this is a plugin you should get. You can add some loading bar or something like that to show that ajax request is in progress.
Quote from site
The jQuery BlockUI Plugin lets you simulate synchronous behavior when using AJAX, without locking the browser. When activated, it will prevent user activity with the page (or part of the page) until it is deactivated. BlockUI adds elements to the DOM to give it both the appearance and behavior of blocking user interaction.
jquery Cycle (http://malsup.com/jquery/cycle/)
And one more plugin from malsup.com site. It’s slideshow plugin that got amazing effects to cycle your images.
Quote from site
The jQuery Cycle Plugin is a slideshow plugin that supports many different types of transition effects. It supports pause-on-hover, auto-stop, auto-fit, before/after callbacks, click triggers and much more.
So these are plugins that I mainly use on my projects. There are much more that I didn’t mention in here, but I had to choose the best. You should check them out if you don’t already know them, because I am sure that it will help you to make your site more interactive to visitors.