itmo-php-course/engine/app/template/portial_music.php

43 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use Shalex\Engine\Db;
// Подключение к базе данных
try {
$link = Db::connection(CONFIG_DB);
} catch (\Doctrine\DBAL\Exception $e) {
die("Ошибка подключения к базе данных: " . $e->getMessage());
}
// Получаем параметры пагинации
$offset = isset($_GET['offset']) ? (int)$_GET['offset'] : 0;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 5;
// Запрос данных из таблицы files с пагинацией
try {
$sql = "SELECT download_link FROM files LIMIT :limit OFFSET :offset";
$stmt = $link->prepare($sql);
$stmt->bindValue('limit', $limit, \Doctrine\DBAL\ParameterType::INTEGER);
$stmt->bindValue('offset', $offset, \Doctrine\DBAL\ParameterType::INTEGER);
$result = $stmt->executeQuery();
$audioFiles = $result->fetchAllAssociative();
} catch (\Doctrine\DBAL\Exception $e) {
die("Ошибка при запросе к базе данных: " . $e->getMessage());
}
// Преобразуем массив в простой список путей
$audioFiles = array_column($audioFiles, 'download_link');
// Рендеринг треков
foreach ($audioFiles as $index => $file):
if (file_exists($file)) : ?>
<div class="audio-player">
<audio id="audio-<?php echo $index; ?>" controls="controls">
<source src="<?php echo htmlspecialchars($file); ?>" type="audio/mpeg">
Ваш браузер не поддерживает HTML5 audio.
</audio>
</div>
<?php else: ?>
<p>Файл "<?php echo htmlspecialchars($file); ?>" не найден.</p>
<?php endif;
endforeach;
?>