473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to insert a PHP array into a javaScript array

Claus Mygind
571 512MB
I was looking for a simple way to create a javaScript array when streaming out my php code.

my php sql query generates a quick rowset result in an associative array like this.
Expand|Select|Wrap|Line Numbers
  1. if ( $result = $db->query($sql) )
  2. {
  3.   if ($result->num_rows > 0)
  4.   {
  5.     $f = mysqli_fetch_all( $result, $resulttype = MYSQLI_ASSOC );
  6.   }
  7. }
  8.  
Then when streaming out the page, inserting the following in the page header
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.   var exportArray = <?php echo $f;?>;
  3. </script>
  4.  
But of course this simple example simply generates a javaScript array with no content.

Is it only possible to copy this data using two loops. Where in outer loop you cycle through the php array. And in the inner loop you cycle through each column of that specific row?

What I was looking for was a better way.

When streaming out my page to the client computer, I was looking for a quick way to insert this data into a javaScript array on the resulting page web page. I was hoping not to have to loop through the php array and extract each column in each row.
Apr 13 '15 #1

✓ answered by Dormilich

I was looking for a simple way to create a javaScript array when streaming out my php code.
json_encode() ?

1 1508
Dormilich
8,658 Expert Mod 8TB
I was looking for a simple way to create a javaScript array when streaming out my php code.
json_encode() ?
Apr 14 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: JKop | last post by:
template<class T> inline T& NthArrayMember( T & (array),size_t i) { return array; } template<class T> inline const T& NthArrayMember( const T & (array),size_t i) { return array;
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
1
by: garimapuri | last post by:
hi ihad an array in php and iwant to insert its value in database the coding is: <?php $nv_pairs = explode('&',$query_string); $array; list ($name, $value) = explode ('=',$nv_pairs); $i = 0;...
7
by: Eric Boutin | last post by:
Hi ! I would like to generate an array of type char; I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them;...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
7
by: Oleg Konovalov | last post by:
Hi, I am trying to pass a bunch of checked checkboxes (Javascript array) from page1 to the Java action class on subsequent web page (page2). (on page 1 I have a bunch of DB rows with a checkbox,...
6
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
Hey guys, I've searched high and low for a way to populate a vb array with data from a javascript array. I can find 50 ways to do it with ASP but not one for VB. I appreciate what help you can...
4
by: Bob Bedford | last post by:
Hello, I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and 4 giving the values 1 and 2. How can I do that, how to search in an array of array ? in short, how to write the...
3
by: JJ | last post by:
I am using a handler (processImage.ashx) to display an image. The image is displayed according to parameters passed in the querystring. The handerl is called via some clientside javascript. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.