Advanced Search

Results 1 to 6 of 6

Thread: Fetch data from database

  1. #1
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Fetch data from database

    1) Script Title: Fading scroller

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...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.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Do you have a database already? Is it just a static list of messages? or do you want to vary them?
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Yes, database is ready and that is static list of messages

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 Code:
    <?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).
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  5. The Following User Says Thank You to traq For This Useful Post:

    salman1karim (11-04-2013)

  6. #5
    Join Date
    Sep 2011
    Posts
    35
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    I resolved the issue thanks for your help.

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,624
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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].
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

Similar Threads

  1. Get data from Database instead xml
    By F0u4d in forum PHP
    Replies: 0
    Last Post: 05-31-2012, 04:02 PM
  2. Replies: 0
    Last Post: 11-28-2010, 07:25 PM
  3. [Ajax] Fetch data from .txt
    By sysout in forum JavaScript
    Replies: 0
    Last Post: 04-17-2009, 09:16 AM
  4. get data from only one database
    By Marcus-Hill in forum MySQL and other databases
    Replies: 3
    Last Post: 04-04-2008, 10:04 PM
  5. display data from database
    By avneet in forum PHP
    Replies: 1
    Last Post: 03-10-2008, 05:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •