Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Question JQuery: how do i load php dynamic content into a div?

    Hi, I'm creating a page listing videos taken from a Youtube channel using the youtube api and using php's simple xml. I have 2 pages, one that lists the videos with their thumbnails, and another displaying the videos comments when the user clicks on the more info link. But at the moment this more link 'link' takes me to another page. I would like it to display in a div called comments when the user clicks the more info link.

    Heres my code:

    test1.php (lists videos)
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
        div.item {
          border-top: solid black 1px;      
          margin: 10px; 
          padding: 2px; 
          width: auto;
          padding-bottom: 20px;
        }
        span.thumbnail {
          float: left;
          margin-right: 20px;
          padding: 2px;
          border: solid silver 1px;  
          font-size: x-small; 
          text-align: center
        }    
        span.attr {
          font-weight: bolder;  
        }
        span.title {
          font-weight: bolder;  
          font-size: x-large
        }
        img {
          border: 0px;  
        }    
        a {
          color: brown; 
          text-decoration: none;  
        }
    </style>
    <script src="js/jquery-1.4.js" type="text/javascript"></script>
    <script type="text/javascript">
    </script>
    </head>

    <body>
    <?php
        
    // set feed URL
        
    $feedURL 'http://gdata.youtube.com/feeds/api/users/timesplitta/uploads';
        
        
    // read feed into SimpleXML object
        
    $sxml simplexml_load_file($feedURL);
        
    ?>
          <h1>Videos</h1>
        <?php
        
    // iterate over entries in feed
        
    foreach ($sxml->entry as $entry) {
          
    // get nodes in media: namespace for media information
          
    $media $entry->children('http://search.yahoo.com/mrss/');
          
          
    // get video player URL
          
    $attrs $media->group->player->attributes();
          
    $watch $attrs['url']; 
          
          
    // get video thumbnail
          
    $attrs $media->group->thumbnail[0]->attributes();
          
    $thumbnail $attrs['url']; 
                
          
    // get <yt:duration> node for video length
          
    $yt $media->children('http://gdata.youtube.com/schemas/2007');
          
    $attrs $yt->duration->attributes();
          
    $length $attrs['seconds']; 
          
          
    // get <yt:stats> node for viewer statistics
          
    $yt $entry->children('http://gdata.youtube.com/schemas/2007');
          
    $attrs $yt->statistics->attributes();
          
    $viewCount $attrs['viewCount']; 
          
          
    // get <gd:rating> node for video ratings
          
    $gd $entry->children('http://schemas.google.com/g/2005'); 
          if (
    $gd->rating) {
            
    $attrs $gd->rating->attributes();
            
    $rating $attrs['average'];
            
    $numRaters $attrs['numRaters'];
          } else {
            
    $rating 0
            
    $numRaters 0;
          }
          
          
    $gd $entry->children('http://schemas.google.com/g/2005');
          if (
    $gd->comments->feedLink) { 
            
    $attrs $gd->comments->feedLink->attributes();
            
    $countHint $attrs['countHint']; 
          }
          
          
    $arr explode('/',$entry->id);
          
    $id $arr[count($arr)-1];
          
    ?>
          <div class="item">
            <span class="title">
              <a href="<?php echo $watch?>"><?php echo $media->group->title?></a>
            </span>
            <p><?php echo $media->group->description?></p>
            <p>
              <span class="thumbnail">
                <a href="<?php echo $watch?>"><img src="<?php echo $thumbnail;?>" /></a>
                <br/>click to view
              </span> 
              <span class="attr">Keywords:</span> <?php echo $media->group->keywords?><br />
              <span class="attr">By:</span> <?php echo $entry->author->name?> <br/>
              <span class="attr">Duration:</span> <?php printf('%0.2f'$length/60); ?> 
              min. <br/>
              <span class="attr">Views:</span> <?php echo $viewCount?> <br/>
              <span class="attr">Rating:</span> <?php echo $rating?><br />
              <span class="attr">Number of Raters:</span> <?php echo $numRaters?>
              <span class="attr">Uploaded:</span> <?php echo date('d F Y H:i',strtotime($entry->published)); ?><br />
              <a href="test2.php?id=<?php echo $id;?>" class="moreinfo">More Info</a> <span class="attr">Number of comments:</span> <?php echo $countHint?>
            </p>
            <div id="comments"></div>
          </div>      
        <?php
        
    }
        
    ?>
    </body>
    </html>
    test2.php (lists the comments)

    PHP Code:
    <?php
        
    // function to parse a video <entry>
        
    function parseVideoEntry($entry) {      
          
    $obj= new stdClass;
          
          
    // get nodes in media: namespace for media information
          
    $media $entry->children('http://search.yahoo.com/mrss/');
          
    $obj->title $media->group->title;
          
    $obj->description $media->group->description;
          
          
    // get video player URL
          
    $attrs $media->group->player->attributes();
          
    $obj->watchURL $attrs['url']; 
          
          
    // get video thumbnail
          
    $attrs $media->group->thumbnail[0]->attributes();
          
    $obj->thumbnailURL $attrs['url']; 
                
          
    // get <yt:duration> node for video length
          
    $yt $media->children('http://gdata.youtube.com/schemas/2007');
          
    $attrs $yt->duration->attributes();
          
    $obj->length $attrs['seconds']; 
          
          
    // get <yt:stats> node for viewer statistics
          
    $yt $entry->children('http://gdata.youtube.com/schemas/2007');
          
    $attrs $yt->statistics->attributes();
          
    $obj->viewCount $attrs['viewCount']; 
          
          
    // get <gd:rating> node for video ratings
          
    $gd $entry->children('http://schemas.google.com/g/2005'); 
          if (
    $gd->rating) { 
            
    $attrs $gd->rating->attributes();
            
    $obj->rating $attrs['average']; 
          } else {
            
    $obj->rating 0;         
          }
            
          
    // get <gd:comments> node for video comments
          
    $gd $entry->children('http://schemas.google.com/g/2005');
          if (
    $gd->comments->feedLink) { 
            
    $attrs $gd->comments->feedLink->attributes();
            
    $obj->commentsURL $attrs['href']; 
            
    $obj->commentsCount $attrs['countHint']; 
          }
          
          
    // get feed URL for video responses
          
    $entry->registerXPathNamespace('feed''http://www.w3.org/2005/Atom');
          
    $nodeset $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
          2007#video.responses']"
    ); 
          if (
    count($nodeset) > 0) {
            
    $obj->responsesURL $nodeset[0]['href'];      
          }
             
          
    // get feed URL for related videos
          
    $entry->registerXPathNamespace('feed''http://www.w3.org/2005/Atom');
          
    $nodeset $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
          2007#video.related']"
    ); 
          if (
    count($nodeset) > 0) {
            
    $obj->relatedURL $nodeset[0]['href'];      
          }
        
          
    // return object to caller  
          
    return $obj;      
        }   
        
        
    // get video ID from $_GET 
        
    if (!isset($_GET['id'])) {
          die (
    'ERROR: Missing video ID');  
        } else {
          
    $vid $_GET['id'];
        }
        
        
    // set video data feed URL
        
    $feedURL 'http://gdata.youtube.com/feeds/api/videos/' $vid;

        
    // read feed into SimpleXML object
        
    $entry simplexml_load_file($feedURL);
        
        
    // parse video entry
        
    $video parseVideoEntry($entry);
        
        
    // read 'video comments' feed into SimpleXML object
        // parse and display each comment
        
    if ($video->commentsURL && $video->commentsCount 0) {
          
    $commentsFeed simplexml_load_file($video->commentsURL);    
          echo 
    "<tr><td colspan=\"2\"><h3>" $commentsFeed->title 
          
    "</h3></td></tr>\n";
          echo 
    "<tr><td colspan=\"2\"><ol>\n";
          foreach (
    $commentsFeed->entry as $comment) {
            echo 
    "<li>" $comment->content " by <a href=\"".$comment->author->uri."\"> "$comment->author->name ."</a></li>\n";
          }
          echo 
    "</ol></td></tr>\n";
        }
        
        
    // read 'video responses' feed into SimpleXML object
        // parse and display each video entry
        
    if ($video->responsesURL) {
          
    $responseFeed simplexml_load_file($video->responsesURL);    
          echo 
    "<tr><td colspan=\"2\"><h3>" 
          
    $responseFeed->title "</h3></td></tr>\n";
          foreach (
    $responseFeed->entry as $response) {
            
    $responseVideo parseVideoEntry($response);
            echo 
    "<tr>\n";
            echo 
    "<td><a href=\"{$responseVideo->watchURL}\">
            <img src=\"$responseVideo->thumbnailURL\"/></a></td>\n"
    ;
            echo 
    "<td><a href=\"{$responseVideo->watchURL}\"
            >{$responseVideo->title}</a><br/>\n"
    ;
            echo 
    sprintf("%0.2f"$responseVideo->length/60) . " min. | 
            {$responseVideo->rating} user rating | {$responseVideo->viewCount} 
            views<br/>\n"
    ;
            echo 
    $responseVideo->description "</td>\n";
            echo 
    "</tr>\n";      
          }
        }
        
        
    // read 'related videos' feed into SimpleXML object
        // parse and display each video entry
        
    if ($video->relatedURL) {
          
    $relatedFeed simplexml_load_file($video->relatedURL);    
          echo 
    "<tr><td colspan=\"2\"><h3>" 
          
    $relatedFeed->title "</h3></td></tr>\n";
          foreach (
    $relatedFeed->entry as $related) {
            
    $relatedVideo parseVideoEntry($related);
            echo 
    "<tr>\n";
            echo 
    "<td><a href=\"{$relatedVideo->watchURL}\">
            <img src=\"$relatedVideo->thumbnailURL\"/></a></td>\n"
    ;
            echo 
    "<td><a href=\"{$relatedVideo->watchURL}\">
            {$relatedVideo->title}</a><br/>\n"
    ;
            echo 
    sprintf("%0.2f"$relatedVideo->length/60) . " min. | 
            {$relatedVideo->rating} user rating | {$relatedVideo->viewCount} 
            views<br/>\n"
    ;
            echo 
    $relatedVideo->description "</td>\n";
            echo 
    "</tr>\n";      
          }
        }
        echo 
    "</table>\n";    
        
    ?>
    "The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do."

    Excellent resource for learning PHP here

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,606
    Thanks
    6
    Thanked 997 Times in 970 Posts
    jQuery doesn’t care about PHP, it only cares about the output HTML at the end. Look into the load() or ajax() functions. You can load any HTML from another file into a container on the current page this way.


  •  

    Posting Permissions

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