2011-08-10 48 views
1

我想要使用jQuery的顯示/隱藏腳本上的PHP文件,它是這樣的:PHP的foreach,CSS和jQuery腳本。如何互動?

<?php 
foreach ($empresas as $empresa) { 
echo "<style type='text/css'> 
.$empresa[teaserid] { 
width:100%; 
background-color: #CCC; 
color: #000; 
margin-top:10px; 
border:1px solid #CCC; 
position:relative; 
vertical-align:middle; 
} 
.showhide$empresa[teaserid] { 
    display:none; 
} 
</style>"; 
echo '<script type="text/javascript">'; 
echo ' $(document).ready(function(){ '; 
echo '   $(\".$empresa[teaserid]\").hide(); '; 
echo '   $(\".showhide$empresa[teaserid]\").show(); '; 
echo ' $(\".showhide$empresa[teaserid]\").click(function(){ '; 
echo ' $(\".$empresa[teaserid]\").slideToggle(), 500; '; 
echo ' }); '; 
echo ' });'; 
echo '</script>'; 
echo "<a href='#' class='showhide$empresa[teaserid]'>$empresa[titulo]</a><br /> 
    <div class='$empresa[teaserid]'> 
TEST</div>"; 
} 
?> 

所以,我需要的是在PHP一個foreach呼應新的CSS值和新jQuery腳本。因爲每個DIV需要不同的CSS和jQuery來關聯,並能夠顯示和隱藏其內容。這種迴應沒有奏效。 CSS運行良好,但jQuery不支持PHP $字符串。我能做什麼? 或者還有更簡單的方法來做到這一點?一個僅涉及任何當前div的jQuery函數? 感謝任何人誰可以幫助我在這一個..

+2

ahhhhhhhhhhhhhh! – samccone

+0

即時通訊不是程序員.. :(如你所見... – Aron

+0

其確定,我們都開始在某個地方.. http://tinsology.net/2009/06/client-side-vs-server-side-code/閱讀這個..燈泡將打開然後 – samccone

回答

0

在PHP中,你只能把雙引號字符串變量:

$a = 'Jon'; 
echo "Hi $a"; // Should output 'Hi Jon' 
echo 'Hi $a'; // should output 'Hi $a' 

所以,如果你想在Javascript功能來閱讀:

$(".Jon") // where Jon is the value of $a 

然後在PHP中,你可以這樣輸出:

echo '$(\".' . $a . '\")'; // Notice we are splitting the echo statement into a concatenation of 3 strings 
+1

以及php中的var語句如何處理ya? –

+0

大聲笑哎呀,那應該絕對不會在那裏:) –

+0

好!就像Jon Gu說的那樣工作吧! – Aron

0

有沒有真正的ne編輯讓PHP爲每個項目打印出一個新的CSS和Javascript塊。如果你編寫的CSS和JavaScript是一個好方法,那麼它將全部爲你處理。其他

有一點要記住的是,PHP是在它的最基本的一個模板語言,所以如果你發現自己呼應大塊的代碼,你很可能是有些不妥。相反,像這樣:

<?php 
$variable = "SOme value"; 
echo "Some very long huge string with <html> tags and stuff plus $variables"; 
?> 

你可以做這樣的事情,打開和關閉標籤幾次。

<?php $variable = "SOme value"; ?> 
Some very long huge string with <html> tags and stuff plus <?php print $variables; ?> 

有了這個,這裏是你的代碼的重做版本。 而且,不需要使用硬編碼的ID生成PHP輸出自定義Javascript,您需要的是構建您的JavaScript,以便它不需要知道任何這些東西,就像我已經做的那樣。

<style type='text/css'> 
/* The CSS is the same, so just have one class that is used on all the items. */ 
.empresa-teaser { 
    width:100%; 
    background-color: #CCC; 
    color: #000; 
    margin-top:10px; 
    border:1px solid #CCC; 
    position:relative; 
    vertical-align:middle; 
} 
</style> 

<script type="text/javascript"> 
    $(function(){ 
    // Hide all of the teasers to start. 
    $(".empresa-teaser").hide(); 

    // Add a handler for when the show/hide link is clicked. 
    $(".showhide-empresa-teaser").click(function(){ 

     // When it is clicked, find the next item with the 'empresa-teaser' class 
     // and show it using a sliding toggle effect. 
     $(this).nextAll('.empresa-teaser:first').slideToggle(500); 
    }); 
    }); 
</script> 


<?php foreach ($empresas as $empresa) { ?> 

    <a href='#' class="showhide-empresa-teaser"> 
    <?php print $empresa['titulo']; ?> 
    </a> 
    <br /> 
    <div class="empresa-teaser">TEST</div> 

<?php } ?> 
+0

哇!這很漂亮! :)謝謝你洛根! – Aron

1

值得一提的是:

您可以創建一個設置對象,讓您的變量,例如:

你的PHP:

<?php 
    $settings = array('settingA' => 'something', 
         'settingB' => 'something else', 
         'wat'  => 9001); 
?> 

保持你的JS在你的js文件 - scripts.js,不管。但您可以將其添加到您的html文件中:

<script> 
    var settings = <?php echo json_encode($settings) ?>; 
</script> 

該輸出是什麼?

var settings = {"settingA":"something","settingB":"something else","wat":9001}; 

所以,你可以把你所需的服務器信息在一個全局對象(或把它作爲你的應用程序對象或東西的屬性),並可以訪問它。

console.log(settings.settingA); // returns "something" 

進一步看你的問題,不要忘記你不必留在PHP標籤。我不主張永遠需要PHP在你的CSS,但這應該幫助你掌握的概念:

<style> 
    <?php foreach ($empresas as $empresa): ?> 
    .showhide<?php echo $empresa['teaserid'] ?> { 
     display:none; 
    } 
    <?php endforeach; ?> 
</style> 
+0

這些東西。所有這些東西。做他們所有。 – gf3

+0

太棒了:D謝謝! – Aron