Link to home
Start Free TrialLog in
Avatar of mrhparis
mrhparis

asked on

Can't get PHP variable to print properly in jQuery argument of my custom Drupal theme

I am trying to add dynamic PHP variables for the paths in a piece of jQuery swap-image code. The code works perfectly well -- meaning the javascript works -- when I use static paths. But when I add the PHP to make it dynamic, it won't work. Yet, the code seems to print properly -- i.e. just like the static code -- when inspected in Firebug.

BACKGROUND:
This my first Drupal site. I'm using Views to build the SQL query and handle the CMS heavy lifting. But I'm building my own custom theme. Again, when I use static paths in the .tpl.php file, it works fine in the Drupal environment. It's when I add the dynamic PHP that things seem to break. I'm wondering if this is some Drupal thing that I don't understand. My PHP and jQuery work fine on a non-Drupal setup.

CODE:
I've attached the static code that does work. Then one of many attempts at including the PHP that doesn't seem to work. I've also include the HTML output from Firebug of the attempt at including the PHP. And THAT code looks like it SHOULD work! What's Drupal doin' here?!

PLEASE HELP! I've spent five hours on this and am at my wit's end!
Thanks!

This is the STATIC code that works fine: (This is in a block-level .tpl.php file) 
<?php 
$path = $fields['field_artphoto_fid']->content; 
$pur_path = strip_tags($path);
$node_id = $row->nid; 
?> 

<a href="/node/<? print $node_id ?>"><img id="Thumb_1" class="swapImageDisjoint { sin: ['#main:sites/default/files/imagecache/thumbnail_square/images/city_clock.jpg'], sout: ['#main:sites/default/files/imagecache/thumbnail_square/images/city_clock.jpg'] }" src="<? print $pur_path ?>" /></a> 
Here's one of MANY attempts to add the PHP that DOESN'T work: 
<?php 
$path = $fields['field_artphoto_fid']->content; 
$pur_path = strip_tags($path);
$node_id = $row->nid;  
?> 

<a href="/node/<? print $node_id ?>"><img id="Thumb_1" class="swapImageDisjoint { sin: ['#main:<? print $pur_path ?>'], sout: ['#main:<? print $pur_path ?>'] }" src="<? print $pur_path ?>" /></a> 

YET, here's the HTML output from Firebug using the second set of code above: 
<img id="Thumb_1" class="swapImageDisjoint { sin: ['#main: sites/default/files/imagecache/thumbnail_square/images/city_clock.jpg '], sout: ['#main: sites/default/files/imagecache/thumbnail_square/images/city_clock.jpg '] }" src="sites/default/files/imagecache/thumbnail_square/images/city_clock.jpg"/>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrhparis
mrhparis

ASKER

THANK YOU!
I wish EE would let me give more than 500 points for this one, it had me SO flustered.

The 'trim' command did the trick. (And I switched my 'print' statements to 'echo' and added php after the ?).

I don't know why the extra space was getting added, or why it only caused a problem in my Drupal environment, and not in my PHP mock-up that I used to build my theme.
But your 'trim' command fixed it!

Thanks again.
B-) Glad it worked! Thx for the grade and points!