PDA

View Full Version : Fetch data from database



salman1karim
10-24-2013, 05:08 AM
1) Script Title: Fading scroller

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex2/fadescroll.htm

3) Describe problem: In this script we define the variable for the displaying the text in the scroller. But if i want to fetch the text from the database than whats the possible way? for database i want to use mysql + and for scripting PHP.

traq
10-24-2013, 05:46 AM
Do you have a database already? Is it just a static list of messages? or do you want to vary them?

salman1karim
10-24-2013, 06:22 AM
Yes, database is ready and that is static list of messages

traq
10-24-2013, 09:53 PM
You'll have to retrieve the content from your DB, and then use it to write your JS array. This is a little bit hacky, but part of the problem is how the js is written - it's in the global scope, and some functionality is tacked on, etc.. This will work.

It will depend on your schema, but selecting the data will look something like
<?php
# connect to your database
$DB = new mysqli( 'your-database-host','username','password','table-name' );

# get the content of each of your messages
$result = $DB->query( "SELECT `content` FROM `message`" );
if( $result ){
# loop over results
while( $row = $result->fetch_row() ){
# write each item for your js array
$messages[] = addslashes( $row[0] );
}
# then, combine those items into a JS array
$js_array = "var fcontent = ['". implode( "','",$messages ) ."'];";
# note, this is a string representation of a javascript statement.
# it'll look a bit like this:
/*
var fcontent = ['first message','another message','and so forth'];
*/
# ...which is exactly what we need for the javascript.
# two "gotchas":
# 1) this will need to be output to the document _before_ you call the fadingscroller script (otherwise, it "won't exist yet" when the page tries to use it).
# 2) in the fadingscroller script, you need to _remove_ the lines where `fcontent` is defined (otherwise, our fcontent will be overwritten).
}
else{
/* the query failed, do something else */
}

All in all, not that elegant. But the alternative is to rewrite the script (which certainly could [probably should] be done, but I don't know if you want to get into that right now. I'll tinker with it in my free time).

salman1karim
11-04-2013, 12:05 PM
I resolved the issue thanks for your help.

traq
11-04-2013, 06:59 PM
You're very welcome :)

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].