I am working for a newspaper on my city, we hire a 3rd party company to build an app for us, and they are asking for jsons about our database, I really hate jsons always have hard time with them.
The problem I have is simple and complicated for me at least, I need to display only the elements with variable 7 on the column entry_blog_id
which belong to table mt_entry
the path would be mt_entry.entry_blog_id
. The problem is when I declare entry_blog_id = 7
, the code doesn't work, but if I delete that variable all the code works fine but choose all blogs id.
Here's is my full code, I need some help please (I know my English is horrible sorry about that)
if(isset($_GET['seccion']))
{
$SQL = "SELECT entry_id, entry_title, entry_text, entry_created_on, entry_basename, mt_blog.blog_name AS blog_name,
mt_blog.blog_site_path AS blog_site_path, mt_category.category_label AS category_label,
mt_category.category_basename AS category_basename, COUNT(mt_asset.asset_url) AS contadorImagenes,
mt_asset.asset_url AS asset_url, mt_asset.asset_file_ext AS asset_file_ext
FROM mt_entry, mt_placement, mt_category, mt_blog, mt_objectasset, mt_asset
WHERE
mt_entry.entry_id = mt_placement.placement_entry_id AND
mt_blog.blog_id = mt_entry.entry_blog_id AND
mt_placement.placement_category_id = mt_category.category_id AND
mt_entry.entry_status = 2 AND
mt_entry.entry_id = mt_objectasset.objectasset_object_id AND
mt_objectasset.objectasset_asset_id = mt_asset.asset_id
GROUP BY mt_entry.entry_id
ORDER BY mt_entry.entry_created_on DESC
LIMIT 0, 16";
$i=0;
$RESULT = mysql_query($SQL);
while($DATA = mysql_fetch_array($RESULT))
{
$entry_id=$titulo=$DATA[entry_id];
$creacionNoticia=new DateTime($DATA[entry_created_on]);
$creacionNoticiaToExplode=date_format($creacionNoticia,"Y-m-d");
$creacionNoticiaExploded=explode("-",$creacionNoticiaToExplode);
$meses = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
$creacionNoticia=$creacionNoticiaExploded[2]." de ".$meses[$creacionNoticiaExploded[1]-1]." del ".$creacionNoticiaExploded[0];
$basenameFinal=str_replace("_","-",$DATA[entry_basename]);
$categoriaBasenameFinal=str_replace("_","-",$DATA[category_basename]);
$link=$link="http://www.periodicoabc.mx/".$DATA[blog_site_path]."/".$categoriaBasenameFinal."/".$creacionNoticiaExploded[0]."/".$creacionNoticiaExploded[1]."/".$basenameFinal.".php";
$asset_Url=substr($DATA[asset_url], 3);
$categoriaNoticia=$DATA[category_label];
$imagen="http://www.periodicoabc.mx/".$DATA[blog_site_path]."/".$asset_Url;
$imagenesNum=$DATA[contadorImagenes];
if($imagenesNum > 1){$galeriaIntegrada='<span class="ni-type nc-gallery"></span>';}else{$galeriaIntegrada='';}
$titulo=$DATA[entry_title];
$text=strip_tags($DATA[entry_text]);
$text = preg_replace("/^\W*((\w[\w'-]*\b\W*){1,16}).*/ms", '\\1', $text);
?>
{ "id":
"<? echo $entry_id; ?>"
, "titulo":
"<? echo $titulo; ?>"
, "fecha":
"<? echo $creacionNoticia; ?>"
, "imagen":
"<? echo $imagen; ?>"
, "video":
"<? echo $link; ?>"}
<?}?>
<?}?>
],
"pagina":
"<? echo "1"; ?>"
"seccion":
"<? echo "noticias"; ?>"
Look at json_encode function
$data = array(
'field' => 'value',
'id' => $entry_id
);
$result = json_encode($data);
echo $result;
{"field":"value", "id":7}
Also I turn on my telepathy mode and prepare a query for you :)
SELECT e.entry_id, e.entry_title, e.entry_text, e.entry_created_on, e.entry_basename,
b.blog_name AS blog_name,
b.blog_site_path AS blog_site_path,
c.category_label AS category_label,
c.category_basename AS category_basename,
COUNT(a.asset_url) AS contadorImagenes,
a.asset_url AS asset_url,
a.asset_file_ext AS asset_file_ext
FROM mt_entry e
INNER JOIN mt_placement p ON p.placement_entry_id = e.entry_id
INNER JOIN mt_category c ON c.category_id = p.placement_category_id
INNER JOIN mt_blog b ON b.blog_id = e.entry_blog_id
INNER JOIN mt_objectasset oa ON oa.objectasset_object_id = e.entry_id
INNER JOIN mt_asset a ON a.asset_id = oa.objectasset_asset_id
WHERE e.entry_id = 7
GROUP BY e.entry_id
ORDER BY e.entry_created_on DESC
LIMIT 0, 16
try it
Your problem here is that your are trying to reinvent the wheel. PHP comes with a perfect function called: "json_encode".
Just put your data in it (array, object ...) and it will transform it for you. In your case, do the following:
$data = array(
'id' => $id,
'titulo' => $titulo,
'fecha' => $fecha
);
echo json_encode($data);