JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 15th, 2001, 11:17 AM
curvedvision curvedvision is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 2 curvedvision User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Creating a javascript array from database data

Hi there,

Well after a few hours roaming around various websites I am at a loss.
Here is what I am trying to do:

1) Access a MySQL database which contains 1 table
2) Read the records from this table
3) Store the values returned from this table into javascript array
elements, ie, if I get the values "dog", "cat" and "cow" back I want
these stored in an array as such:
myArray[0] = valueReturned1
myArray[1] = valueReturned2
myArray[2] = valueReturned3

You get the idea.

Problem being that I cannot work out how to implement the javascript
section of this. At the moment my php script writes the values returned
from the database to screen but I require these to be stored in a
javascript array. Please can someone help me before I go mad

Here is my current .php script:

----------------------------------------------
<html>
<head>
<title>Menus test</title>
</head>
<body bgcolor="white">

<?php
$dbhost = 'localhost';
$dbuser = 'guest';
$dbpass = 'guest';
$dbname = 'IFE';
$dbtable = 'menus';

//------ DATABASE CONNECTION --------//
mysql_connect($dbhost,$dbuser,$dbpass)
or die ("Unable to connect to database");

mysql_select_db($dbname)
or die ("Unable to select database");


$sql = "SELECT * FROM $dbtable";
$result = mysql_query($sql);

$number = mysql_numrows($result);

$i = 0;

if ($number == 0)
print "Error - No records found";
elseif ($number > 0)
{
while ($i < $number)
{
$text = mysql_result($result, $i, "name");
print "$text";

$i++;
}
}

mysql_free_result($result);
mysql_close();
?>

</body>
</html>
----------------------------------------------

Thanks, Neil

Reply With Quote
  #2  
Old August 15th, 2001, 01:36 PM
JohnB JohnB is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Location: UK
Posts: 527 JohnB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
just print out the javascript with the php variables as the values. Something like this -may have errors but it gives the general idea.
PHP Code:
<html
<
head
<
title>Menus test</title
</
head
<
body bgcolor="white"

<?
php 
$dbhost 
'localhost'
$dbuser 'guest'
$dbpass 'guest'
$dbname 'IFE'
$dbtable 'menus'

//------ DATABASE CONNECTION --------// 
mysql_connect($dbhost,$dbuser,$dbpass
or die (
"Unable to connect to database"); 

mysql_select_db($dbname
or die (
"Unable to select database"); 


$sql "SELECT * FROM $dbtable"
$result mysql_query($sql); 

$number mysql_numrows($result); 

$i 0

if (
$number == 0
print 
"Error - No records found"
elseif (
$number 0

echo
"<script language='javascript'>\n";
echo
"myarray = new Array($number-1);\n";
while (
$i $number

$text mysql_result($result$i"name"); 
print 
"$text"
echo
"myarray[$i]=$text;";
$i++; 


echo
"\n</script>";
mysql_free_result($result); 
mysql_close(); 
?> 

</body> 
</html> 
__________________
Doc MacGurgles Babylonian Lizard Tooth Oil worked for me

Reply With Quote
  #3  
Old August 16th, 2001, 03:42 AM
curvedvision curvedvision is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 2 curvedvision User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OK thanks a lot for your help JohnB. That appears to get the desired result

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Creating a javascript array from database data

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap