Friday, April 29, 2011

Read and write JSON with PHP

JSON logo

A few days ago at work, my boss ordered me some programming tasks. I was very happy. As I told some time ago that was not programmed because I had been assigned other tasks. Taken at a junior and send it to run errands. So I will never move from junior  :)

But complaints aside, I'm to the point. It turns out that I was commissioned to make a PHP script that generates a json file to read it from javascript.

JSON

I like to say "jota son", but people usually say "yei son". Is a data representation format that encapsulates objects, arrays or variables in a special notation.

JSON is JavaScript Object Notation. And basically it, the way in which objects are represented in Javascript. Inside it may be from a single variable, an array of whole objects, etc.

JSON vs XML
XML vs JSON

XML is another way of representing data, but uses tags to format it like HTML. The advantage of JSON is basically two:
  • It's lighter than XML, because it does not add labels to add many bytes to content.
  • You can read almost directly in JavaScript and that makes it faster. In the case of XML is that process more and that makes it slower.
AJAX

In recent years Ajax has become fashionable. How you communicate with the server at first was the XML. But the burst JSON to be imposed for those benefits I mentioned earlier. So many communications are made in Json ajax, many web services now use only this format.

What they asked me

What they asked me to do, basically, is a kind of web service.

From a web page, using ajax, are requested data to a url (where hosts my PHP script). These data are used to build a table, which is not important at this time what is its purpose.

On the server a file is included in my php script that contains a variable with data. This variable is populated with data from the database at a certain period of time (that I am not concerned me, because it is already done.)

This variable is an object that contains an associative array with values, such as:

Ajax Team
$data = array(
  'dato1' => 1,
  'dato2' => 2,
  'dato3' => 3,
  'dato4' => 4,
  'dato5' => 5,
);

After searching the web for a while, and that some of my colleagues at work told me I had to include a library to do so, I found that it brings a PHP function to do this work:
  • json_encode, which returns a string in JSON format to the representation of what we as a parameter.
and also is its counterpart:
  • json_decode  to decode the json that we passed as a parameter and returns the variable, array or object it represents.
json_encode()

But in my case I just needed json_encode. So I just had to do this:
PHP Elephant

$json = json_encode($data);

and then simply print.

echo $json;

and it prints the following:

{"dato1":1,"dato2":2,"dato3":3,"dato4":4,"dato5":5}

JSON decoding

I did not need it, but if you send me the json to get the equivalent in PHP:

$json = '{"dato1":1,"dato2":2,"dato3":3,"dato4":4,"dato5":5}';
$data = json_decode($json);

And ready in $data as well as we had before. So easy.

Every so often you have to read the PHP manual

This feature is available from version 5.2. It seems that once used a library for JSON objects, or was done by hand, so my colleagues advised me that. But I searched the PHP manual and found these functions.

Sometimes you have to search in the php manual. Is a language that is constantly evolving and may be that what we had to do by hand, now is fixed in a single native function  :)

Hopefully it helps someone   :)

Diego Arregui

Wednesday, April 27, 2011

The Brujita Veron and yogurt consumption

The other day I was watching TV and saw an ad where the Brujita Veron, the idol of the fans of Estudiantes de la Plata, was advocating the consumption of yogurt.

Is not the first time I see it, nor does the first advertisement for this product. Of course not just any yogurt, but the brand is paid to do, Yogurísimo, the famed dairy company La Serenisima. We are saying that if we use this yogurt, will be stronger and get tired less.

With messages like this, many have consumed yogurt daily. My mom is one of them. But now with the Brujita, but for many years. 


With slogans like "A yogurt every day" and "Feed your lust for life" have gained many adherents and addicted to a product which, after all, is supposed to be healthy. I prefer me addicted to yogurt than cigarette after all. I myself take my yogurt every day. Not if you feed my will to live, but I like it.

Right character

But this was not what motivated me to write, but my question on the election of the Brujita to promote yogurt. What does matter? Because is a health nut? Because play ball? Because is it famous? Because is Estudiantes de la Plata player? Because is it bare head?

May have believed that Veron would rise making sales of its yogurt. But beyond his skills as a player, I never liked this Brujita. It seems a nasty character. So I do not understand how he was chosen for this series of ads. Because there is no only one, but already several in which it appears. I never buy a food product promoted by the Brujita Veron. Maybe a pair of football booties, but not yogurt. I miss Pancho Ibanez! Back Pancho, we don't forgive you!   :)

And watch I did not believe that Brujita intentionally play bad against England in the 2002 World Cup and all that was said. Not even put into question his skills as a football player. I do not like, it's only that. And I guess that's why I think that no one should fit. Most likely I'm wrong, but I can not help. If I had my yogurt factory, Brujita to be the last person I think to promote it. It's what they call "a matter of skin."

With Yogurisimo there is Brujita for a while

So says the end of one advertisement he did for this yogurt, along with his father and his son. That son was the one who ran over a girl with ATV in Cariló in last year. What the hell was the child with a ATV!

But back to advertising, what does that mean? that if we take this yogurt will be eternal? your child will be player of Estudiantes de la Plata as he and his father? 


Whatever the interpretation, I still leaves the question of whether it was good choice for this player to promote the yogurt. Only managers of La Serenisima knows the answer. And after all this time still hiring the Brujita, they seem to do very well.

I will end here, because I go to the store to buy yogurt. Greetings!

Diego Arregui

Monday, April 25, 2011

The Monday's video 4 - He get nervous

When you're handy with the keyboard, using all fingers to type, use the mouse only when needed, everything is faster.

But not everyone knows how to use keyboard shortcuts and only use the index finger for typing. To cut and paste into Word, use the little buttons instead of Ctrl + C and Ctrl + V, do the scroll bar by dragging the side of the window instead of using the mouse scroll wheel or arrow keys. For them everything is going slower.

For the character of this Monday's video goes something like that. And the one standing next to him wants him hanged. It's like when a knowing, wants the other to do things like make yourself. It is very funny  :)

The Scrollwheel


Until next video

Diego Arregui

Bloggers - Meet Millions of Bloggers