权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>Passing data from PHP to JavaScript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing data from PHP to JavaScript - Stack Overflow

programmeradmin9浏览0评论

I have a simple php variable i need to carry over to a javascript part.

Code:

<?php
if ($p == 'home') { $selected == '0'; }
if ($p == 'music') { $selected == '1'; }
if ($p == 'videos') { $selected == '2'; }
if ($p == 'search') { $selected == '3'; }
if ($p == 'about') { $selected == '4'; }
if ($p == 'contact') { $selected == '5'; }
?>
<script type="text/javascript">
//SYNTAX: tabdropdown.init("menu_id", [integer OR "auto"])
tabdropdown.init("colortab", $selected) <-- $selected is the variable I want to carry over
</script>

I have a simple php variable i need to carry over to a javascript part.

Code:

<?php
if ($p == 'home') { $selected == '0'; }
if ($p == 'music') { $selected == '1'; }
if ($p == 'videos') { $selected == '2'; }
if ($p == 'search') { $selected == '3'; }
if ($p == 'about') { $selected == '4'; }
if ($p == 'contact') { $selected == '5'; }
?>
<script type="text/javascript">
//SYNTAX: tabdropdown.init("menu_id", [integer OR "auto"])
tabdropdown.init("colortab", $selected) <-- $selected is the variable I want to carry over
</script>
Share Improve this question edited Jul 22, 2011 at 7:03 Quentin 945k132 gold badges1.3k silver badges1.4k bronze badges asked Jul 22, 2011 at 6:58 rackemup420rackemup420 1,5672 gold badges16 silver badges38 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

It is the same as any other piece of text.

tabdropdown.init("colortab", <?php echo $selected; ?>)

Since the value is one of a set of known values which are all numbers, it doesn't need escaping or quoting.

First of all

<?php

$map = array(
   'home' => 0,
   'music' => 1,
   'videos' => 2,
   'search' => 3,
   'about' => 4,
   'contact' => 5 
);

$selected = $map[$p];

?>

And i would remend to just use some variable, like :

?>
<script type="text/javascript">
var data = { /*something that you want to pass to the script */ };
</script>
<?php

And then , lower, when you include your external JS files ( they should be right before closing tag ) , you can check within the files if variable data is set. And then act upon that data .

Although the above solutions will help, it required you having JavaScript embedded your HTML/PHP file (which, granted you do have).

If you have a separate js file then you will need to take a different approach.

Store it in a hidden HTML variable:

<input type="hidden" id="ColorTabSelected" value="<?php echo($selected);?>">

Then in your js

var selected = document.getEelementById("ColorTabSelected").value;
tabdropdown.init("colortab", selected);

In general, I find this approach preferable anyway, it promotes separation and means that you don't have a executable module of code that is changeable and dynamic. This means you can isolate it effectively for unit testing.

<?php
$p = 'home';
$map = array(
    'home' => '0',
    'music' => '1',
    'videos' => '2',
    'search' => '3',
    'about' => '4',
    'contact' => '5'
);
$selected = isset($map[$p]) ? $map[$p] : $map['home'];
?>
<script type="text/javascript">
tabdropdown.init("colortab", <?=$selected?>)
</script>

Try with :

<?php $yourVar = "hello"; ?>
<script>
 var yourVar = "<?php echo $yourVar ?>"
</script>
发布评论

评论列表(0)

  1. 暂无评论