回答php字符串作为javascript函数的问题

时间:2011-07-26 17:13:39

标签: php javascript iphone wordpress

我正在使用Jaipho从自定义Wordpress插件向移动图库显示图像。使用Jaipho画廊的wordpress主题使用WP-mobile-detector插件显示。

我遇到的问题是当我使用php收集照片的URL以回应javascript要解析的函数时。我从Safari的元素检查器中获取了生成的静态javascript代码,并将其粘贴到我的代码中,注释掉了php,它可以在任何地方使用。 Safari for iOS似乎不喜欢php生成的javascript代码。

  • HTML 5
    • <DOCTYPE html>
    • <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    • <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  • PHP 5.2.6
  • Wordpress 3.2.1

什么时候有效:

  • 用户代理在Safari上设置为iPhone
  • 静态代码取代了php生成的代码

    $imageArray = $case->images_assc_array();  
    $i = 0;  
    foreach($imageArray['views'] as $view_name => $view_images) {  
       $before_img = $view_images['before'];  
       $after_img = $view_images['after'];  
    
       echo "dao.ReadImage($i,'".$before_img->medium_size()."','".$before_img->small_size()."','".ucfirst($view_name)." Before','".$case->description."');";  
       $i++;  
       echo "dao.ReadImage($i,'".$after_img->medium_size()."','".$after_img->small_size()."','".ucfirst($view_name)." After','".$case->description."');";  
       $i++;  
    }
    

预期示例生成输出:

    dao.ReadImage( 0,'/wp-content/uploads/rmgallery_images/medium/408/before-front.jpg','/wp-content/uploads/rmgallery_images/small/408/before-front.jpg','Front Before','38 year old who underwent a tummy tuck.');  
    dao.ReadImage( 1,'/wp-content/uploads/rmgallery_images/medium/410/after-front.jpg','/wp-content/uploads/rmgallery_images/small/410/after-front.jpg','Front After','38 year old who underwent a tummy tuck.');
    dao.ReadImage( 2,'/wp-content/uploads/rmgallery_images/medium/409/before-side.jpg','/wp-content/uploads/rmgallery_images/small/409/before-side.jpg','Side Before','38 year old who underwent a tummy tuck.');
    dao.ReadImage( 3,'/wp-content/uploads/rmgallery_images/medium/411/after-side.jpg','/wp-content/uploads/rmgallery_images/small/411/after-side.jpg','Side After','38 year old who underwent a tummy tuck.');

3 个答案:

答案 0 :(得分:2)

你有一些不匹配的引号:

echo dao.ReadImage($i,'".$before...

echo "dao.ReadImage($i,'".$after...

等等。

试试这些:

echo 'dao.ReadImage('.$i.',"'.$before_img->medium_size().'","'.$before_img->small_size().'","'.ucfirst($view_name).' Before","'.$case->description.'");';
echo 'dao.ReadImage('.$i.',"'.$after_img->medium_size().'","'.$after_img->small_size().'","'.ucfirst($view_name).' After","'.$case->description.'");';

答案 1 :(得分:2)

归功于@Marc B和@ linuxrules94的综合解决方案:

<?php
$imageArray = $case->images_assc_array();
$i = 0;

    foreach($imageArray['views'] as $view_name => $view_images):
        $before_img = $view_images['before'];
        $after_img = $view_images['after'];
    ?>

        dao.ReadImage(<?=json_encode($i);?>, <?=json_encode($before_img->medium_size());?>,<?=json_encode($before_img->small_size());?>,<?=json_encode(ucfirst($view_name));?> + " Before", <?=json_encode(stripslashes($case->description));?>);
        <? $i++; ?>
        dao.ReadImage(<?=json_encode($i);?>, <?=json_encode($after_img->medium_size());?>,<?=json_encode($after_img->small_size());?>,<?=json_encode(ucfirst($view_name));?> + " After", <?=json_encode(stripslashes($case->description));?>);
        <? $i++;
    endforeach; ?>

谢谢大家!

答案 2 :(得分:1)