2012-03-24 78 views
1

我有一个Drupal 网站。我想要的是: - 如果用户在名为'fotos(-20xx)'或'filmpjes(-20xx)'的页面上(括号内的内容是可选的,x的数字是0-9 )没有任何反应 - 如果用户在网页上除上述之外,新rel atriibute添加到$attributes如果不等于

这是我脑子里想的:

$fotos = array('/fotos', '/fotos-2004', '/fotos-2005', '/fotos-2006', '/fotos-2007', '/fotos-2008', '/fotos-2009', '/fotos-2010', '/fotos-2011', '/filmpjes', '/filmpjes-2007', '/filmpjes-2009', '/filmpjes-2010', '/filmpjes-2011'); 
    $currentpage = $_SERVER['REQUEST_URI']; 

    foreach ($fotos as $foto) {if($foto != $currentpage) {$attributes['rel'] = 'shadowbox';}} 

这有两个问题:

  1. 它只是不起作用
  2. 每年我都要增加新的一年(例如,/fotos-2013)

有没有人有任何想法如何可以写在一个短片段?而且,更重要的是,有没有人看到错误?

+0

你可以做一个[正则表达式] [http://php.net/manual/en/function.preg-match.php]环节的比赛,如果'fotos'和/或'filmpjes'是常数项。 – hjpotter92 2012-03-24 12:21:45

+0

此代码位于哪里,'index.php'? – galymzhan 2012-03-24 12:22:47

回答

1

变化,我认为你的要求是增加一个变量时URI中不存在数组。

$base = array('/fotos','/filmpjes'); 
    $baseFotos = '/fotos-'; 
    $baseFilm = '/filmpjes-'; 
    $i = '2004'; $j = date('Y'); 
    while ($i <= $j) { 
     $fotos[] = $baseFotos.$i; 
     $film [] = $baseFilm.$i; 
     $i++; 
    } 
    $result = array_merge($base,$fotos,$film); 

    // check if base uri in array. 
    if (!in_array($_SERVER['REQUEST_URI'], $result)) { 
     $attributes['rel'] = 'shadowbox'; 
    } 
+0

非常感谢,这似乎像一个魅力工作! – 2012-03-24 13:27:13

+0

不客气@Bram Vanroy – 2012-03-24 14:11:24

+0

小凹凸:我注意到,它的工作原理应该如此,但不幸的是它不适用于基础路径'/ fotos'和'/ filmpjes'。所以基本上这些需要被添加到$ result,但我坦率地不知道为什么。任何想法? – 2012-03-24 15:19:35

0

为了解决您可以创建$fotos dinamically这样

<?php 

$fixed = '/fotos'; 
$time = strtotime('now'); 
$year_start = '2004'; 
$year_end = date('Y', $time); 
$fotos = array($fixed); 
for(; $year_start <= $year_end; $year_start++){ 
    $fotos[] = "$fixed-$year_start"; 
} 

我不明白的代码片段的其他应该做什么多年的问题,但每次要覆盖相同的元素!在任何情况下,你是在谈论$attribute但写道$attributes

+0

所以这基本上会在2004年,2005年,2006年,2007年,2008年,2009年的阵容中创建一个阵列......?并且该sippet应该按照我所说的去做:当用户不在一年的页面上时(例如fotos-2004,fotos-2005 ...),应该向$ attributes添加rel属性。 – 2012-03-24 12:40:45

0

我只是想着这行

<?php $currentpage = $_SERVER['REQUEST_URI']; ?> 

也许这可能是这个,而不是

<?php $currentpage = drupal_get_path_alias($_GET['q']); ?> 
+0

我知道Drupal有自己的手段来获得链接,但是你的例子做了什么? 'q'代表什么? (注意:这是Drupal 7我talkig约) – 2012-03-24 12:39:00

+1

http://www.example.com/sample - 会给你的价值'样品' – 2012-03-24 12:50:53