Results 1 to 4 of 4

Thread: Help! - PHP inside JS to create dynamic content

  1. #1
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! - PHP inside JS to create dynamic content

    1) Script Title: DD Drop Down Panel

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

    Right i have been trying to get help for this in a few places but so far no one seems to want to help. I'm trying to use this in a dynamic context within wordpress. what i want to do is get the "Panel", "content" and "tab" id's to automatically generate based on a php variable. In this case it is
    Code:
    . $category->category_nicename.
    I have tried a number of variations but to no effect. I have renamed dropdownpanel.js as dropdownpanel.php.

    I'm pretty sure it can be done but I'm just not sure exactly how to get the JS and PHP to interact.

    Code:
    var defaultpanel=new ddpanel({
    	ids: ["panel", "content", "tab"], // id of main panel DIV, content DIV, and tab DIV
    	stateconfig: {initial: "0px", persiststate: true}, // initial: initial reveal amount in pixels (ie: 5px)
    	animate: {enabled: true, steps: 5}, //steps: number of animation steps. Int between 1-20. Smaller=faster.
    	pointerimage: {enabled: true, src: ["arrow-down.gif", "arrow-up.gif"]},
    	closepanelonclick: {enabled: true} // close panel when links or elements with CSS class="closepanel" within container is clicked on?
    })
    Additional information if it helps.

    The code that generates the panel, content and tab is:

    Code:
    <div id="panelnav">
    <?php 
    $args=array(
      'orderby' => 'id',
      'order' => 'DESC'
      );
    $categories=get_categories('child_of=54');
    foreach ($categories as $category) {
    echo '<div id="'. $category->category_nicename.'tab" class="ddpaneltab left col_3"> 
    <a href="#"><h3>' . $category->category_nicename. '</h3></a></div>';} ?>
    </div>
    
    <div class="clear"></div>
    
    <?php 
    $args=array(
      'orderby' => 'id',
      'order' => 'DESC'
      );
    $categories=get_categories('child_of=54');
    foreach ($categories as $category) {
    echo '<div id="'. $category->category_nicename.'panel" class="ddpanel">
    <div id="'. $category->category_nicename.'content" class="ddpanelcontent">';
    
    $query = new WP_Query( 'cat='.$category->cat_ID );
    
    while ( $query->have_posts() ) : $query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    echo '</div>
    </div>';
    
    wp_reset_postdata();
    
    } ?>

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

    Default

    javascript and php DO NOT interact. php works on the server, and javascript works on the user's computer.

    you can use php to customize your javascript, however. Do not rename the javascript file with a php extension. You would need to use php to insert the customized values you want, and then serve the modified script.

    I don't know much about wordpress, so I couldn't help you with specifics. You would probably have better luck on a wordpress forum.

  3. #3
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, but how can i use php to customise the javascript?

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

    Default

    very generally,
    PHP Code:
    $myalert 'Hello World!';
    $script '<script> alert("'$myalert .'"); </script>';
    print 
    $script
    in this example, php outputs <script> alert("Hello World!"); </script> to the browser. At this point, php is done and the browser takes over. It parses the javascript and shows the alert.

    As I said, they can't really interact. PHP's job is to write html (or javascript, in this case) and output it to the webpage.

    The reason I suggetsed asking on a wordpress forum is that WP has it's own structure and php functions which are not necessarily well understood by people (like me) who don't code for wordpress. For example, I can imagine that the function $query->have_posts() mentioned in your code finds posts associated with whatever article you're currently working on. However, I don't know what parameters it requires or accepts, how it finds posts, or what value(s) it returns. People who frequent a wp-specific forum would be far more likely to have sound advice about how to modify it.

Tags for this Thread

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
  •