Results 1 to 10 of 10

Thread: Modifying php/javascript to pull from database

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Modifying php/javascript to pull from database

    1) Script Title: Ultimate Fade-in slideshow (v1.51)

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...nslideshow.htm

    I located here: http://www.dynamicdrive.com/forums/s...ad.php?t=10016 a php script written to grab all available photos in a directory. Thank you
    Twey! The following works perfectly.

    PHP Code:
    <?php
    // getpics.php
    $directory "slideshow"// Directory in which the images are stored, relative to the location of this script. Must have the slash.

    header("Content-Type: text/javascript");
    $dir opendir($directory);
    for(
    $i=0;$file readdir($dir);$i++)
    if(
    strpos($file".") === 0) {
      
    $i--;
      continue;
    } else echo(
    "fadeimages[$i]=['$directory/$file', 'http://example.com/products.php', ''];\n");
    ?>
    My question is that I have been try to get this to pull from a mySQL table, and I can't seem to get it to work.

    PHP Code:
    <?php
    $db_username 
    "I connect to db ok";  // Your database login username
    $db_password "I connect to db ok";  // Your database login password
    $db_name "I connect to db ok";      // The name of the database you wish to use
    $db_host "I connect to db ok";   // The address of the database. Often this is localhost, but may be for example db.yoursite.com

    $tbl_name "slideshow"// The name of your table in the database
    $fadebgcolor "black"// The color you would like to display in slideshow background - #000000 will also work

     
    $dbh=mysql_connect($db_host$db_username$db_password) or die ('You need to set your database connection in this file.</td></tr></table></body></html>');
    mysql_select_db($db_name) or die ('You need to set your database connection in this file.</td></tr></table></body></html>');

    //display all the items from database

    $query  "SELECT * FROM $tbl_name ORDER BY id";
    $result mysql_query($query);

    //while(list($id,$image,$link)= mysql_fetch_row($result))
    //{
    $id=$r["id"];
    $image=$r["image"];
    $link=$r["link"];
          
    for( 
    $i 0$i $row $r=mysql_fetch_array($query); $i++)
    {
    header("Content-Type: text/javascript");

    echo 
    "fadeimages[".$row["id"]."]=['".$row["image"]."', '".$row["link"]."', ''];\n";
    }
    ?>
    The reason for the database is so that the enduser can use an update page to change the links and images if they sell a product that is featured in the fading slideshow. I've tried the "for" and "while", but I must be overlooking something. I would greatly appreciate if someone had a suggestion/solution.

    Thank you

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    PHP Code:
    <?php
    $db_username 
    'I connect to db ok';  // Your database login username
    $db_password 'I connect to db ok';  // Your database login password
    $db_name 'I connect to db ok';      // The name of the database you wish to use
    $db_host 'I connect to db ok';   // The address of the database. Often this is localhost, but may be for example db.yoursite.com

    $tbl_name 'slideshow'// The name of your table in the database
     
    $dbh=mysql_connect($db_host$db_username$db_password) or die ('You need to set your database connection in this file.</td></tr></table></body></html>');
    mysql_select_db($db_name) or die ('You need to set your database connection in this file.</td></tr></table></body></html>');

    //display all the items from database

    $query  'SELECT * FROM $tbl_name ORDER BY id';
    $result mysql_query($query);

    header('Content-Type: text/javascript');

    $count 0;

    while(
    $q mysql_fetch_array($result)) {
     echo 
    "fadeimages[".$count."]=['".$q['image']."', '".$q['link']."', ''];\n";

    $count++;
    }
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you for the fast reply thetestingsite

    I used your code from $query on down, but the slideshow didn't appear. No php parse errors or anything so we are good there. I also double checked the database itself to make sure that update.php was working correctly to update the db and it was.

    Just to verify that all paths were correct and to try to narrow it down that it might be an issue with getpics.php I manually added the photos/links into the javascript...

    fadeimages[0]=["http://example.com/images/W013.jpg", "http://example.com/proddetail.php?prod=W013", ""]

    and that worked like it should. I am using full paths in the db like above with $id $image $link as the vars. I also tried a few other things but was unsuccessful. Any ideas? Thank you

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Can you post a link to a test page with the code on it so that I can examine it further.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Here you go
    http://winnersbred.com/slideshow/

    Thank you

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Apparently getpics.php is not returning any results. I'll look at the code again and see if I can see if I can find anything missing, but it may take a little while as I am still at work at the moment.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    No problem, I appreciate the help so far. I'll continue to look at things as well.

  8. #8
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    The great thing about PHP is that most of the time there are multiple ways of doing things. I found a solution that may not be the best, but it seems to work with no lag time. Below is the code so if anyone else needs anything like this they can use it.

    getpics.php (viewing it alone outputs all the items just like you would see if you added them in manually to your page.
    PHP Code:
    <?php
    $host
    ="your info"// Host name
    $username="your info"// Mysql username
    $password="your info"// Mysql password
    $db_name="your info"// Database name
    $tbl_name="your info"// Table name

    // Connect to server and select database.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // Retrieve data from database
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    header("Content-Type: text/javascript");
    // Start looping rows in mysql database.
    while($rows=mysql_fetch_array($result)){
    ?>
    fadeimages[<? echo $rows['id']; ?>]=["<? echo $rows['image']; ?>", "<? echo $rows['link']; ?>", ""]
    <?
    // close while loop
    }

    // close connection
    mysql_close();
    ?>
    In your page where the javascript is located add...
    <?php include("getpics.php"); ?> where your links are to appear. When you view the source it will look like you manually added everything in. If I get a chance I might try to go back to see if your original code does the same.

    I really appreciate the fast replies, and this seems like a great community!

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Glad to see you found a solution, and thanks for posting it so that others may benefit from it.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Feb 2008
    Posts
    137
    Thanks
    18
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by ?foru View Post
    In your page where the javascript is located add...
    <?php include("getpics.php"); ?>
    Just an update here;
    PHP Code:
    // [B]here you add your Connect to host and select database. [/B]

    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 

    // Retrieve data from database 
    $sql="SELECT image FROM $tbl_name"//changed to include one field this part is so COOL!
    // You can allow for restriction like [ICODE]SELECT image FROM $tbl_name WHERE side = 'left'[/ICODE]
    $result=mysql_query($sql); 

    //header("Content-Type: text/javascript"); //closed, it's not needed see below
    // Start looping rows in mysql database. 

    $i 0//added this

    while($rows=mysql_fetch_array($result)){ 
    echo(
    "fadeimages[$i]=['".$rows[image]."', '', ''];\n"); //changed this line

    $i++; //added this

    // close while loop 


    // close connection 
    mysql_close(); 
    Now to include this you add this to a page, called random.php
    Code:
    <script type="text/javascript">
    <!--
    var fadeimages=new Array()
    -->
    </script>
    <script src="getpics.php" type="text/javascript"></script>
    <script type="text/javascript">
    <!--
    var fadebgcolor="#9FCBF0"
    
    /***********************************************
    * Ultimate Fade-In Slideshow (v1.51): © Dynamic Drive (http://www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/
    
    ////NO need to edit beyond here/////////////
    
    rest of code...
    Cheers
    Last edited by student101; 01-13-2009 at 07:50 AM.
    ASCII stupid question, get a stupid ANSI!
    Beta is Latin for still doesn’t work.
    Mac users swear by their Mac, PC users swear at their PC.
    Keyboard not found...Press any key to continue.

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
  •