PHP - 显示结果,基于链接的条件

时间:2009-12-11 18:16:39

标签: php sql conditional

寻找以下代码的帮助。我认为我组织php调用和函数调用的方式可能存在问题。我是初学者,我正在尝试允许用户显示不同表格的结果,具体取决于他们点击的链接。

我收到此错误:

解析错误:语法错误,第16行/home/content/c/e/l/celebrything/html/wp-content/themes/celebrything/sidebar.php中的意外“{”

在这里纠正问题的任何帮助都会很棒。这是代码:

<div id="sidebar">

<div class="post">
<h2>

<font color="#333333">Most Popular Celebrities</font><br>
<font color="#333333">in last 24 hours</font>
<br>
<br>

<a href="page.php?table=today">Today</a>
<a href="page.php?table=week">Week</a>
<a href="page.php?table=month">Month</a>

<?php
if (!in_array($table, array('today', 'week', 'month')) {
  return false;
}

global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
    urlencode($row->name) . '&search=Search">' . $row->name .
    '</a> - ' . $row->count . ' Posts<br/>';
}
}

?>

showTable($_GET['table']);


</h2>
</div>

</div>

<div class="clear"></div>

更新代码 ----------------

<div id="sidebar">

<div class="post">
<h2>

<font color="#333333">Most Popular Celebrities</font><br>
<font color="#333333">in last 24 hours</font>
<br>
<br>

<a href="page.php?table=today">Today</a>
<a href="page.php?table=week">Week</a>
<a href="page.php?table=month">Month</a>

<?php
if (!in_array($table, array('today', 'week', 'month'))) {
  return false;
}

global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
    urlencode($row->name) . '&search=Search">' . $row->name .
    '</a> - ' . $row->count . ' Posts<br/>';
}



if (!empty($_GET['table'])) {
showTable($_GET['table']);

} else { showTable('today'); }

?>




</h2>
</div>

</div>

<div class="clear"></div>

2 个答案:

答案 0 :(得分:2)

1。在第一个if区块

上缺少a)
if (!in_array($table, array('today', 'week', 'month'))) {
  return false;
}

2。在结束}之前还有一个额外的?>

}
}

?>

3。您需要在结束showTable之前设置?>功能,如:

showTable($_GET['table']);
?>

摘要:

获取支持语法突出显示的代码编辑器。你会爱上它。

答案 1 :(得分:2)

在我看来,你正在引用一个函数 - showTable() - 但是你没有在函数内部设置你的逻辑(除非你在代码示例中留下了一些东西)。应该是:

<?
//----------------------
//Create the showTable() function, which won't do anything until it's called. It can
//reside anywhere on the page, really. It's here just because this is where I put it.

function showTable($table) {
   if (!in_array($table, array('today', 'week', 'month'))) {  
      return false;
   }

   global $wpdb;
   $result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
   foreach($result as $row) {
      echo('<a href="http://www.celebrything.com/?s='.urlencode($row->name) . '&search=Search">'.$row->name.'</a> - '.$row->count.' Posts<br/>');
   }
}

//----------------------
//Here is where you actually call the function, to display some stuff on the page

if (!empty($_GET['table'])) {
   showTable($_GET['table']);
} else { 
   showTable('today');
}
?>

上面的代码假设你正在使用Wordpress内置的正确/工作函数(我不太了解Wordpress)。但上面的语法应该清除任何与函数语法相关的问题,我认为这是你的主要问题。