Jump to content

[SOLVED] Return array problem from Mysql db


captdavid

Recommended Posts

Have been trying to hack an existing javascript scroller to display items from a mysql db using php. I know just enough php to be dangerous (but I'm learning) Anyway, here is the situation. 

dbsetup : One table with 8 rows, 3 fields per row, "id" "content" "status" 

Script Function: Contect to db, Grab "content" from all rows where "status"=active, append this to some std javascript calls in a given format and make it available to the rest of the javascript.

Problem: The javascript requires the data being pulled from the db to be in the following format:

 

ITEMS = [ {'file': '','content': 'Some Content','pause_b': 2,'pause_a': 0},

{'file': '','content': 'Some other Content, 'pause_b': 2,'pause_a': 0}]

Notice there is No Comma after the last item before the closing bracket.

 

The output of my php looks like this (Typical):

ITEMS = [ {'file': '','content': 'Some Content', 'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Some Other Content', 'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Some Other Content Again', 'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Yet Some More Content', 'pause_b': 2,'pause_a': 0},]

 

Notice the comma after the last item.....

 

Rest of the php works fine, and as a matter of fact the scroller javascript works with this output in FF but unfortunately IE hangs with the last comma. Here is the php I am using now.

 

<?PHP
$host = 'mydbhost';
$user = 'iusername';
$pass = 'pswd';
$name = 'dbname';
$cnn = mysql_connect($host, $user, $pass) or die ('Error connecting to mysql');
mysql_select_db($name);
$query = "SELECT * FROM ts WHERE status='active' ORDER BY id_num ASC LIMIT 8";
$result = mysql_query($query) or die('Error');
$num_rows = mysql_num_rows($result);

?>

var LOOK = {
    // scroller box size: [width, height]
    'size': [120, 110]
},

BEHAVE = {
    // autoscroll - true, on-demand - false
    'auto': true,
    // vertical - true, horizontal - false
    'vertical': true,
    // scrolling speed, pixels per 40 milliseconds;
    // for auto mode use negative value to reverse scrolling direction
    'speed': 2
},

ITEMS = [
<?php
while($row = mysql_fetch_object ( $result ) ){
if($i == $num_rows){
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0}";
}else{
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0},";
$i++;
}
}
mysql_free_result($result);
mysql_close();
?>]

 

Problem is how to eliminate the last comma while leaving the rest? Think it should be simple, but like the jitterbug, it plumb evaded me.  ???

 

TIA

David ONeill

ITEMS = [
<?php
while($row = mysql_fetch_object ( $result ) ){
   $res[] = "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0}";
}
echo(implode(",",$res));

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

 

You can use implode to easily add the comma between rows. $res[] means to append this string to the $res array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.