Jump to content


This topic is now archived and is closed to further replies.


overall question, mostly javascript

Recommended Posts

I never had to use php with javascript, I also never messed with arrays much, I always did basic stuff.
The thing is, I have tried creating 2 dimensional arrays, associative arrays, and nothing is working, I think I need a stepping stone if someone can help, after creating about 3-4 different scripts(I am trying to get a grounding point for the rest of the program), here is what I have at the moment.
I am trying to figure out

// send header so browser knows how to treat the page
Header("content-type: application/x-javascript");
mysql_connect("localhost", "elostand_west", "apex");
// run query's
$select = "SELECT * FROM businesses;";
$query = mysql_query($select);
// check for number of rows for later use
$numrows = mysql_num_rows($query);
if ($numrows) {
// creates array's dynamically using php
var name = new Array(<?php echo $numrows; ?>);
var url = new Array(<?php echo $numrows; ?>);
// set some parameters to help control my script further down
var inc = 0;
var x
while ($row = mysql_fetch_array($query)) {

name[inc++] = '<?php echo $row['name']; ?>';
url[inc++] = '<?php echo $row['url']; ?>';
}// end while
for (var i in name){
document.getElementById('scrollboxtime').innerHTML = name[x] + "<br />" + url[x];

Also I have a div on the homepage, like
[code]<div id="scrollboxtime"></div>[/code]

Here are the things I have been trying to accomplish with this script, that I have encountered problems with

1. I want it to pull data from a database, somehow trap information on 2 different values.  I need the name and associated url of each entry.

2. Once i have successfully recorded the information so I can work with the data I need to get them to display on the div like so

[move]name       name          name         name
  url           url               url             url[/move]

Something similar to that but enough space in between them, to not look messed up when a longer url, is put into the banner.

3. I need to get this div, to be populated automatically, as I said, the database pulls information, creates a js array, and uses that array as the information.
I need this div, to take the text, as it looks formatted above, and scroll it from the right wall of the div, all the way to the left wall of the div, and disappear off the page(at the end of the div), but it needs to keep scrolling across, when the final database entry(which there all recorded into an array when someone opens the homepage), comes, then it resets back at the first, and keeps going forever. 
As for the scrolling, I was using scrollTo, scrollBy, scroll, and then I finally realized these aren't used to get the scroll affect I wanted, they are used to set up scroll bars on the page, or on elements, now I have to figure out how to get it to scroll as well.
Any advice, any guidance, any stepping stones will be greatly appreciated.

Share this post

Link to post
Share on other sites
the way I worked around the arrays in PHP and javascript was to create a PHP array object
pass it into this PHP function
function arraytojs($array){
$str = "";
foreach ($array as $val):
  $str .=  "\"".$val."\",";
$str = rtrim($str, ",");
echo $str;

by calling in Javascript the following line of code
var jsArray = new Array(<?=arraytojs($phpArray)?>);//call in the js script

which means I get all the data onto the javascript side into an new array and can manipulate it in Javascript.

dont know if that helps,

Share this post

Link to post
Share on other sites
expanding on what I said above, here is a bit more, I cannot guarantee the accuarcy of my code as I have not tested it, after writing the above PHP function and running the query to get everything from Buisnesses you could
while ($row = mysql_fetch_array($query)) {
$arrName[] = $row['name'];
$arrUrl[] = $row['url'];
this will create 2 seperate arrays of exactly the same length one hol;ding the names and one holding the urls
then in the javascript side you could
var jsarrName = new Array(<?=arraytojs($arrName)?>);
var jsarrUrl = new Array(<?=arraytojs($arrUrl)?>);

for (var x = 0; x < jsarrName.length(); x++) {
  var name = jsarrName[x];
  var url = jsarrUrl[x];
  document.getElementById('scrollboxtime').innerHTML = name + "<br />" + url;
which re-creates the 2 arrays but now in javascript, using the for loop it iterates over both of them and makes var name and var url equal the same index values as they were origonally put in and then uses scrollboxtime

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.