Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Needageek to unravel a newbies problem
Old 04-08-2008, 01:46 AM Needageek to unravel a newbies problem
Junior Talker

Posts: 4
Name: David ONeill
Trades: 0
Have a javascript I am hacking to inject some dynamic data from a mysql db
got it all working with one exception I can't figure how to get around, and I figure someone here will go " Well duh" I don't mind, I know just enough php to be dangerous :-) Here we go..

Got a mysql db table named "ts"
Consists of 8 rows with fields id,content,status

Everything works with one problem, here is output of typical scroller.tpl0.php

Code:
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 = [ {'file': '','content': 'Thank you for stopping
 by..... Check back soon','pause_b': 2,'pause_a': 0},{'file': '','content': 'We
 Are Under Construction Your Mileage May Vary','pause_b': 2,'pause_a':
 0},{'file': '','content': 'Downtown Savannah Store Now Open 
14 West State Street','pause_b': 2,'pause_a': 0},{'file': '','content': 'Tips
 and tricks from our professionals','pause_b': 2,'pause_a': 0},{'file':
 '','content': 'Members only area with chat and forums','pause_b':
 2,'pause_a': 0},{'file': '','content': 'Live WebCam from our new downtown
 location','pause_b': 2,'pause_a': 0},]
The problem is the comma just before the closing bracket in ITEMS..
it needs to go away while the commas between the rest of the array stay.

current code to generate the above follows:

Code:
<?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();
?>]
Suggestions?

TIA
Capt David ONeill
captdavid is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-08-2008, 07:56 AM Re: Needageek to unravel a newbies problem
KML9870's Avatar
One Bad Mamma :-)

Posts: 5,489
Name: Kandi
Location: Western NY
Trades: 0
You have a comma at the end of your echo:

Quote:
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0},";
$i++;
__________________
~~Kandi~~

Please login or register to view this content. Registration is FREE


KML9870 is offline
Reply With Quote
View Public Profile
 
Old 04-08-2008, 08:32 PM Re: Needageek to unravel a newbies problem
Junior Talker

Posts: 4
Name: David ONeill
Trades: 0
Quote:
You have a comma at the end of your echo:
Yeah, the problem is that their needs to be a comma between the items in the array

Code:
ITEMS = [ {'file': '','content': 'Downtown Savannah Store Now Open 
14 West State Street','pause_b': 2,'pause_a': 0},

{'file': '','content': 'Tips and tricks from our professionals',
'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Members only area with chat and forums',
'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Live WebCam from our new downtown location',
'pause_b': 2,'pause_a': 0},]
What I need to do is loose the comma after the last item just before the closing bracket Ie:
Not : 'pause_b': 2,'pause_a': 0},]
But Rather: 'pause_b': 2,'pause_a': 0}]

Thats what I don't know how to do..

Suggestions?
captdavid is offline
Reply With Quote
View Public Profile
 
Old 04-08-2008, 10:29 PM Re: Needageek to unravel a newbies problem
KML9870's Avatar
One Bad Mamma :-)

Posts: 5,489
Name: Kandi
Location: Western NY
Trades: 0
I think the easiest way to do it will be to split it into two parts - one to deal with the results greater than 1. Then echo the last result by itself without the last comma.
__________________
~~Kandi~~

Please login or register to view this content. Registration is FREE


KML9870 is offline
Reply With Quote
View Public Profile
 
Old 04-08-2008, 10:51 PM Re: Needageek to unravel a newbies problem (problem solved)
Junior Talker

Posts: 4
Name: David ONeill
Trades: 0
Problem Solved

New Code:
Code:
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.

Tks to "UNIFLARE" at PHPFreaks.com
captdavid is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Needageek to unravel a newbies problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19572 seconds with 12 queries