Jump to content

overall question, mostly javascript


Ninjakreborn

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

[code]<?php
// send header so browser knows how to treat the page
Header("content-type: application/x-javascript");
mysql_connect("localhost", "elostand_west", "apex");
mysql_select_db("elostand_general");
// 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; ?>);
<?php
}
// set some parameters to help control my script further down
?>
var inc = 0;
var x
<?php
while ($row = mysql_fetch_array($query)) {
?>

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

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.
Link to comment
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
[code]
function arraytojs($array){
$str = "";
foreach ($array as $val):
  $str .=  "\"".$val."\",";
endforeach;
$str = rtrim($str, ",");
echo $str;
}[/code]

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

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,
Link to comment
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
[code]
while ($row = mysql_fetch_array($query)) {
$arrName[] = $row['name'];
$arrUrl[] = $row['url'];
}[/code]
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
[code]
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;
}[/code]
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.