Log in

View Full Version : Help! - PHP inside JS to create dynamic content



p15
05-16-2011, 08:20 PM
1) Script Title: DD Drop Down Panel

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

. $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.



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:



<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();

} ?>

traq
05-16-2011, 11:26 PM
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.

p15
05-17-2011, 10:10 PM
Thanks, but how can i use php to customise the javascript?

traq
05-18-2011, 01:17 AM
very generally,


$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.