Re: How to convert this PHP into JavaScript [message #173175 is a reply to message #173173] |
Fri, 25 March 2011 23:45 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 3/25/2011 7:23 PM, sheldonlg wrote:
> On 3/25/2011 5:01 PM, Jerry Stuckle wrote:
>> On 3/25/2011 4:51 PM, Denis McMahon wrote:
>>> On Fri, 25 Mar 2011 15:22:42 -0400, Jerry Stuckle wrote:
>>>
>>>> On 3/25/2011 2:59 PM, sheldonlg wrote:
>>>> > On 3/24/2011 12:50 PM, Jerry Stuckle wrote:
>>>> >> On 3/24/2011 10:23 AM, Oltmans wrote:
>>>> >>> Hi PHP gurus, I hope you're doing well.
>>>> >>>
>>>> >>> I'm trying to convert a PHP script into JavaScript. I'm posting here
>>>> >>> because I don't know much PHP and I hope someone among you can help.
>>>> >>> Here is the PHP
>>>> >>>
>>>> >>> array(
>>>> >>> 'abc' => '1 AND (2 OR
>>>> >>> 3)', // Define how to
>>>> >>> use the following criteria
>>>> >>> 'def' => array('marketing_campaign_id', 'status', 'status'), //
>>>> >>> Filter by these three criteria 'ghi' => array('eq', 'eq',
>>>> >>> 'eq')
>>>> >>> );
>>>> >>>
>>>> >>>
>>>> >>> I need to convert above into JavaScript object which looks like
>>>> >>>
>>>> >>> {
>>>> >>>
>>>> >>> 'abc' : '1 AND (2 OR 3)'
>>>> >>> }
>>>> >>>
>>>> >>>
>>>> >>> but I don't know how to convert
>>>> >>>
>>>> >>> array('marketing_campaign_id', 'status', 'status')
>>>> >>>
>>>> >>> into equivalent JavaScript. Can someone please let me know how to
>>>> >>> convert above into JavaScript? I will really appreciate your help in
>>>> >>> this regard. Thanks!
>>>> >>
>>>> >> If you want to write javascript, you should ask in a javascript
>>>> >> newsgroup.
>>>> >>
>>>> >>
>>>> > No Jerry, not this time. He has a PHP array and he wants to use that
>>>> > PHP array to generate a script in javascript. That means that
>>>> > knowledge
>>>> > of BOTH is required.
>>>> >
>>>> > This is more properly asked here in this PHP group than in the
>>>> > Javascript group. Everyone here also knows at least SOME Javascript.
>>>> > The reverse is not true for the Javascript group. People there may not
>>>> > know ANY PHP (though most seem to).
>>>> >
>>>> >
>>>> No, Sheldon, he needs to be in comp.lang.javascript - where some of the
>>>> people there know PHP.
>>>>
>>>> The first thing he needs is the appropriate javascript he needs - i.e.
>>>> hand coding it. The generating the appropriate javascript from the
>>>> array is simple.
>>>>
>>>> Now, if he would have said "I have this javascript I need to generate
>>>> from this PHP array...", I would agree this is more appropriate.
>>>
>>> It does depend on your viewpoint of what he's asking.
>>>
>>> My viewpoint is that he has a server side php application that generates
>>> a (two dimensional associative) array in php, and that he subsequently
>>> wants to use the data in that array client side with javascript running
>>> in a website viewers browser.
>>>
>>> So, I see his problem as being "how do I pass the information contained
>>> in this (two dimensional associative) array in my php script on the
>>> server into the webpage that I am generating to send to the website
>>> viewers client browser so that it can be used by scripts running in a
>>> client side context on that web page?"
>>>
>>> My solution is to create a JSON string representation of his (two
>>> dimensional associative) array, and put that string into the heredoc
>>> output in a way that the javascript will create a javascript object
>>> that,
>>> whilst not technically a (two dimensional associative) array, is similar
>>> enough to one in structure that he can access the data elements of it in
>>> a manner that correlates to the keys in the original php (two
>>> dimensional
>>> associative) array.
>>>
>>> Doubtless there are other solutions, if his array always had the same
>>> structure and format, he could write code to generate whatever
>>> javascript
>>> representation of the array he wished[1], but the json approach means
>>> that if the structure of his php generated array changes, the
>>> transfer of
>>> that array into a javascript object is probably robust enough to not
>>> need
>>> "tweaking", and the only changes he's likely to have to make to his
>>> javascript will be how he uses the modified object, without having to
>>> worry about restructuring the conversion from the php array to the
>>> javascript object.
>>>
>>> Rgds
>>>
>>> Denis McMahon
>>>
>>
>> <snip>
>>
>>
>> Personally, I think a much easier solution would be to just generate the
>> javascript in the page - no json overhead, no extra calls to the server
>> or anything else.
>>
>> As for the structure of the PHP array changing - he'll have the same
>> problem with his javascript either way - he'll have to change the PHP
>> code creating the json data and/or the javascript to process it.
>>
>> But either way, he still needs to know what he wants for his javascript.
>>
>
> I just, two days ago, had a need to do just that. What I did was:
>
> print "<script>\n";
> print " var foo = new Array();\n";
> print " foo[0] = " . $foo[0] . ";\n";
> etc. (generating the array for javascript)
> print " function myFunc(i) { \n";
> print " ...... do stuff with the foo[i] ......\n";
> print " }\n";
> print "</script\n";
>
> and then have the function myFunc called as needed later in the page.
>
>
Sure, I've done similar many times. It's quite a bit easier than trying
to use json.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|