how to print array through ALERT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

how to print array through ALERT

Post by eshban »

I have an array like

Code: Select all

Array
(
    [0] => check_files/toplogo.png
    [1] => check_files/topphp.png
    [2] => check_files/shadow_tr.jpg
    [3] => check_files/shadow_bl.jpg
    [4] => check_files/shadow_br.jpg
)

Can i print all the paths through JAVASCRIPT ALERT
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: how to print array through ALERT

Post by Christopher »

In Javascript use the join method (e.g. myarray.join(",")). In php use the implode function (e.g. implode(",", $myarray)).
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

you could use something like:

Code: Select all

function array_alert($array){
	echo '<script type="text/javascript">alert("'.implode('\n',array_map(@addslashes,$array)).'");</script>';
}
but BE WARNED - this is not guaranteed to work - it could end up in the middle of an attribute or something!

Cheers,
Kieran
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

OK - it bothered me a little that the alert function was so picky, here's a new one:

Code: Select all

function jAlert($var){
	if(is_array($var)) return print('<script type="text/javascript">alert("'.implode('\n',array_map(@addslashes,$var)).'");</script>');
	return print('<script type="text/javascript">alert("'.addslashes($var).'");</script>');
}
It accepts either an array OR a string.

Cheers,
Kieran
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

a simple one liner

Code: Select all

function jAlert($var){
      return '<script type="text/javascript">alert("'.addslashes(is_array($var) ? implode("\n", $var) : $var).'");</script>';
}
You had a couple problems,
a) whats the point of returning a print?
b) carriage needs to be processed in double quotes, else it's taken literally
c) your use of array_map isn't correct, you should consult the manual
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Jcart wrote:a simple one liner

Code: Select all

function jAlert($var){
      return '<script type="text/javascript">alert("'.addslashes(is_array($var) ? implode("\n", $var) : $var).'");</script>';
}
You had a couple problems,
a) whats the point of returning a print?
b) carriage needs to be processed in double quotes, else it's taken literally
c) your use of array_map isn't correct, you should consult the manual
a: Returning a print killed two birds with one stone - exiting the function as soon as the first print happened, AND providing a non-falsy return value that also happens to contain the output of the function.

b: Carriage returns need to be processed in double quotes in PHP - in this case the single quotes are used to pass the properly escaped carriage return string to Javascript. If you give Javascript a real carriage return, it breaks the alert() function.

c: As far as I can tell, my use of array_map() was correct, I just double checked the manual. Do you have any particular objection in mind to this usage?

Furthermore:

1: addslashes must not be run on the whole string, as it will escape our already "escaped" \n.

2: your function doesn't work (at least for arrays) - test it!

All that being said, I do like your idea of making it a one-line function, but that makes it more difficult to extend to more data types, like objects.

Cheers,
Kieran
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

c: As far as I can tell, my use of array_map() was correct, I just double checked the manual. Do you have any particular objection in mind to this usage?
array_map(@addslashes... triggers an error saying constant 'addslashes' not defined assuming 'addslashes' -- the string which, incidentally...is the correct usage :)
EDIT: someone very recently on planetphp has blogged about this.

Code: Select all

// consider: define('addslashes', 'unexceptedRedirection');
array_map(@addslashes, $array); // bad 
array_map('addslashes', $array); // good
read about the callback type, its very interesting.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I must have been thinking about Javascript when I used that callback! :oops:

Now that you mention it, it makes perfect sense 8) Thanks ole.
Post Reply