MysqliDb::orderBy PHP Method

orderBy() public method

This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries.
public orderBy ( string $orderByField, $orderbyDirection = "DESC", array $customFields = null ) : MysqliDb
$orderByField string The name of the database field.
$customFields array Fieldset for ORDER BY FIELD() ordering
return MysqliDb
    public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields = null)
    {
        $allowedDirection = array("ASC", "DESC");
        $orderbyDirection = strtoupper(trim($orderbyDirection));
        $orderByField = preg_replace("/[^-a-z0-9\\.\\(\\),_`\\*\\'\"]+/i", '', $orderByField);
        // Add table prefix to orderByField if needed.
        //FIXME: We are adding prefix only if table is enclosed into `` to distinguish aliases
        // from table names
        $orderByField = preg_replace('/(\\`)([`a-zA-Z0-9_]*\\.)/', '\\1' . self::$prefix . '\\2', $orderByField);
        if (empty($orderbyDirection) || !in_array($orderbyDirection, $allowedDirection)) {
            throw new Exception('Wrong order direction: ' . $orderbyDirection);
        }
        if (is_array($customFields)) {
            foreach ($customFields as $key => $value) {
                $customFields[$key] = preg_replace("/[^-a-z0-9\\.\\(\\),_` ]+/i", '', $value);
            }
            $orderByField = 'FIELD (' . $orderByField . ', "' . implode('","', $customFields) . '")';
        }
        $this->_orderBy[$orderByField] = $orderbyDirection;
        return $this;
    }

Usage Example

コード例 #1
0
ファイル: functions.php プロジェクト: hoseinBL/SoftwareTalks
/**
 * send message to all users
 * @param $text
 * @param $chatid
 * @param $status
 * @param MysqliDb $db
 * @param TelegramBot\Api\BotApi $bot
 */
function sendMessageToAll($text = null, $chatid, $status, $db, $bot)
{
    //this is use for hide confirm keyboard
    $hideKeys = new \TelegramBot\Api\Types\ReplyKeyboardHide(true);
    if ($status == 1) {
        //confirm keyboard
        $keys = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("بله", "خیر")), false, true, true);
        if ($text == null) {
            //admin is going to send next message and next message stored
            $db->orderBy('ID', 'DESC');
            $q = $db->getOne('nextMessages', array('text'));
            $text = $q['text'];
        }
        $db->update('adminOperations', array('message' => $text));
        $status = 2;
        //admin get confirm
        $msg = "پیام زیر برای همه کاربران ارسال خواهد شد. آیا برای ارسال پیامها اطمینان دارید؟\n\n";
        $msg .= $text;
        $bot->sendMessage($chatid, $msg, true, null, $keys);
    } elseif ($status == 2 && $text == 'بله') {
        //get all user and send message for them
        $users = $db->get('users');
        $db->orderBy('ID', 'DESC');
        //custom message and next message temporary stored in adminOperations table
        $q = $db->getOne('adminOperations', array('message'));
        $message = $q['message'];
        foreach ($users as $user) {
            try {
                $bot->sendMessage($user['ID'], $message);
            } catch (Exception $e) {
                error_log($e->getMessage());
            }
        }
        $bot->sendMessage($chatid, 'پیام مورد نظر ارسال شد', true, null, $hideKeys);
        $status = 0;
    } else {
        $bot->sendMessage($chatid, 'ارسال پیام لغو شد', true, null, $hideKeys);
        $status = 0;
    }
    $db->update('adminOperations', array('send_status' => $status));
}
All Usage Examples Of MysqliDb::orderBy