javascript oluşturmak için php foreach döngüsü

0 Cevap php

Böyle php bir popup javascript oluşturmak için gidiyorum:

$popup_title = array(); 
$popup_text = array(); 
$popup_time = array();
$popup_delay = array(); 

array_push($popup_title, T_gettext("Ready for ..."));
array_push($popup_text, "<a href=\"x.php\">".T_gettext("Click here to...")."</a>"); 
array_push($popup_time, 3000);  
array_push($popup_delay, 6000); 

burada ben javascript kodu yapmak:

if(!empty($popup_title)){
//constuct javascript

$popups = array();
foreach ( $popup_title as $key => $title )
{
    $popups[$key] = new stdClass();
    $popups[$key]->title = $title;
}
foreach ( $popup_text as $key => $text )
{
    $popups[$key]->text = $text;
}
foreach ( $popup_time as $key => $time )
{
    $popups[$key]->time = $time; 
}
//print javascript
echo "
<script type=\"text/javascript\">
$(document).ready(function(){"; 
foreach ( $popups as $popup ):
echo "
    setTimeout(function() {
        $.gritter.add("; echo json_encode($popup); echo ");
    }, ".($popup_delay");"; // <---------Here I need to place popup_delay 
    endforeach;
echo "  
});
</script>"; 

Bu, örneğin, bu javascript verir:

<script type="text/javascript">
$(document).ready(function(){
    setTimeout(function() {
        $.gritter.add({"title":"Ready for..","text":"<a href=\"x.php\">Click here to...<\/a>","time":3000});
    }, 0);  
});
</script>

I am not used to foreach. a for loop would be something like this: for($n=0; $n < count($popup_delay); $n++){ echo $popup_delay[$n]; } , but how do I loop through the $popup_delay values with foreach, when I already use json_encode($popup) from foreach ( $popups as $popup ):

0 Cevap