The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

emdeboas (7) [Avatar] Offline
#1
I an going to nag about PHP again. This time the JSON stuff in listing 3.6.
PHP 5.2 has built-in JSON support, older versions can use a simple JSON package at:
http://pear.php.net/pepr/pepr-proposal-show.php?id=198

The code for images.php becomes then (using the PEAR package):

<?php <br /> header("Content-type: text/javascript"smilie;
require('images.inc.php');
require "JSON.php";
$json = new Services_JSON();
$arr['path'] = $path; // $arr is an associative array with the data
$arr['pre'] = $img_pre_path;
if (count($subdirs)>0) $arr['folders'] = $subdirs;
if (count($imgs)>0) $arr['images'] = $imgs;
echo $json->encode($arr); // encode the array to JSON format
?>

Much clearer and more reusable I would think.
Cheers! Ghica.
davecrane (149) [Avatar] Offline
#2
Re: PHP an JSON
Hi Ghica,

Thanks for nagging smilie , and sorry it's taken me so long to reply.

Your comments about the PHP are valid, and I've added a few hints to use the proper libraries if doing it in production.

Writing example code for a book is a funny business, especially with Ajax, where the server-side code is of secondary importance to the book. I've tried to resist the urge to use special features of PHP, Java or whatever else I'm using, so that the code can be read as pseudo-code by peope using other languages - I'm hoping the book will be of interest across the PHP, Java, Ruby, .NET communities. As a result, I often write very simplistic PHP or Java, for these demonstration purposes!

Thanks for all your helpful comments.

Dave
emdeboas (7) [Avatar] Offline
#3
Re: PHP an JSON
Thanks, Dave.
I was about to give up reading the early version of the book, thinking that apparently our feedback is not appreciated or even noticed.

> especially with Ajax, where the server-side code is
> of secondary importance to the book.
Why is the server side code of secondary importance? The use of Ajax poses many new questions on how to architect your application. All the books on Ajax I have seen so far, seem to shift the UI logic to the client (javascript). Personally, I think that is not a good idea, and not necessary. I will post another message explaining how.

> I've tried to
> resist the urge to use special features of PHP, Java
> or whatever else I'm using, so that the code can be
> read as pseudo-code by people using other languages -
I can sympathize with this, there is no right way to do it. It poses the danger though, that the examples will appeal to nobody. If PHP is my language, then I would think the PHP examples are not realistic and not usable for my own development, and of the other examples I would think: not my language! Etc. However, good PHP is so close to good Java (or any other language with OO features), that Java or C# developers should understand it easily. More easily than the other way around unfortunately.

Thanks again for writing a useful book!
Ghica.