Welcome Guest, Not a member yet? Register   Sign In
Json with CI: easy way?
#1

[eluser]Ignacio[/eluser]
Hi guys! Anybody knows an easy, secure and fast way to get Json data like this?
Code:
["Sylvia Molloy", "Ignacio Giri", "Arturo Andres Roig", "Ricardo Rojas"]
and with two values?
Code:
[['Afghanistan', 'AF'], ['Anatolia', 'AT'], ['Armenia', 'AR']]
ideas?
thanks!!
#2

[eluser]Nick Husher[/eluser]
If you're using PHP5, you can call the function json_encode($variable), which returns a valid JSON object. There's also a PEAR library for non-PHP5-enabled.
#3

[eluser]Ignacio[/eluser]
Can we create a example in a real worl?
Using PHP5 of course.
You say: call the function json_encode($variable), so...
Code:
$searchterm = 'a';
$query = $this->db->query("SELECT a.AccentCity FROM Cities a WHERE a.AccentCity LIKE '%".$searchterm."%' ORDER BY a.AccentCity DESC LIMIT 0, 10");
$res = $query->result();
echo json_encode($res);
that return:
Code:
[{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""}]
see the problem?
thanks for your response
#4

[eluser]Nick Husher[/eluser]
Try:
Code:
$res = $query->result_array();
#5

[eluser]Ignacio[/eluser]
no, that return
Code:
[{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""},{"AccentCity":""}]
#6

[eluser]Nick Husher[/eluser]
Just curious, what happens if you print_r($query->result_array())?
#7

[eluser]Ignacio[/eluser]
return
Code:
Array ( [0] => Array ( [AccentCity] => Þönglabakki ) [1] => Array ( [AccentCity] => Þykkvabæjarklaustur ) [2] => Array ( [AccentCity] => Þykkvabejarklaustur ) [3] => Array ( [AccentCity] => Þverá ) [4] => Array ( [AccentCity] => Þrestbakki ) [5] => Array ( [AccentCity] => Þrestbakki ) [6] => Array ( [AccentCity] => Þóroddsstaður ) [7] => Array ( [AccentCity] => Þorlákshöfn ) [8] => Array ( [AccentCity] => Þorkötlustaðir ) [9] => Array ( [AccentCity] => Þjórsárbrú ) )
in json should be
Code:
["Þönglabakki", "Þykkvabæjarklaustur", "Þykkvabejarklaustur" ........
sorry about the weird cities, hehe,
I need to optimize my query too
#8

[eluser]Nick Husher[/eluser]
I'm going to guess and say it's related to the character encoding of the database entry, not necessarily the function itself. Perhaps converting the city names into entities and then running json_encode on them would work?
#9

[eluser]Ignacio[/eluser]
thanks, it work with this:
Code:
$searchterm = 'a';
$query = $this->db->query("SELECT a.AccentCity FROM Cities a WHERE a.AccentCity LIKE '%".$searchterm."%' ORDER BY a.AccentCity DESC LIMIT 0, 10");
$res = $query->result_array();
foreach($res as $key) {
    foreach($key as $value) {
        $arr[] = htmlentities($value);
    }
}
echo json_encode($arr);
but isn't there an easy way than those foreachs with CI?
#10

[eluser]Nick Husher[/eluser]
You could encode the values as you insert them, if that's a possibility.




Theme © iAndrew 2016 - Forum software by © MyBB