<?php
// Requiere: $conn (mysqli) conectado + $dom, $ID_p, $domold (si aplica)

$rand = rand(6500, 98745);

// (1) Bandera de dominio (mantengo tu lógica)
$dom_bandera = $dom ?? '';
if (!empty($domold)) {
    $dom_bandera = $dom;
    $dom = $domold;
}

// Sanitizar ID (por si viene raro)
$ID_p = (int)($ID_p ?? 0);

// Helper: quitar esquema (https:/http:) como hacías
$strip_scheme = static function (?string $u): string {
    $u = (string)$u;
    $u = str_replace(['https:', 'http:'], '', $u);
    return $u;
};

// Traer 1 registro
$img = array_fill(0, 10, '');
$url = '';

if ($ID_p > 0) {
    $stmt = $conn->prepare("SELECT img0,img1,img2,img3,img4,img5,img6,img7,img8,img9,url FROM publicidades_slide WHERE ID = ? LIMIT 1");
    if ($stmt) {
        $stmt->bind_param("i", $ID_p);
        $stmt->execute();
        $res = $stmt->get_result();
        if ($res && ($row22 = $res->fetch_assoc())) {
            for ($i = 0; $i <= 9; $i++) {
                $k = "img{$i}";
                $img[$i] = $row22[$k] ?? '';
            }
            $url = $row22['url'] ?? '';
        }
        $stmt->close();
    }
}
?>

<div id="MySlider<?php echo (int)$rand; ?>"
     class="flickity-carousel"
     data-flickity='{ "lazyLoad": true, "autoPlay": 6500, "adaptiveHeight": true, "pageDots": false, "wrapAround": true, "freeScroll": false }'>

    <?php
    $href = $strip_scheme($url);

    for ($i = 0; $i <= 9; $i++) {
        if (empty($img[$i])) { continue; }
        ?>
        <div class="carousel-cell4">
            <a target="_blank" href="<?php echo htmlspecialchars($href, ENT_QUOTES, 'UTF-8'); ?>">
                <img class="carousel-cell-image4"
                     data-flickity-lazyload-src="<?php echo htmlspecialchars($dom . '/' . $img[$i], ENT_QUOTES, 'UTF-8'); ?>"
                     width="100%" />
            </a>
        </div>
        <?php
    }
    ?>

</div>

<?php
// (2) restaurar dominio
$dom = $dom_bandera;
?>