函数只返回一个元素

时间:2015-09-26 19:55:19

标签: php mysql function return

为什么这个函数只返回一个,最后一个元素从数据库中输入?来自不同类别的更多内容:

函数从表“tentego_cat”获取类别id,在“tenteg_img”上搜索元素,其中“tentego_vote”表上的记录最多

功能代码:

function bestOfDay($pattern, $where, $currentPage, $objPerPage)
{
    global $ads, $user, $rewrite, $kernel, $page;
    if (!isset($_SESSION['last_cat'])) $_SESSION['last_cat'] = false;
    $plugins = $this->loadPlugins();
    $sett = $page;
    if (isset($currentPage) && is_numeric($currentPage) && $currentPage > 0) {
        $page = mysql_real_escape_string($currentPage - 1) * $objPerPage;
    }
    else $page = 0;
    $selectCategory = mysql_query("SELECT tentego_img_cat.* FROM tentego_img_cat");
    while ($cat = mysql_fetch_array($selectCategory)) {
        $catID = $cat['id'];
        $date = date('Y-m-d');
        $query = mysql_query("SELECT tentego_img.* FROM tentego_img INNER JOIN tentego_img_vote ON tentego_img.id = tentego_img_vote.object_id WHERE cat =$catID GROUP BY tentego_img_vote.object_id ORDER BY SUM( (CASE WHEN tentego_img_vote.vote = '0' AND tentego_img_vote.date LIKE '" . $date . "%' THEN '1' ELSE '0' END) ) DESC LIMIT 1");
        $text = NULL;
        $return = NULL;
        while ($img = mysql_fetch_array($query)) {
            $return.= $ads->load('#AD[object]#');
            $object = null;
            $text = preg_replace("/\[object url=(.*)\]/", $object, $pattern);
            $shortTitle = $img['title'];
            if (strlen($shortTitle) > 200) {
                $shortTitle = mb_substr($shortTitle, 0, 500, 'utf-8') . '<a href="' . $rewrite->img("#ID#", "#REWRITE-TITLE#") . '" style="color: #0080; font-size: 16px;">   [...] - Przejdź do wpisu</a>';
            }
            else $shortTitle = $shortTitle . '<a href="' . $rewrite->img("#ID#", "#REWRITE-TITLE#") . '"style="color: #0080; font-size: 16px;"> - Przejdź do wpisu </a>';
            $text = str_replace("#TITLE#", nl2br($shortTitle) , $text);
            $text = str_replace("#REWRITE-TITLE#", $rewrite->changeSigns($img['title']) , $text);
            $text = str_replace("#ID#", $img['id'], $text);
            // GÅ‚osy
            $votes = mysql_query("SELECT SUM(IF(vote=0,1,0)) as votes
                                            FROM `tentego_img_vote` WHERE `object_id`=" . $img['id'] . " GROUP BY object_id");
            $vote = mysql_fetch_array($votes);
            if ($vote['votes'] != 0) {
                $text = str_replace("#VOTE#", $vote['votes'], $text);
            }
            else {
                $text = str_replace("#VOTE#", '0', $text);
            }
            $date = new DateTime($img['date']);
            $text = str_replace("#DATE#", $date->format("d.m.y") . ', ' . $date->format("H:i") , $text);
            if ($img['type'] == 'Myśl') {
                // Owner
                if ($img['owner'] != 0) {
                    $owner = mysql_fetch_array(mysql_query("SELECT tablicacms_users.user FROM `tablicacms_users` WHERE `id`=" . $img['owner']));
                    $owner_name = $owner['user'];
                }
                else {
                    $owner_name = "Gość";
                    $owner['id'] = 0;
                }
                $text = str_replace("#OWNER#", '<a href="' . $rewrite->user($img['owner'], $rewrite->changeSigns($owner_name)) . '">' . $owner_name . '</a>', $text);
                // Owner Avatat
                if ($img['owner'] != 0) {
                    $owner = mysql_fetch_array(mysql_query("SELECT * FROM `tablicacms_users` WHERE `id`=" . $img['owner']));
                    $owner_name = $owner['user'];
                }
                else {
                    $owner_name = "Gość";
                    $owner['id'] = 0;
                }
                if ($img['owner'] != 0) {
                    $owner = mysql_fetch_array(mysql_query("SELECT * FROM `tablicacms_users` WHERE `id`=" . $img['owner']));
                    $owner_name = $owner['user'];
                }
                else {
                    $owner_name = "Gość";
                    $owner['id'] = 0;
                    $owner['avatar'] = "upload/avatars/default.png";
                }
                if ($owner['avatar'] != NULL) {
                    $owner_avatar = $owner['avatar'];
                }
                else {
                    $owner['avatar'] = "upload/avatars/default.png";
                }
                $text = str_replace("#OWNER-AVATAR#", 'http://' . $_SERVER['HTTP_HOST'] . '/' . $owner['avatar'], $text);
                $text = str_replace("#BOOK#", null, $text);
            }
            else if ($img['type'] == 'Cytat') {
                // Owner
                $owner = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_author` WHERE `id`=" . $img['author']));
                $text = str_replace("#OWNER#", '<a href="' . $rewrite->author($img['author'], $rewrite->changeSigns($owner['author'])) . '">' . $owner['author'] . '</a>', $text);
                if ($owner['avatar'] != null) {
                    $text = str_replace("#OWNER-AVATAR#", 'http://' . $_SERVER['HTTP_HOST'] . '/' . $owner['avatar'], $text);
                }
                else {
                    $text = str_replace("#OWNER-AVATAR#", 'http://' . $_SERVER['HTTP_HOST'] . '/
                        upload/authors/author.jpeg', $text);
                }
                if ($img['book'] != null) {
                    $book = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_content_books` WHERE `id`=" . $img['book']));
                    $text = str_replace("#BOOK#", '<a href="' . $rewrite->book($book['id'], $rewrite->changeSigns($book['name'])) . '">' . $book['name'] . '</a>', $text);
                }
                else {
                    $text = str_replace("#BOOK#", null, $text);
                }
            }
            // MODERATOR #
            $text = str_replace("#MOD_TOOLS#", $this->mod_tools($img) , $text);
            // KONIEC MODERATORA #
            if ($user->verifyLogin()) {
                $verify = mysql_num_rows(mysql_query("SELECT * FROM `tentego_img_vote` WHERE `object_id`=" . $img['id'] . " AND `user_id`=" . $user->userInfo('id')));
                if ($verify) $text = preg_replace('/\[FAV\=(.*)\|(.*)\]/', "$2", $text);
                else $text = preg_replace('/\[FAV\=(.*)\|(.*)\]/', "$1", $text);
            }
            else $text = preg_replace('/\[FAV\=(.*)\|(.*)\]/', "", $text);
            if ($user->verifyLogin()) {
                $text = str_replace("#NOTES-POPUP#", '<a href="" class="showModal" onClick="addToNote(' . $img['id'] . ',0)"><i class="book icon"></i></a>', $text);
            }
            else $text = str_replace("#NOTES-POPUP#", null, $text);
            $cat = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_img_cat` WHERE `id`=" . $img['cat']));
            $text = str_replace("#COLOR#", $cat['color'], $text);
            $text = str_replace("#CATEGORY#", '<a href="' . $rewrite->categories($img['cat'], $rewrite->changeSigns($cat['name'])) . '">' . $cat['name'] . '</a>', $text);
            $return.= $text;
        }
    }
    if (empty($return)) $return = "<div id=\"display_error\">" . $kernel->lang['object']['empty'] . "</div>";
    $page = $sett;
    return $return;
}

1 个答案:

答案 0 :(得分:0)

为什么使用$return = NULL; ??

当您需要使用$return = '';

附加数据时

尝试以下代码,

function bestOfDay($pattern, $where, $currentPage, $objPerPage)
{
    global $ads, $user, $rewrite, $kernel, $page;
    if (!isset($_SESSION['last_cat'])) $_SESSION['last_cat'] = false;
    $plugins = $this->loadPlugins();
    $sett = $page;
    if (isset($currentPage) && is_numeric($currentPage) && $currentPage > 0) {
        $page = mysql_real_escape_string($currentPage - 1) * $objPerPage;
    }
    else $page = 0;
    $selectCategory = mysql_query("SELECT tentego_img_cat.* FROM tentego_img_cat");
    while ($cat = mysql_fetch_array($selectCategory)) {
        $catID = $cat['id'];
        $date = date('Y-m-d');
        $query = mysql_query("SELECT tentego_img.* FROM tentego_img INNER JOIN tentego_img_vote ON tentego_img.id = tentego_img_vote.object_id WHERE cat =$catID GROUP BY tentego_img_vote.object_id ORDER BY SUM( (CASE WHEN tentego_img_vote.vote = '0' AND tentego_img_vote.date LIKE '" . $date . "%' THEN '1' ELSE '0' END) ) DESC LIMIT 1");
        $text = '';
        $return = '';
        while ($img = mysql_fetch_array($query)) {
            $return.= $ads->load('#AD[object]#');
            $object = null;
            $text = preg_replace("/\[object url=(.*)\]/", $object, $pattern);
            $shortTitle = $img['title'];
            if (strlen($shortTitle) > 200) {
                $shortTitle = mb_substr($shortTitle, 0, 500, 'utf-8') . '<a href="' . $rewrite->img("#ID#", "#REWRITE-TITLE#") . '" style="color: #0080; font-size: 16px;">   [...] - Przejdź do wpisu</a>';
            }
            else $shortTitle = $shortTitle . '<a href="' . $rewrite->img("#ID#", "#REWRITE-TITLE#") . '"style="color: #0080; font-size: 16px;"> - Przejdź do wpisu </a>';
            $text = str_replace("#TITLE#", nl2br($shortTitle) , $text);
            $text = str_replace("#REWRITE-TITLE#", $rewrite->changeSigns($img['title']) , $text);
            $text = str_replace("#ID#", $img['id'], $text);
            // GÅ‚osy
            $votes = mysql_query("SELECT SUM(IF(vote=0,1,0)) as votes
                                            FROM `tentego_img_vote` WHERE `object_id`=" . $img['id'] . " GROUP BY object_id");
            $vote = mysql_fetch_array($votes);
            if ($vote['votes'] != 0) {
                $text = str_replace("#VOTE#", $vote['votes'], $text);
            }
            else {
                $text = str_replace("#VOTE#", '0', $text);
            }
            $date = new DateTime($img['date']);
            $text = str_replace("#DATE#", $date->format("d.m.y") . ', ' . $date->format("H:i") , $text);
            if ($img['type'] == 'Myśl') {
                // Owner
                if ($img['owner'] != 0) {
                    $owner = mysql_fetch_array(mysql_query("SELECT tablicacms_users.user FROM `tablicacms_users` WHERE `id`=" . $img['owner']));
                    $owner_name = $owner['user'];
                }
                else {
                    $owner_name = "Gość";
                    $owner['id'] = 0;
                }
                $text = str_replace("#OWNER#", '<a href="' . $rewrite->user($img['owner'], $rewrite->changeSigns($owner_name)) . '">' . $owner_name . '</a>', $text);
                // Owner Avatat
                if ($img['owner'] != 0) {
                    $owner = mysql_fetch_array(mysql_query("SELECT * FROM `tablicacms_users` WHERE `id`=" . $img['owner']));
                    $owner_name = $owner['user'];
                }
                else {
                    $owner_name = "Gość";
                    $owner['id'] = 0;
                }
                if ($img['owner'] != 0) {
                    $owner = mysql_fetch_array(mysql_query("SELECT * FROM `tablicacms_users` WHERE `id`=" . $img['owner']));
                    $owner_name = $owner['user'];
                }
                else {
                    $owner_name = "Gość";
                    $owner['id'] = 0;
                    $owner['avatar'] = "upload/avatars/default.png";
                }
                if ($owner['avatar'] != NULL) {
                    $owner_avatar = $owner['avatar'];
                }
                else {
                    $owner['avatar'] = "upload/avatars/default.png";
                }
                $text = str_replace("#OWNER-AVATAR#", 'http://' . $_SERVER['HTTP_HOST'] . '/' . $owner['avatar'], $text);
                $text = str_replace("#BOOK#", null, $text);
            }
            else if ($img['type'] == 'Cytat') {
                // Owner
                $owner = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_author` WHERE `id`=" . $img['author']));
                $text = str_replace("#OWNER#", '<a href="' . $rewrite->author($img['author'], $rewrite->changeSigns($owner['author'])) . '">' . $owner['author'] . '</a>', $text);
                if ($owner['avatar'] != null) {
                    $text = str_replace("#OWNER-AVATAR#", 'http://' . $_SERVER['HTTP_HOST'] . '/' . $owner['avatar'], $text);
                }
                else {
                    $text = str_replace("#OWNER-AVATAR#", 'http://' . $_SERVER['HTTP_HOST'] . '/
                        upload/authors/author.jpeg', $text);
                }
                if ($img['book'] != null) {
                    $book = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_content_books` WHERE `id`=" . $img['book']));
                    $text = str_replace("#BOOK#", '<a href="' . $rewrite->book($book['id'], $rewrite->changeSigns($book['name'])) . '">' . $book['name'] . '</a>', $text);
                }
                else {
                    $text = str_replace("#BOOK#", null, $text);
                }
            }
            // MODERATOR #
            $text = str_replace("#MOD_TOOLS#", $this->mod_tools($img) , $text);
            // KONIEC MODERATORA #
            if ($user->verifyLogin()) {
                $verify = mysql_num_rows(mysql_query("SELECT * FROM `tentego_img_vote` WHERE `object_id`=" . $img['id'] . " AND `user_id`=" . $user->userInfo('id')));
                if ($verify) $text = preg_replace('/\[FAV\=(.*)\|(.*)\]/', "$2", $text);
                else $text = preg_replace('/\[FAV\=(.*)\|(.*)\]/', "$1", $text);
            }
            else $text = preg_replace('/\[FAV\=(.*)\|(.*)\]/', "", $text);
            if ($user->verifyLogin()) {
                $text = str_replace("#NOTES-POPUP#", '<a href="" class="showModal" onClick="addToNote(' . $img['id'] . ',0)"><i class="book icon"></i></a>', $text);
            }
            else $text = str_replace("#NOTES-POPUP#", null, $text);
            $cat = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_img_cat` WHERE `id`=" . $img['cat']));
            $text = str_replace("#COLOR#", $cat['color'], $text);
            $text = str_replace("#CATEGORY#", '<a href="' . $rewrite->categories($img['cat'], $rewrite->changeSigns($cat['name'])) . '">' . $cat['name'] . '</a>', $text);
            $return.= $text;
        }
    }
    if (empty($return)) $return = "<div id=\"display_error\">" . $kernel->lang['object']['empty'] . "</div>";
    $page = $sett;
    return $return;
}