本文介绍了调用PHP函数两次,只能使用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用PHP关联数组将同一页面上的jQuery幻灯片的不同实例的文本和图像的不同值回显为HTML。这里是HTML:

 < div class ='slide'> 
< div class ='mosaic-block fade'>
< div class ='mosaic-overlay'>
< div class ='text>< p>这是文字!< / p>< / div>
< / div>
< div class ='mosaic-image'>< img src ='imgs / the-img.png'/>< / div>
< / div> <! - 马赛克块淡化 - >
< / div> <! - .slide - > `b




我为每个幻灯片类型写了数组,包含每个幻灯片的文本和图像,这里是例子:

  $ my_content = array(
'image1.png'=>'image1 text!',
'image2.png'=>'image2 text!'
);

然后我写了一个函数,其中包含幻灯片类别和内容的参数:

  function gallery_content($ content){
foreach($ content as $ img => $ txt){
echo < div class ='slide'>
< div class ='mosaic-block fade'>
< div class ='mosaic-overlay>
< div < / div>
< div class ='mosaic-image'>< p>< p> img src ='imgs / other /\".$ img。'/>< / div>
< / div><! - 镶嵌块淡化 - >
< ; / div><! - .slide - >;



$ b我称之为: gallery_content($ my_content ); ,它工作得很好。但是当我尝试再次调用另一组值时,只有第一个实例似乎工作。我试图直接使用数组而不是变量作为参数,并为每个幻灯片,使不同的功能,但不断获得相同的结果。


为什么函数不能被多次调用?感谢您的提前。 请参阅最终页面(HTML)上生成的源代码来判断PHP是否工作或不。我认为你的结果高级(DIV)可能具有相同的ID或其他属性,因此jQuery激活它们,所以只有一个正在正确运行(第一个),第二个不运行。 / p>

希望有帮助。


I am trying to use PHP associative arrays to echo different values for text and images into HTML for different instances of a jQuery slideshow on the same page. Here's the HTML:

<div class='slide'>
    <div class='mosaic-block fade'>
        <div class='mosaic-overlay'>
        <div class='text'><p>This is the text!</p></div>
            </div>
        <div class='mosaic-image'><img src='imgs/the-img.png'/></div>
    </div> <!-- mosaic-block fade -->
</div> <!-- .slide --> `

I wrote arrays for each type of slideshow containing the text and image for each slide, here's and example:

$my_content = array(
        'image1.png' => 'image1 text!',
        'image2.png' => 'image2 text!'
        );

Then I wrote a function with parameters for the category of slideshow and the content:

function gallery_content($content) {
    foreach ( $content as $img => $txt ) {
    echo "<div class='slide'>
        <div class='mosaic-block fade'>
        <div class='mosaic-overlay'>
            <div class='text'><p>".$txt."</p></div></div>
            <div class='mosaic-image'><img src='imgs/other/".$img."'/></div>
        </div> <!-- mosaic-block fade -->
        </div> <!-- .slide --> ";
}

I call it like this: gallery_content($my_content); and it works really well. But when I try to call it again for another set of values, only the first instance seems to work. I tried using the array directly instead of the variable as a parameter AND making a separate function for each slideshow, but keep getting the same results.

Why can't the function be called more than once? Thanks in advance.

解决方案

See the source code that is generated on your final page(HTML) to judge whether PHP did its work or not. i think thats your resultant "galaries"(DIV) might be having the same ID or other attributes thorugh which the jQuery activates them, and so only one is being run properly (the first one), and the second one is not run.

Hope that helps.

这篇关于调用PHP函数两次,只能使用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:49