FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » to fill a select with json javascript from arrays
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
to fill a select with json javascript from arrays [message #175072] Wed, 10 August 2011 12:13 Go to next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
Member
in JSON/javascript if I have 3 php’s array, how can insert their values in
3 selects?
the select also must to be dependant; example Car Brand->Car
Model->Accessories
I look guide but cannot understand how solve the problem


ARRAY BRAND
brand[01]='Brand 1';
....
brand[20]='Brand 20';

ARRAY MODEL (every brand have 3 models,total 60 models)
model[01][m01]=’model m01 of brand 1’;
model[01][m02]=’model m02 of brand 1’;
....
model[20][m59]=‘model m59 of brand 20’;
model[20][m60]=‘model m60 of brand 20’;

ARRAY ACCESSORIES (every models can to have n accessories; exmpl. m01-> 3
accessories,
m60 -> 2 access.)
accessories[m01][]=‘accessories 1 of model m01’;
accessories[m01][]=‘accessories 2 of model m01’;
accessories[m01][]=‘accessories 3 of model m01’;
……..
accessories[m60][]=‘accessories 1 of model m60’;
accessories[m60][]=‘accessories 2 of model m60’;
Re: to fill a select with json javascript from arrays [message #175073 is a reply to message #175072] Wed, 10 August 2011 12:51 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 8/10/2011 2:13 PM, nawfer wrote:
> in JSON/javascript if I have 3 php’s array, how can insert their values in
> 3 selects?
> the select also must to be dependant; example Car Brand->Car
> Model->Accessories
> I look guide but cannot understand how solve the problem
>
>
> ARRAY BRAND
> brand[01]='Brand 1';
> ...
> brand[20]='Brand 20';
>
> ARRAY MODEL (every brand have 3 models,total 60 models)
> model[01][m01]=’model m01 of brand 1’;
> model[01][m02]=’model m02 of brand 1’;
> ...
> model[20][m59]=‘model m59 of brand 20’;
> model[20][m60]=‘model m60 of brand 20’;
>
> ARRAY ACCESSORIES (every models can to have n accessories; exmpl. m01-> 3
> accessories,
> m60 -> 2 access.)
> accessories[m01][]=‘accessories 1 of model m01’;
> accessories[m01][]=‘accessories 2 of model m01’;
> accessories[m01][]=‘accessories 3 of model m01’;
> ……..
> accessories[m60][]=‘accessories 1 of model m60’;
> accessories[m60][]=‘accessories 2 of model m60’;
>

Hi,

This is actually a JavaScript/JSON question.

What you probably need is something like this:
[Javascript]

for (var key in someArray){
var theValue = someArray[key];
// do stuff
}

which is a little like PHP's:

foreach ($someArray as $key => $theValue){

}

Whit that approach you'll have the key of some array, which you will
need in your accessories-array to look up the different values.

Regards,
Erwin Moller




--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: to fill a select with json javascript from arrays [message #175075 is a reply to message #175073] Wed, 10 August 2011 16:16 Go to previous messageGo to next message
nawfer is currently offline  nawfer
Messages: 34
Registered: August 2011
Karma: 0
Member
> This is actually a JavaScript/JSON question.

but in the first step how can take the array php and have array js?
I read json is solution but which is the correct code that can to work with
my type of php array?
Re: to fill a select with json javascript from arrays [message #175076 is a reply to message #175075] Wed, 10 August 2011 17:26 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/10/2011 12:16 PM, nawfer wrote:
>> This is actually a JavaScript/JSON question.
>
> but in the first step how can take the array php and have array js?
> I read json is solution but which is the correct code that can to work with
> my type of php array?

You have to understand - PHP runs on the server, and javascript on the
client. By the time your javascript code executes, PHP has completed
its execution and terminated.

So what you have to do in your PHP code is create the javascript code to
build the array then send the javascript code to the client.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: to fill a select with json javascript from arrays [message #175077 is a reply to message #175076] Wed, 10 August 2011 19:48 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 8/10/2011 1:26 PM, Jerry Stuckle wrote:
> On 8/10/2011 12:16 PM, nawfer wrote:
>>> This is actually a JavaScript/JSON question.
>>
>> but in the first step how can take the array php and have array js?
>> I read json is solution but which is the correct code that can to work
>> with
>> my type of php array?
>
> You have to understand - PHP runs on the server, and javascript on the
> client. By the time your javascript code executes, PHP has completed its
> execution and terminated.
>
> So what you have to do in your PHP code is create the javascript code to
> build the array then send the javascript code to the client.

Jerry, out of curiosity, with the information you have, is there
solution that is only PHP?

Bill B
Re: to fill a select with json javascript from arrays [message #175078 is a reply to message #175077] Wed, 10 August 2011 21:19 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/10/2011 3:48 PM, Bill B wrote:
> On 8/10/2011 1:26 PM, Jerry Stuckle wrote:
>> On 8/10/2011 12:16 PM, nawfer wrote:
>>>> This is actually a JavaScript/JSON question.
>>>
>>> but in the first step how can take the array php and have array js?
>>> I read json is solution but which is the correct code that can to work
>>> with
>>> my type of php array?
>>
>> You have to understand - PHP runs on the server, and javascript on the
>> client. By the time your javascript code executes, PHP has completed its
>> execution and terminated.
>>
>> So what you have to do in your PHP code is create the javascript code to
>> build the array then send the javascript code to the client.
>
> Jerry, out of curiosity, with the information you have, is there
> solution that is only PHP?
>
> Bill B

Not if he wants to use json. Other than that, I have no idea what he's
trying to do. But if it requires execution on the client, then
obviously not. However, if it could all be done server-side, then it
easily could.

But reading between the lines, I suspect he's using AJAX to refresh part
of a page after a search is performed. I don't know, though, and could
be way off.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: to fill a select with json javascript from arrays [message #175079 is a reply to message #175072] Thu, 11 August 2011 00:12 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 10 Aug 2011 14:13:10 +0200, nawfer wrote:

> in JSON/javascript ....

If you're trying to create a json string from PHP array data to send to a
javascript application:

http://uk2.php.net/manual/en/function.json-encode.php

If you're trying to create a PHP array or object from json data that has
been created by a javascript application:

http://uk2.php.net/manual/en/function.json-decode.php

if you're trying to create json data with javascript, or create a
javascript object from json data, try asking in a javascript newsgroup or
forum, not a php one.

If you're trying to send a string of json encoded data in response to a
client side ajax style request:

<?php
// process the request
// create the data string to send
echo $json_encoded_string;
?>

Note that generally you don't want to include any other output, and
specifically no html tags.

Rgds

Denis McMahon
Re: to fill a select with json javascript from arrays [message #175086 is a reply to message #175075] Thu, 11 August 2011 20:50 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
nawfer wrote:

>> This is actually a JavaScript/JSON question.
>
> but in the first step how can take the array php and have array js?
> I read json is solution but which is the correct code that can to work
> with my type of php array?

RTFM: <http://php.net/json_encode> (there are examples there, too!)

You would be well-advised to ignore Jerry Stuckle. The record shows that he
seldom knows what he is talking about but unfortunately compensates that
with uncalled-for rudeness.

Please follow the recommendations in
<http://www.netmeister.org/news/learn2quote.html>.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: to fill a select with json javascript from arrays [message #175087 is a reply to message #175086] Thu, 11 August 2011 21:05 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 8/11/2011 4:50 PM, Thomas 'PointedEars' Lahn wrote:
> You would be well-advised to ignore Jerry Stuckle. The record shows that he
> seldom knows what he is talking about but unfortunately compensates that
> with uncalled-for rudeness.

I will leave the issue of competency to those who are smarter. As to
rudeness - he is blunt, but so are you. Bluntness in and of itself is
not rude. You are not rude when you are blunt, and neither is he.

Could you both profit from a visit with Dale Carnegie to win friends and
influence people? Yes. Passing on doing so makes neither of you a lesser
person.

Glass houses.

Bill B
Re: to fill a select with json javascript from arrays [message #175088 is a reply to message #175086] Thu, 11 August 2011 21:56 Go to previous messageGo to next message
Paul Herber is currently offline  Paul Herber
Messages: 26
Registered: February 2011
Karma: 0
Junior Member
On Thu, 11 Aug 2011 22:50:33 +0200, Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> wrote:

> nawfer wrote:
>
>>> This is actually a JavaScript/JSON question.
>>
>> but in the first step how can take the array php and have array js?
>> I read json is solution but which is the correct code that can to work
>> with my type of php array?
>
> RTFM: <http://php.net/json_encode> (there are examples there, too!)
>
> You would be well-advised to ignore Jerry Stuckle.

I've learnt far more useful stuff from Jerry's posts than from anyone else's in this
group. When he answers a question by directing the OP to another group he is always (FSVO
always) right.



--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/
Re: to fill a select with json javascript from arrays [message #175089 is a reply to message #175088] Thu, 11 August 2011 22:42 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Paul Herber wrote:

> Thomas 'PointedEars' Lahn wrote:
>> nawfer wrote:
>>>> This is actually a JavaScript/JSON question.
>>> but in the first step how can take the array php and have array js?
>>> I read json is solution but which is the correct code that can to work
>>> with my type of php array?
>> RTFM: <http://php.net/json_encode> (there are examples there, too!)
>>
>> You would be well-advised to ignore Jerry Stuckle.
>
> I've learnt far more useful stuff from Jerry's posts than from anyone
> else's in this group. When he answers a question by directing the OP to
> another group he is always (FSVO always) right.

YMMV.

However, as you could have noticed, this is _not_ solely a JavaScript/JSON
question, as PHP has had a *built-in* function for creating JSON from PHP
arrays for quite some time (which is a Good Thing, as it spares you all the
looping and escaping). Subscribers of cljs, for example, do not need to
know that as there are various ways to create JSON. So that question is not
off-topic here, and it is not on-topic there.

Not knowing that (or knowing where to find it to give the correct answer)
but replying with somewhat correct, yet useless information nevertheless
("somewhat" because e.g., PHP is _not_ limited to the server side as
JavaScript is _not_ limited to the client side, and there is no "javascript"
to begin with – we have been over this) does _not_ count as a competent
answer in my book, and in my experience there have been few useful comments
from Jerry but a lot of unnecessarily rude and really useless ones by
comparison. Enough for me to ignore him (with one exception) and recommend
that to others similarly mistreated, which I usually do not do. Sorry, but
that's that.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: to fill a select with json javascript from arrays [message #175090 is a reply to message #175087] Thu, 11 August 2011 23:53 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/11/2011 5:05 PM, Bill B wrote:
> On 8/11/2011 4:50 PM, Thomas 'PointedEars' Lahn wrote:
>> You would be well-advised to ignore Jerry Stuckle. The record shows
>> that he
>> seldom knows what he is talking about but unfortunately compensates that
>> with uncalled-for rudeness.
>
> I will leave the issue of competency to those who are smarter. As to
> rudeness - he is blunt, but so are you. Bluntness in and of itself is
> not rude. You are not rude when you are blunt, and neither is he.
>
> Could you both profit from a visit with Dale Carnegie to win friends and
> influence people? Yes. Passing on doing so makes neither of you a lesser
> person.
>
> Glass houses.
>
> Bill B

And you can take your self-righteous attitude and stick it where the sun
doesn't shine. That way it will be right next to your head.

In case you can't figure it out - no one here gives a damn what you
think. And no one is going to change what they do, no matter how much
you troll your crap.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: to fill a select with json javascript from arrays [message #175091 is a reply to message #175090] Fri, 12 August 2011 03:24 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 8/11/2011 7:53 PM, Jerry Stuckle wrote:
> On 8/11/2011 5:05 PM, Bill B wrote:
>> On 8/11/2011 4:50 PM, Thomas 'PointedEars' Lahn wrote:
>>> You would be well-advised to ignore Jerry Stuckle. The record shows
>>> that he
>>> seldom knows what he is talking about but unfortunately compensates that
>>> with uncalled-for rudeness.
>>
>> I will leave the issue of competency to those who are smarter. As to
>> rudeness - he is blunt, but so are you. Bluntness in and of itself is
>> not rude. You are not rude when you are blunt, and neither is he.
>>
>> Could you both profit from a visit with Dale Carnegie to win friends and
>> influence people? Yes. Passing on doing so makes neither of you a lesser
>> person.
>>
>> Glass houses.
>>
>> Bill B
>
> And you can take your self-righteous attitude and stick it where the sun
> doesn't shine. That way it will be right next to your head.
>
> In case you can't figure it out - no one here gives a damn what you
> think. And no one is going to change what they do, no matter how much
> you troll your crap.

It was a compliment and a defense, Jerry. Slow down on your reading.

Bill B
Re: to fill a select with json javascript from arrays [message #175092 is a reply to message #175091] Fri, 12 August 2011 10:39 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/11/2011 11:24 PM, Bill B wrote:
> On 8/11/2011 7:53 PM, Jerry Stuckle wrote:
>> On 8/11/2011 5:05 PM, Bill B wrote:
>>> On 8/11/2011 4:50 PM, Thomas 'PointedEars' Lahn wrote:
>>>> You would be well-advised to ignore Jerry Stuckle. The record shows
>>>> that he
>>>> seldom knows what he is talking about but unfortunately compensates
>>>> that
>>>> with uncalled-for rudeness.
>>>
>>> I will leave the issue of competency to those who are smarter. As to
>>> rudeness - he is blunt, but so are you. Bluntness in and of itself is
>>> not rude. You are not rude when you are blunt, and neither is he.
>>>
>>> Could you both profit from a visit with Dale Carnegie to win friends and
>>> influence people? Yes. Passing on doing so makes neither of you a lesser
>>> person.
>>>
>>> Glass houses.
>>>
>>> Bill B
>>
>> And you can take your self-righteous attitude and stick it where the sun
>> doesn't shine. That way it will be right next to your head.
>>
>> In case you can't figure it out - no one here gives a damn what you
>> think. And no one is going to change what they do, no matter how much
>> you troll your crap.
>
> It was a compliment and a defense, Jerry. Slow down on your reading.
>
> Bill B

I can read just fine, Bill. And I stand by my statement.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: json_decode
Next Topic: Calling stored procedure on remote SQL Server machine with PDO
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 19:33:38 GMT 2024

Total time taken to generate the page: 0.02124 seconds