Forums / Cotonti / Support / Question about Json requests

Dyllon
#1 2013-03-26 18:01

I'm looking to create something similar to Cotonti.com's private message feature. I've been browsing around through the source code, but I don't know how to achieve it. From what I gather, this is the main part of it on the javascript end:

$.getJSON('index.php?r=pm_json', function(data)
{	
	var rows = '<ol>';
	$.each(data, function(key, val)
	{
		console.log(val);
		var url = 'pm?m=message&amp;id=0'.replace('0', val.pm_id);
		rows += '<li><a href="'+url+'">'+val.pm_title+'</a><small>Sender: '+val.pm_fromuser+'</small></li>';
	});
	rows += '</ol>';
	$('#pmbox').prepend(rows);
});

I just can't seem to figure out how to PHP side of it goes. Any insight?

We are what we repeatedly do. Excellence then, is not an act, but a habit.
Trustmaster
#2 2013-03-27 06:29

It's as simple as this:

<?php defined('COT_CODE') or die('Wrong URL.');
/* ====================
[BEGIN_COT_EXT]
Hooks=ajax
[END_COT_EXT]
==================== */

if ($usr['id'] > 0 && COT_AJAX)
{
	require_once cot_incfile('pm', 'module');
	if ($usr['newpm'])
	{
		echo json_encode($db->query("SELECT * FROM $db_pm WHERE pm_touserid=? AND pm_tostate=0", $usr['id'])->fetchAll());
	}
}

 

May the Source be with you!
Sergey
#3 2013-03-27 08:59

Function json_encode does not work correctly. It is with a large array of data gives an error when processing objects and arrays. This function corrects the errors. In the form, I keep the contents of pages, it increases the rate of conversion of pages in a friendly layout:

function object_in_array   	// 	преобразование структуры в массивы. исправление косяка json_decode
	(                       // 	внимание! функция рекурсивная.
 	$объект			//	@param  - сложная структура состоящая из объектов и массивов
  	)
{
if	(!is_object($объект) and !is_array($объект))	return $объект;
if	(is_object($объект))	$объект = get_object_vars($объект);
return array_map('object_in_array',$объект);
}

 

www.cotonti.mobi
Trustmaster
#4 2013-03-27 10:45

In the above example fetchAll() method always returns an array of arrays, not objects.

May the Source be with you!