How I fill javascript array with php variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wiedzim
Forum Newbie
Posts: 5
Joined: Wed Mar 31, 2004 12:38 am

How I fill javascript array with php variables?

Post by wiedzim »

Hello,



I need fill javascript array with some php variables from



$vyber_osoba="SELECT ID,UZIVATEL,NAZEV FROM A_PRAVA WHERE UZIVATEL='".$_SESSION['uzivatel']."' ";
$vysledok_vyber_osoba = ODBC_Exec($pripojenie, $vyber_osoba );



while(ODBC_Fetch_Row($vysledok_vyber_osoba)):
$meno=ODBC_Result($vysledok_vyber_osoba, "ID").ODBC_Result($vysledok_vyber_osoba, "NAZEV").ODBC_Result($vysledok_vyber_osoba, "JMENO");
endwhile;





And I need put variables $meno into this array and variables must be saparate by comma



var selectbox = new Array('$meno0','$meno1' .... ); for example .....



It's possible, thank you very much



wiedzim
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is possible, just use PHP to echo out the Javascript.

Mac
wiedzim
Forum Newbie
Posts: 5
Joined: Wed Mar 31, 2004 12:38 am

var selectbox = new Array('$meno0','$meno1' .... ); for exam

Post by wiedzim »

hello twigletmac


you mean

var selectbox = new Array('<? echo $meno; ?>' );

thanks wiedzim
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Exactly.

Mac
wiedzim
Forum Newbie
Posts: 5
Joined: Wed Mar 31, 2004 12:38 am

Post by wiedzim »

but its doesn't working the array is empty
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

How do $meno0 and $meno1 get set? Are you doing debugging, ie. as you set the variables echoing them out to make sure they have a value?

Mac
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

hello,

for me it seems that the $meno0 and $meno1 arent set!

try it with $meno[0] and $meno[1]

greez Pozor

PS: to get the content of an array use this Format array[$id] //id is the number of the entry or the labeling if you have an assoc_array...
wiedzim
Forum Newbie
Posts: 5
Joined: Wed Mar 31, 2004 12:38 am

Post by wiedzim »

while(ODBC_Fetch_Row($vysledok_vyber_osoba)):
$meno=ODBC_Result($vysledok_vyber_osoba, "ID").ODBC_Result($vysledok_vyber_osoba, "NAZEV").ODBC_Result($vysledok_vyber_osoba, "JMENO");
endwhile;

I only set one $meno from ODBC_RESULT, but if I have a while --- endwhile; echo $meno; I have more records and this records I'd like put into array but I dont know how

ID is a personal numner etc. 4589854236 not a number 1,2,3.4
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

hello,

im working normally with mysql and the funktion for mysql...
while(ODBC_Fetch_Row($vysledok_vyber_osoba)):
$meno=ODBC_Result($vysledok_vyber_osoba, "ID").ODBC_Result($vysledok_vyber_osoba, "NAZEV").ODBC_Result($vysledok_vyber_osoba, "JMENO");
endwhile;

Code: Select all

<?php

$ID = ODBC_Result($vysledok_vyber_osoba, "ID"); //only to simplify the code here (better readability)

while(condition)
{
  $meno[] =  $ID;
}

or

while(condition)
{
  $meno[0][] = $ID;
  $meno[1][] = $NAZEV;
  $meno[3][] = $JMENO;
}

or

while(condition)
{
  $meno[] = $ID.$NAZEV.$JMENO;
}
?>
Here a little skript to work it out when you have a array...

Code: Select all

<?php
$meno[] = 'first';
$meno[] = 'second';
$meno[] = 'third';
$meno[] = 'fourth';
$meno[] = 'fifth';

$output = 'var selectbox = new Array(';
$size = sizeof($meno);
$count = 1;
foreach($meno as $value)
{
  if((1 < $count) and ($size >= $count))
  { 
    $output .= ',';  //only between the values
  }
  $output .= "'".$value."'";
  $count++;
  echo 'count: '.$count.'<br>';
  echo 'value: '.$value.'<br>';
}
$output .= ');';
echo '<pre>';
echo $output;
echo '</pre>';
?>
wiedzim
Forum Newbie
Posts: 5
Joined: Wed Mar 31, 2004 12:38 am

Post by wiedzim »

thanks its work better

bye wiedzim
Post Reply