Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-25-2005, 06:46 PM   PM User | #1
rndilger
New Coder

 
Join Date: Jan 2005
Posts: 62
Thanks: 1
Thanked 0 Times in 0 Posts
rndilger is an unknown quantity at this point
mysql data into an array

Hi, I'm still quite new to php and mysql and thus have run into the following problem. I would like to read IDs from a database into an array and then pass that array to a javascript function. If I'm thinking correctly, this should be possible as php is server side and javascript client side. Anyways, I've got the following query:

mysql_query("SELECT id FROM hatchery WHERE parent='0' ORDER BY id")

and want to pass the selected ids into an array to be used by a javascript function. Any help would be appreciated!

Thanks
rndilger is offline   Reply With Quote
Old 01-25-2005, 09:26 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
it's not a mysql issue. what server side language are you using? PHP?
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-25-2005, 10:12 PM   PM User | #3
rndilger
New Coder

 
Join Date: Jan 2005
Posts: 62
Thanks: 1
Thanked 0 Times in 0 Posts
rndilger is an unknown quantity at this point
yes, sorry...in all, i'm using php and javascript
rndilger is offline   Reply With Quote
Old 01-25-2005, 10:24 PM   PM User | #4
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
something like
PHP Code:
$script = ("<script language=\"JavaScript\">
              <!-- Begin array
              var arrItems1 = new Array();\n"
);
$y '0';
while (
$row mysql_fetch_assoc($result)) {
    
$script .= ("arrItems1[" .$y "] = \"" .    $row['id'] . "\";\n");
    
$y ++ ;
}
$script .= ('//  End -->
                 </script>'
); 
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 01-25-2005, 10:57 PM   PM User | #5
rndilger
New Coder

 
Join Date: Jan 2005
Posts: 62
Thanks: 1
Thanked 0 Times in 0 Posts
rndilger is an unknown quantity at this point
Thanks for the help!

Just one more question. Is is possible to then pass that array to a function as an argument?

For example...

while(list($id, $parent) = mysql_fetch_row($result))
{
$str.=CreateItem($id, $parent, $arrItems1);
}
return($str);

Function CreateItem($id, $parent, $arrItems1)
{
CODE
}
rndilger is offline   Reply With Quote
Old 01-26-2005, 12:37 AM   PM User | #6
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
No.

If you wanna use it as a javascript-array, then it is sent to the browser --> javascript = clientsided. So the PHP is just generating the html-code that you need for your clientside javascript.
if you want to use it as a PHP array, then you need:
PHP Code:
$arrItems1 = array();
while (
$row mysql_fetch_assoc($result)) {
    
$arrItems1[] = $row['id'];

__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:40 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.