PHP中的上一个/下一个按钮

时间:2014-04-22 10:34:35

标签: php mysql next

我几乎都是PHP新手,所以请耐心等待。

我正在尝试构建一个运行在名为Core的cms上的网站。我正在努力使前一个/下一个按钮循环通过标签而不是条目。标签作为core_tags存储在数据库中。每个标签都有自己的tag_id,这是一个数字。我已经尝试更改了上一个/下一个按钮的强制代码,但是它一直给我'警告:mysql_fetch_array()期望参数1是资源,在第50行/home/core/functions/get_entry.php中给出null 。“

非常感谢任何帮助。

Get_entry.php:

<?php

$b = $_SERVER['REQUEST_URI'];

if($entry) {
    $b = substr($b,0,strrpos($b,"/")) . "/core/";
    $id = $entry;
    $isPerma = true;
} else {
    $b = substr($b,0,mb_strrpos($b,"/core/")+6);
    $id = $_REQUEST["id"];
}

$root = $_SERVER['DOCUMENT_ROOT'] . $b;
$http = "http://" . $_SERVER['HTTP_HOST'] . substr($b,0,strlen($b)-5);

require_once($root . "user/configuration.php");
require_once($root . "themes/".$theme."/configuration.php");
require_once($root . "functions/session.php");

if(is_numeric($id)) {
    $type = "entry";
} else {
    $type = "page";
}

$id = secure($id);

if($type == "page") {
    $data = mysql_query("SELECT p.* FROM core_pages p WHERE p.page_title = \"$id\"");
    $page_clicks = 0;
    while($p = mysql_fetch_array($data)) {
        $url = $p["page_url"];
        $path = $root . "user/pages/" . $url;
        $page_clicks = $p['hits']+1;
        require($path);
    }

    mysql_query("UPDATE core_pages p SET
    p.hits = $page_clicks
    WHERE p.page_title = $id");
}

if($type == "entry") {

// queries the dbase
        $data_tags = mysql_query("SELECT entry_id,entry_title FROM core_entries WHERE entry_show = 1 ORDER BY entry_position DESC") or die(mysql_error());

    $navArr=array();
    while($tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)){
                array_push($navArr,$tmparray['entry_id']);
        }

    function array_next_previous($array, $value) {
    $index = array_search($value,$array);

    //if user clicked to view the very first entry
    if($value == reset($array)){
    $return['prev'] = end($array);
    $return['next'] = $array[$index + 1];
    //if user clicked to view the very last entry
    }else if($value == end($array)){
    $return['prev'] = $array[$index - 1];
    reset($array);
    $return['next'] = current($array);
    }else{
        $return['next'] = $array[$index + 1];
        $return['prev'] = $array[$index - 1];
    }
                return $return;
    }

    $data = mysql_query("SELECT e.* FROM core_entries e WHERE e.entry_id = $id AND e.entry_show = 1");
    $entry_clicks = 0;
    if(@mysql_num_rows($data) < 1) {
        die("Invalid id, no entry to be shown");
    }
    while($e = mysql_fetch_array($data)) {
        $nextPrevProject = array_next_previous($navArr,$id); 
        $entry_id       = $e['entry_id'];
        $entry_title    = $e['entry_title'];
        // DATE
        $t              = $e["entry_date"];
        $y              = substr($t,0,4);
        $m              = substr($t,5,2);
        $d              = substr($t,8,2);
        $entry_date     = date($date_format,mktime(0,0,0,$m,$d,$y));
        $entry_text     = $e['entry_text'];
        $entry_extra1   = $e['entry_extra1'];
        $entry_extra2   = $e['entry_extra2'];
        $entry_client   = $e['entry_client'];
        $entry_position = $e['entry_position'];
        $entry_hits     = $e['hits']+1;
        $entry_new      = $e['entry_new'];


        if($entry_new == 1) {
            $isNew = true;
        } else {
            $isNew = false;
        }

        if($nice_permalinks) {
            $entry_perma = "$http".$entry_id;
        } else {
            $entry_perma = "$http"."?entry=$entry_id";
        }

        $data_e2t = @mysql_query("SELECT e2t.tag_id FROM core_entry2tag e2t WHERE e2t.entry_id = $entry_id");

        $tag_str = "";

            while($e2t = @mysql_fetch_array($data_e2t)) {
                $tag_id = $e2t["tag_id"];
                $data_tags = @mysql_query("SELECT t.tag_text FROM core_tags t WHERE t.tag_id = $tag_id");
                    while($t = @mysql_fetch_array($data_tags)) {
                        $tag_text = $t["tag_text"];
                        $tag_str = $tag_str . "<a class=\"tag-link\" name=\"tag".$tag_id."\" href=\"#tag-"._encode($tag_text)."\">".$tag_text."</a>".$separator_tags;
                    }
            }

            $entry_tags = substr($tag_str,0,strlen($tag_str)-strlen($separator_tags));

        $layout_path = $root . "user/uploads/" . treat_string($entry_title) . "/layout.php";
        if(is_file($layout_path) && (@filesize($layout_path) > 0)) {
            require($layout_path);
        } else {
            require($theme_path . "parts/entry.php");
        }
    }

    mysql_query("UPDATE core_entries e SET
    e.hits = $entry_hits
    WHERE e.entry_id = $id");

}

if($isPerma) {
echo "<a class=\"index-link\" href=\"$http\">back to index</a>";
}
?>

3 个答案:

答案 0 :(得分:1)

在此处使用之前,您尚未定义$ data_entries:

    while($tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)){
                array_push($navArr,$tmparray['entry_id']);
        }

这就是您获得描述性错误消息的原因。 你的意思是使用$ data_tags吗?

答案 1 :(得分:0)

使用:"SELECT p.* FROM core_pages p WHERE p.page_title = '".$id."'

注意:mysql_connect不是sql-injection保存。如果您使用mysql_connect,请更改为PDO。

答案 2 :(得分:0)

$data_entries未在第50行定义,然后mysql_fetch_array返回给定的null值异常。

尝试将$tmparray = mysql_fetch_array($data_entries,MYSQL_ASSOC)更改为$tmparray = mysql_fetch_array($data_tags,MYSQL_ASSOC)

希望这有帮助!