意外的T_STRING - For循环

时间:2014-06-13 10:42:44

标签: php

我的分页脚本出现此错误。我的编辑说它在$ active线上,但是我无法看到它应该在哪里。

意外的T_STRING

$current_page = 1;
if(isset($_GET['pagenr']) && is_int($_GET['pagenr'] * 1))
{
        $current_page = $_GET['pagenr'] * 1;
}

$query_count = "SELECT COUNT(thread_id) AS antal FROM forum_thread WHERE fk_sub_category_id = $subid ORDER BY sticky_thread DESC";
$result_count = mysqli_query($con, $query_count);
$row_count = mysqli_fetch_assoc($result_count);
$threads_in_category = $row_count['antal'];

//Ceil runder op til hele tal.
$total_pages = ceil($threads_in_category / $threads_per_page);


echo "<ul class='pagination'>";

for ($i=1; $i<=$total_pages; $i++)
{ 
    $active = ($current_page == $i ? 'class="active"' : 'class="test"');
    $href = "?page=categories&category_id=$category_id&pagenr=$i"; 
    echo "<li $active><a href='$href'>$i</a></li>"; 
} 
echo '</ul>'; 


//Paging end.

3 个答案:

答案 0 :(得分:1)

除了Naing的帖子,你应该正确地连接字符串和变量:

$active = ($current_page == $i) ? 'class="active"' : 'class="test"';
$href = "?page=categories&category_id=" . $category_id . "&pagenr=" . $i; 
echo "<li " . $active . "><a href='" . $href . "'>" . $i . "</a></li>"; 

SQL查询相同。 您可以将{$var}个变量放在大括号中,但更好(关注代码突出显示)只需永远将变量放入带引号的字符串中。

当你把一个变量放在一个字符串中时,PHP应该如何告诉变量何时结束?

$t = 1;
$te = 2;
$tes = 3;
$test = 4;

// is this $t + estt? or $te + stt ? or $tes + tt ? or $test + t ? or $testt ?
echo "$testt"; 

// spoiler: there is no output at all because it "confuses" PHP
//          ... it also confuses the reader and your editor because it can't
//          do code highlighting correctly on those strings

// correct:


echo $test . 't';

答案 1 :(得分:1)

是的请整合Naing Lin Aung和DanFromGermany的建议。

但是您也可以在脚本结束之前和之后检查代码中的空格

白色空间 ?&GT; 白色空间

答案 2 :(得分:0)

我建议

$active = ($current_page == $i ? 'class="active"' : 'class="test"');

$active = ($current_page == $i) ? 'class="active"' : 'class="test"';