/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP to JS array help please

I posted this in here because i think its the JS thats wrong. Its probably something simple which i’m missing.

The MOST annoying thing is this works on my local environment, but when i upload it i have problems.

Basically i have a CSV file which i have built a php class to handle and on this page i get 3 random records from it and use the IDs to get the image names from a folder of images. I have an image class as well which takes an arr of IDs and gets the appropriate images. i.e if i send the ID’s TR34 and CO10 and ask for the thumbs it will return tBRANCH_TR34.jpg and tBRANCH_CO10.jpg.

This is used to make a property of the week display where it shows the image and brief description of the property. There is a next and previous button which the user can click to cycle through them. I’m not particularly good at JS and although this works perfectly on my local computer on a live server it displays the price and description, name and street, but it won’t get the image file names. Alerting the imageArr on the local version it displays the correct 3 filenames, doing the same on the live version it just shows two single quotes ”.

i’m 99% sure the php is all working, as i have copied the code onto the index page and and the filenames you will see under the logo are whats present in the $top_properties_arr.

The image that is present on load is just a dumby image hard coded and in another folder, the images i want are all in the images/ folder.

[url]http://www.eosinteractive.com/tandc/#[/url]

That is a test page ^

here is the code which is in a seperate file in a scripts folder on the server and is called in a javascript tag in the header.

nb: I guess the way i’m pushing the php array into the JS ones is a bit crap, but i’m not sure how to push a 2 dimensional associative php array into JS, so i seperated it into 5 JS arrays and then joined them into a 2 dimensional one.

[QUOTE]

<?php
//##################################
//### Includes
//##################################

include(‘../includes/class_lib.php’);

$images = new image_files(‘../images/’);
$csv = new csv_handler(“../xfile.csv”);

$random_properties_arr = $csv->get_random(3); //Get 3 random properties

foreach($random_properties_arr as $ID=>$header){ //create array of IDS
$ID_arr[] = $header[‘PropertyID’];
}

$image_arr = $images->get_by_ID($ID_arr,”thumb”); //Get images

$counter = 0;
foreach($image_arr as $ID=>$image){
$top_properties_arr[$counter][0] = $ID;
$top_properties_arr[$counter][1] = $image;
$top_properties_arr[$counter][2] = $random_properties_arr[$ID][‘ShortDescription’];
$top_properties_arr[$counter][3] = $random_properties_arr[$ID][‘PropertyName’];
$top_properties_arr[$counter][4] = $random_properties_arr[$ID][‘Street’];
$top_properties_arr[$counter][5] = $random_properties_arr[$ID][‘Price’];
$counter++;

}
?>

window.addEvent(“domready”, function(){
var imageArr = [];
var descriptionArr = [];

var IDArr = [];
var nameArr = [];
var streetArr = [];
var priceArr = [];

var topPropertiesDescription = [];

var pointer = 1;

<?php
$counter = 0;
foreach ($top_properties_arr as $single_property){ //push array to JS
print “IDArr.push(“$single_property[0]” );”;
print “imageArr.push(“$single_property[1]” );”;
print “descriptionArr.push(“$single_property[2]” );”;
print “nameArr.push(“$single_property[3]” );”;
print “streetArr.push(“$single_property[4]” );”;
print “priceArr.push(“$single_property[5]” );”;
}
?>
alert(imageArr);
var noProps;
noProps = imageArr.length;

topPropertiesDescription = new Array (noProps)

for (var i = 0; i < topPropertiesDescription.length; ++ i) topPropertiesDescription[i] = new Array (noProps); //int array

for(var i = 0; i < noProps; i++ ){ //concat arrays
topPropertiesDescription[i][0] = IDArr[i];
topPropertiesDescription[i][1] = imageArr[i];
topPropertiesDescription[i][2] = descriptionArr[i];
topPropertiesDescription[i][3] = nameArr[i];
topPropertiesDescription[i][4] = streetArr[i];
topPropertiesDescription[i][5] = priceArr[i];
}

$$(“.prev”).each(function(prevButton){ //previous property
prevButton.addEvent(“click”,function(){
if(pointer == 0){ pointer = imageArr.length; }
pointer–
$(“top_prop_image”).innerHTML = ‘<a href=”‘+topPropertiesDescription[pointer][0]+'”><img src=”images/’+ topPropertiesDescription[pointer][1] +'” width=”194″ height=”146″ /></a>’;
$(“title”).innerHTML = topPropertiesDescription[pointer][3];
$(“street”).innerHTML = topPropertiesDescription[pointer][4];
$(“short_description”).innerHTML = topPropertiesDescription[pointer][2].substr(1,65)+’…’;
$(“price”).innerHTML = topPropertiesDescription[pointer][5];
});
});

$$(“.next”).each(function(nextButton){ //next property
nextButton.addEvent(“click”,function(){
pointer++
if(pointer == imageArr.length){ pointer = 0; }
$(“top_prop_image”).innerHTML = ‘<a href=”‘+topPropertiesDescription[pointer][0]+'”><img src=”images/’+ topPropertiesDescription[pointer][1] +'” width=”194″ height=”146″ /></a>’;
$(“title”).innerHTML = topPropertiesDescription[pointer][3];
$(“street”).innerHTML = topPropertiesDescription[pointer][4];
$(“short_description”).innerHTML = topPropertiesDescription[pointer][2].substr(1,65)+’…’;
$(“price”).innerHTML = topPropertiesDescription[pointer][5];
});
});

});

[/QUOTE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Mar 15.2009 — Take a look at this thread. Another user asked this same question.
Copy linkTweet thisAlerts:
@TorkaniauthorMar 15.2009 — I may have almost got this to work, i don't think it is the JS actually if i fix it i will post a reply.
Copy linkTweet thisAlerts:
@TorkaniauthorMar 15.2009 — Take a look at this thread. Another user asked this same question.[/QUOTE]

Cheers for that link, that will certainly improve that part of the code, but i am now alerting the php and it seems on f the live version in this file its not getting the image names, so its not actually the JS. I ain't figured it out yet though.
×

Success!

Help @Torkani spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...