コード例 #1
0
ファイル: JsonUtil.php プロジェクト: joseilsonjunior/nutrif
 /**
  * Codificar caracter em UTF-8.
  * 
  * @staticvar int $depth
  * @param type $inArray
  * @return boolean
  */
 public static function utf8ToJson($inArray)
 {
     static $depth = 0;
     /* our return object */
     $newArray = array();
     /* safety recursion limit */
     $depth++;
     if ($depth >= '30') {
         return false;
     }
     /* step through inArray */
     foreach ($inArray as $key => $val) {
         if (is_array($val)) {
             /* recurse on array elements */
             $newArray[$key] = utf8json(json_encode($val));
         } else {
             if (!is_null($val) && is_object($val)) {
                 $newArray[$key] = utf8_encode(json_encode($val->toArray()));
             } else {
                 /* encode string values */
                 $newArray[$key] = utf8_encode(json_encode($val));
             }
         }
     }
     /* return utf8 encoded array */
     return $newArray;
 }
コード例 #2
0
function utf8json($inArray)
{
    global $depth;
    /* our return object */
    $newArray = array();
    /* safety recursion limit */
    $depth++;
    if ($depth >= 30) {
        return false;
    }
    /* step through inArray */
    foreach ($inArray as $key => $val) {
        if (is_array($val)) {
            /* recurse on array elements */
            $newArray[$key] = utf8json($val);
        } else {
            /* encode string values */
            $newArray[$key] = utf8_encode($val);
        }
    }
    /* return utf8 encoded array */
    return $newArray;
}
コード例 #3
0
<?php

require_once '../../connection/dbConn.php';
require_once '../phpTools/utilities.php';
$id = $_GET["id"];
mysql_select_db($database, $dbConn);
$query = sprintf("SELECT * FROM usuarios WHERE id_usuario=%s", $id);
$result = mysql_query($query, $dbConn) or die(mysql_error());
echo json_encode(utf8json(mysql_fetch_assoc($result)));
mysql_free_result($result);
コード例 #4
0
 public function get_tweets($json_data = 10)
 {
     if ($this->ajax == 0) {
         array_push($this->js_includes, "js/stream.js");
         $tmpl_tweet_display = new XTemplate('template/tweet_display.xtpl');
         $limit = (int) $json_data;
         $this->js_tmpl->load_template('tweet_display', 'tweet_display.tweet_box');
         $this->js_tmpl->create_js_from_blocks();
     } else {
         $data = json_decode($json_data, true);
         $limit = $data['limit'];
         $return = array();
         $return['success'] = 0;
     }
     // get current user totals
     // get a list of tweets voted on by the current user
     $user_voted_list = array();
     $voted_list = array();
     $rated = $no_news = $news = $news_op = 0;
     $total_rated = $total_no_news = $total_news = $total_news_op = 0;
     // WHERE `ip`='".."'
     $this->db->query("SELECT * FROM `votes`");
     while ($votes = $this->db->fetch_row()) {
         if ($votes['ip'] == $_SERVER['REMOTE_ADDR']) {
             if (empty($user_votes_list[$votes['tweet_id']])) {
                 $rated++;
                 switch ($votes['vote']) {
                     case 1:
                         $no_news++;
                         break;
                     case 2:
                         $news++;
                         break;
                     case 3:
                         $news_op++;
                         break;
                 }
             }
             $user_voted_list[$votes['tweet_id']] = 1;
         }
         $total_rated++;
         switch ($votes['vote']) {
             case 1:
                 $total_no_news++;
                 break;
             case 2:
                 $total_news++;
                 break;
             case 3:
                 $total_news_op++;
                 break;
         }
         $voted_list[$votes['tweet_id']] = 1;
     }
     if (sizeof($voted_list) == 0) {
         $voted_list[0] = 1;
     }
     // check how many tweets with no votes
     $this->db->query("SELECT id FROM `init_tweets`");
     $total_tweets = $this->db->rows();
     if ($total_tweets - sizeof($voted_list) > 1000) {
         $extra = " AND `id`NOT IN(" . implode(",", array_keys($voted_list)) . ")";
     } else {
         $extra = "";
     }
     $classify = new classify(array(1 => "no_news", 2 => "news", 3 => "news_op"), array(), false);
     $classify->add_tweets();
     $type = array(1 => "Not News", 2 => "News Rp", 3 => "News Op");
     $return_tweets = array();
     if (sizeof($user_voted_list) != 0) {
         $user_extra = "AND `id`NOT IN(" . implode(",", array_keys($user_voted_list)) . ")";
     } else {
         $user_extra = "";
     }
     $this->db->query("SELECT * FROM `init_tweets` WHERE 1 " . $user_extra . $extra . " AND `guess`NOT IN(0,1) ORDER BY RAND() LIMIT " . $limit);
     while ($tweet = $this->db->fetch_row()) {
         $return_tweet = array("id" => $tweet['id'], "tweet_text" => $tweet['tweet'], "tweet_id" => $tweet['twitter_id'], "user" => $tweet['user'], "time" => date("g:i a", $tweet['created']), "date" => date("d/m/Y", $tweet['created']), "guess" => $type[$classify->classify_tweet($tweet['tweet'])]);
         if ($this->ajax == 1) {
             $this->fill($return_tweet, $return_tweets);
         } else {
             $this->fill($return_tweet, $tmpl_tweet_display);
         }
     }
     if ($this->ajax == 0) {
         // create a toplist
         $this->db->query("SELECT COUNT(*) as vote_total, ip FROM `votes` GROUP BY `ip` ORDER BY `vote_total` DESC LIMIT 10");
         while ($users = $this->db->fetch_row()) {
             $tmpl_tweet_display->assign('ip', $users['ip']);
             $tmpl_tweet_display->assign('num_classified', $users['vote_total']);
             if ($users['ip'] == $_SERVER['REMOTE_ADDR']) {
                 $tmpl_tweet_display->assign('you', "(You)");
             } else {
                 $tmpl_tweet_display->assign('you', "");
             }
             $tmpl_tweet_display->parse("tweet_display.top_list");
         }
         $tmpl_tweet_display->assign('rated', $rated);
         $tmpl_tweet_display->assign('no_news', $no_news);
         $tmpl_tweet_display->assign('news', $news);
         $tmpl_tweet_display->assign('news_op', $news_op);
         $tmpl_tweet_display->assign('total_rated', $total_rated);
         $tmpl_tweet_display->assign('total_no_news', $total_no_news);
         $tmpl_tweet_display->assign('total_news', $total_news);
         $tmpl_tweet_display->assign('total_news_op', $total_news_op);
         $tmpl_tweet_display->parse("tweet_display");
         $this->template_out =& $tmpl_tweet_display;
     } else {
         $return['success'] = 1;
         $return['tweets'] = $return_tweets;
         echo json_encode(utf8json($return));
     }
 }