<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET");
header('Access-Control-Max-Age: 86400');
header("Allow: GET");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
    die();
} ?>

{
	"categorias":
[

<?php
include 'url_amigable.php';
$c = (int)$_GET['c'];
$orden_var = 900;
if ($c == '') {$c = 0;};

include 'concha.php';
mysql_connect ("$host2", "$usuario2", "$contra2");
mysql_select_db ("$basedatos2");
$sql = "SELECT * FROM `categorias` WHERE `home` != 0 ORDER BY `orden` LIMIT 4";
$consulta = mysql_query ($sql);
$total_r = mysql_num_rows($consulta);
while ($row = mysql_fetch_array($consulta))
{
$ID = $row["ID"];
$color = $row["color"];
$nombre = $row["nombre"];
$nn = $nn + 1;

                        
?>
            {
                "id": "<?php echo $ID?>",
                "color": "<?php echo $color ?>",
                "nombre": "<?php echo $nombre ?>",
                "articulos":[
                <?php
                    $sql2 = "SELECT * FROM `notas` WHERE `cat` = '$ID' ORDER BY `ID` DESC LIMIT 5";
                    $consulta2 = mysql_query($sql2);
                    $total_r2 = mysql_num_rows($consulta2);
                    $nn2 = 0;
                    while ($row2 = mysql_fetch_array($consulta2))
                    {
                        $ID2 = $row2["ID"];
                        $titulo2 = $row2["titulo"];
                        $bajada2 = $row2["bajada"];
                        $bajada2 = str_replace('"', "'", $bajada2);
                        $bajada2 = str_replace('<br />',' ', $bajada2);
                        $titulo2_mod = json_encode($titulo2);
                        $bajada2 = json_encode($bajada2);
                        $foto2 = $row2["foto"];
                        $carpetafoto2 = $row2["carpetafoto"];
                        $ext_img2 = $row2["ext_img"];
                        
                        $nn2 = $nn2 + 1;
                ?>
                
                {
                    "id": "<?php echo $ID2?>",
                    "titulo": <?php echo $titulo2_mod ?>,
                    "foto": "<?php if($foto2 == null){
                          
                        $sql3 = "SELECT * FROM `fotosnotasfull` WHERE `ID_nota` = '$ID2' LIMIT 1";
                        $consulta3 = mysql_query($sql3);
                        while ($row3 = mysql_fetch_array($consulta3))
                        {
                            $foto3 = $row3["img"];
                            echo $foto3;
                        } mysql_free_result($consulta3);
                        
                    } else {
                            echo $dom."/".$carpetafoto2."/thumbnail_".$foto2.$ext_img2;
                    }
                    ?>",
                    "bajada": <?php echo $bajada2 ?>
                    
                }
                
                <?if ($total_r2 == $nn2) {} else {echo ',';} 
                            
                    } mysql_free_result($consulta2);?>
                ],
                "publicidad": [
                    <?php
                    
                    $sql2 = "SELECT * FROM `publicidades_gif` WHERE `lugar` = '$orden_var'";
                    $consulta2 = mysql_query($sql2);
                    $total_r2 = mysql_num_rows($consulta2);
                    
                    $nn2 = 0;
                    while ($row2 = mysql_fetch_array($consulta2))
                    {
                        $ID2 = $row2["ID"];
                        $lugar2 = $row2["lugar"];
                        $img2 = $row2["img"];
                        $carpetafoto2 = $row2["carpetafoto"];
                        $url2 = $row2["url"];
                        $orden_var = $orden_var + 1;
                        $nn2 = $nn2 + 1;
                        
                        $newstring = substr($img2, -3);
                    ?>
                        {
                            "id": "<?php echo $ID2?>",
                            "lugar": "<?php echo $lugar2 ?>",
                            "img": "<?php if ($newstring == 'jpg' || $newstring == 'gif' || $newstring == 'png'){echo $dom."/".$carpetafoto2."/".$img2;} ?>",
                            "mp4": "<?php if ($newstring == 'mp4'){echo $dom."/".$carpetafoto2."/".$img2;}?>",
                            "url": "<?php echo $url2 ?>"
                        }
                
                    <?if ($total_r2 == $nn2) {} else {echo ',';} 
                    } mysql_free_result($consulta2);?>
                ]

        }
        <? if ($total_r == $nn) {} else {echo ',';} 
          } mysql_free_result($consulta);
        ?>
]
}
