Jump to content


Photo

overall question, mostly javascript


  • Please log in to reply
2 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 07 September 2006 - 02:40 PM

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

<?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];
}

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

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 07 September 2006 - 05:33 PM

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."\",";
 endforeach;
 $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,

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 07 September 2006 - 05:50 PM

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
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users