<? header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
header("Access-Control-Allow-Methods: GET");
header("Allow: GET");
$method = $_SERVER['REQUEST_METHOD'];
if($method == "OPTIONS") {
    die();
}?>

{

	"cancion":

[

<?
include 'url_amigable.php';
include 'concha.php';

$c = (int)$_GET['c'];
$cat = (int)$_GET['cat'];

$i_publi = 10000003;

if ($c == '') {
    $c = 0;

} else {
    $i_publi = $i_publi + $c;
}



        mysql_connect ("$host2", "$usuario2", "$contra2");
        mysql_select_db ("$basedatos2");

            if ($cat == '') {
                $sql = "SELECT * FROM `notas` WHERE `destacado` = 'no' && `home` = 'si' ORDER BY `ID` DESC LIMIT $c,10";

            } else {
                $sql = "SELECT * FROM `notas` WHERE `destacado` = 'no' && `home` = 'si' && `cat` = $cat ORDER BY `ID` DESC LIMIT $c,10";
            }


        $consulta = mysql_query ($sql);
        $total_r = mysql_num_rows($consulta);
        while ($row = mysql_fetch_array($consulta))
        {
            $ID = $row["ID"];
            $ID_cancion = $row["ID"];
            $i_publi = $i_publi + 1;
            $titulo = $row["titulo"];
            $bajada = $row["bajada"];
            $foto = $row["foto"];
            $carpetafoto = $row["carpetafoto"];
            $img_ext = $row["ext_img"];
            $destacado = $row["destacado"];
            $cat = $row["cat"];
            $dia = $row["d"];
            $mes = $row["m"];
            $ano = $row["a"];
            $nn = $nn + 1;



            if ($cat == 0) {} else {
                $sql21 = "SELECT * FROM categorias WHERE ID = '$cat'";
                $consulta21 = mysql_query ($sql21);
                while ($row21 = mysql_fetch_array($consulta21))
                {
                    $categ = $row21["nombre"];
                    $color_cat = $row21["color"];
                }
                mysql_free_result($consulta21);
            }




            $titulo = str_replace('"', "'", $titulo);
            $bajada = str_replace('"', "'", $bajada);
            $bajada = str_replace('<br />',' ', $bajada);
            $bajada = json_encode($bajada);
            ?>

                {
                    "id": "<?php echo $ID_cancion?>",
                    "titulo": "<?php echo $titulo ?>",
                    "bajada": <?php echo $bajada ?>,
                    "fecha": "<?php echo $dia;?>/<?php echo $mes;?>/<? echo $ano;?>",
                    "foto": "<?php if ($foto != ''){ echo $dom."/".$carpetafoto."/thumbnail_".$foto.$img_ext;}?>",
                    "publicidad": [<?php

                        $nn2 = 0;
                        $sql2 = "SELECT * FROM `publicidades_gif` WHERE `publicidades_gif`.`lugar` = '$i_publi'";
                        $consulta2 = mysql_query ($sql2);
                        $total_r2 = mysql_num_rows($consulta2);
                        while ($row2 = mysql_fetch_array($consulta2)) {
                            $img = $row2["img"];
                            $url = $row2["url"];
                            $lugar = $row2["lugar"];
                            $carpetafoto2 = $row2["carpetafoto"];
                            $nn2 = $nn2 + 1;

                            if ($i_publi == $lugar){

                                            ////////////GIF

                                if ($total_r == $nn2 || $nn2 == 1) {} else {echo ',';}

                                ?>
                                {
                                "publi_src":"<? echo $dom."/".$carpetafoto2."/".$img;?>",
                                "publi_url":"<?php echo $url?>",
                                "lugar":"<?php echo $lugar?>"
                                }
                                <?
                            }

                        }

                        mysql_free_result($consulta2);


                    ?>],
                    "categoria": "<?php echo $categ ?>",
                    "color": "<?php echo $color_cat ?>"

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

            <?

        } mysql_free_result($consulta);

?>]}
