Current location: Hot Scripts Forums » Programming Languages » PHP » PHP to JAVASCRIPT: 2D Array Conversion


PHP to JAVASCRIPT: 2D Array Conversion

Reply
  #1 (permalink)  
Old 10-01-03, 12:15 PM
770 770 is offline
New Member
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
PHP to JAVASCRIPT: 2D Array Conversion

I've got a 2D array in php that looks like this:

$pi = 0;
$pf[$pi][0] = 0;
$pf[$pi][1] = 0;
$pf[$pi][2] = 1;
$pf[$pi][3] = 1;
$pf[$pi][4] = "Johnny come lately";
$pf[$pi][5] = 7;
$pi++;
$pf[$pi][0] = 0;
$pf[$pi][1] = 0;
$pf[$pi][2] = 1;
$pf[$pi][3] = 2;
$pf[$pi][4] = "Peter Pan had a mother";
$pf[$pi][5] = 14;
$pi++;
$pf[$pi][0] = 0;
$pf[$pi][1] = 0;
$pf[$pi][2] = 1;
$pf[$pi][3] = 3;
$pf[$pi][4] = "The elf in green";
$pf[$pi][5] = 6;

ETC

How do I get this array into js so I can therein manipulate it?

Thanks for your time - Meir
Reply With Quote
  #2 (permalink)  
Old 10-01-03, 05:58 PM
Patriarch Patriarch is offline
Newbie Coder
 
Join Date: Sep 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
I'm no JavaScript expert, but my guess is that you'd do some loops on the array you set up, and then print out the JavaScript code using the PHP variables.
Reply With Quote
  #3 (permalink)  
Old 10-02-03, 11:36 AM
770 770 is offline
New Member
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Yes - but I have yet to get it working. I'm not too good at javascript, let alone in interaction with php. I need to figure out how to convert the array into a javascript array because it's much too long an array to simply type in.
Reply With Quote
  #4 (permalink)  
Old 10-02-03, 03:34 PM
Chroder Chroder is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Toronto, Ontario
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Try something like this

PHP Code:

<?php


// start off the javascript array stirng
$js_array 'var pf = [';

// loop into the first dimension
for($i 0$ic = (count($pf)-1); $i $ic$i++)
{
    
// add a new javascript array to the js_stirng
    
$js_array .= '[';
    
    
// loop through the second dimension
    
for($x 0$xc = (count($pf[$i])-1); $x $xc$x++)
    {
        
// add the data to the js_array
        
$js_array .= '"'.$pf[$i][$x].'"';
        
        
// if its not the last peice of data, add a comma
        
if($x != $xc)
            
$js_array .= ',';
    }
    
    
// end the javascript array
    
$js_array .= ']';
    
    
// if its not the last peice of data, add a comma
    
if($i != $ic)
        
$js_array .= ',';
}

// finish the js_array string
$js_array .= '];';

// now just print the js
?>

<script language="javascript">
<!--
<?=$js_array?>
//-->
</script>
__________________
DevBox.net | DevBoxForums.com
Reply With Quote
  #5 (permalink)  
Old 11-05-03, 02:47 PM
Sonic The Edge Hog Sonic The Edge Hog is offline
New Member
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Chroder
Try something like this

PHP Code:

<?php


// start off the javascript array stirng
$js_array 'var pf = [';

// loop into the first dimension
for($i 0$ic = (count($pf)-1); $i $ic$i++)
{
    
// add a new javascript array to the js_stirng
    
$js_array .= '[';
    
    
// loop through the second dimension
    
for($x 0$xc = (count($pf[$i])-1); $x $xc$x++)
    {
        
// add the data to the js_array
        
$js_array .= '"'.$pf[$i][$x].'"';
        
        
// if its not the last peice of data, add a comma
        
if($x != $xc)
            
$js_array .= ',';
    }
    
    
// end the javascript array
    
$js_array .= ']';
    
    
// if its not the last peice of data, add a comma
    
if($i != $ic)
        
$js_array .= ',';
}

// finish the js_array string
$js_array .= '];';

// now just print the js
?>

<script language="javascript">
<!--
<?=$js_array?>
//-->
</script>
mine is much liek this and i tried what you gave him and it does not work.

$new ='';
$js_arr_img = 'var slideimages=new Array(';
$js_arr_link = 'var slidelinks=new Array(';
if(!$nr||mysql_num_rows($nr)==0) {
$new = "No New Releases found.<br>";
$js_arr_img .= '")';
$js_arr_link .= '")';
} // end if
else {
for($x = 0, $xc = (mysql_num_rows($nr)); $x < $xc; $x++)
{
$arr = mysql_fetch_array($nr);
$new .= "<b>".ucfirst($arr['band'])."</b> - <a href=\"$arr[mp3link]\" target=\"_blank\">&quot;".ucwords($arr['mp3text'])."&quot;<br></a>";
// add the data to the js array
$js_arr_img .= '"'.$arr['img'].'"';
$js_arr_link .= '"'.$arr['link'].'"';
// if its not the last peice of data, add a comma
if($x != $xc-1) {
$js_arr_img .= ',';
$js_arr_link .= ',';
} //end if
}
// end the javascript array
$js_arr_img .= ')';
$js_arr_link .= ')';
} // end else

---------------------------------------------
<?php
echo "<script language=\"JavaScript1.1\">
<!--
var whichlink=0
var whichimage=0
var pixeldelay=(ie55)? document.images.slide.filters[0].duration*1000 : 0
function slideit(){
if (!document.images) return
if (ie55) document.images.slide.filters[0].apply()
document.images.slide.src=imageholder[whichimage].src
if (ie55) document.images.slide.filters[0].play()
whichlink=whichimage
whichimage=(whichimage<slideimages.length-1)? whichimage+1 : 0
setTimeout(\"slideit()\",slidespeed+pixeldelay)
}
slideit()

//-->
<!--

//Pixelating Image slideshow
//Copyright Dynamic Drive 2001
//Visit http://www.dynamicdrive.com for this script

//specify interval between slide (in mili seconds)
var slidespeed=3000
//specify images
$js_arr_img
//specify corresponding links
$js_arr_link

var imageholder=new Array()
var ie55=window.createPopup
for (i=0;i<slideimages.length;i++){
imageholder[i]=new Image()
imageholder[i].src=slideimages[i]
}

function gotoshow(){
window.location=slidelinks[whichlink]
}

//-->
</script>";
echo $js_arr_img;
?>

-----------------------------
the echo at teh end outputs the js var declaration correctly so i'm lost.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Multidimensional Array Sorting Kage PHP 6 12-12-05 04:19 PM
php and javascript together? gamextremer2003 PHP 5 11-06-03 02:18 PM
Javascript vs PHP, problem with arrays Chuff JavaScript 0 10-03-03 04:01 AM
edit php in Squirrelmail jrcortrightiii PHP 1 07-10-03 02:08 PM


All times are GMT -5. The time now is 06:28 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.