Hi, I’m putting together a php code that outputs html. I got stuck on transforming results of a database query into javascript array. Any guidance will be greatly appreciated. Here are the details:
This is a part of the script that is workin now (with some help from others!):
[code=php]for ($i = 0; ($i < count($pID)); $i++) {
$result=mysql_query(“select * from $table where ClientID= ‘$pID[$i]’ ;”)
or die (“Unable to connect to database “);
while ($row = mysql_fetch_array($result)) {
$arrno1[] = $row[‘NUMBER1’];
$arrno2[] = $row[‘NUMBER2’];
$arrtx[] = $row[‘NAME’];
}
$arr1 = implode( ‘,’ , $arrno1 );
$arr2 = implode( ‘,’ , $arrno2 );
$arr3 = implode( ”,” , $arrtx );
}
echo “var a = [ $arr1 ];”;
echo “var b = [ $arr2 ];”;
echo “var c = [‘$arr3’];”;
The above script generates the following output in html:
var a = [ 100,158];var b = [ 20.50,40.88];var c = [‘string 1′,’string 2’];
My questions are as follows:
1.
Is there a better way to add ‘ ‘ to individual values in the array ? If the array is empy I end up with code like this var c=[ ‘ ‘]; (I need to have elements in this array as strings otherwise the rest of my script doesn’t work…)
How to combine values from $row and some html BEFORE making them into an array? For example:
This is what prints rows of linked text in html (ie based on the values extracted from the database, iterated by the number of rows in $result):
echo “<font face=”Arial, Helvetica, sans-serif” size=”-1″><a href=”javascript:getit(‘”.$row[Name] .”‘)”>”.$row[Description].”</a></font><br>”;
But I want to uptut the above as individual elements in the array to print in html as follows:
var c = [ ‘<font face=”Arial, Helvetica, sans-serif” size=”-1″><a href=”javascript:getit(‘Name1′)”>Description 1</a></font><br>’, ‘<font face=”Arial, Helvetica, sans-serif” size=”-1″><a href=”javascript:getit(‘Name2′)”>Description 2</a></font><br>’]
(not the most efficient and “recommended” way of formatting html and using javascript, I know, but just for the sake of working out this example let’s assume it’s ok).
Thanks