我将在php中构建一个弹出式javascript:
$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);
这里我制作了javascript代码:
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>";
这给出了这个javascript例如:
<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>
我不习惯foreach。 for循环将是这样的:
for($n=0; $n < count($popup_delay); $n++){ echo $popup_delay[$n]; }
,但当我已经使用$popup_delay
中的json_encode($popup)
foreach ( $popups as $popup ):
值?
答案 0 :(得分:1)
您只需要为阵列使用一个键。因此,不是在PHP中创建多个数组,而是可以像这样创建一个多维数组:
$javascript_array = array();
$javascript_array[0]['title'] = "Ready for ...";
$javascript_array[0]['text'] = "Click here to...";
$javascript_array[0]['time'] = 3000;
$javascript_array[0]['delay'] = 6000;
您的数组将如下显示:
if ( !empty ( $javascript_array ) ) {
// dump in your <script> piece here
foreach ( $javascript_array as $js_entry ) {
// put in your filler pieces here
echo "Title: ".$js_entry['title'];
echo "Text: ".$js_entry['text'];
echo "Time: ".$js_entry['time'];
echo "Delay: ".$js_entry['delay'];
} // end foreach
// closed </script>
}