Hello I am working on a University project where I have a Database which contain elements that I want to retrieve a convert into a PHP array which will be used to populate an Html file
Below is an example of the array I want my to retrieve from my database which I cant since I am really new to this
<?php
$slides = array(
array(
'id' => 1,
'title' => '',
'intro' => '',
'imgUrl' => '',
'content' => ''
),
array(
'id' => 2,
'title' => '',
'intro' => '',
'imgUrl' => '',
'content' => ''
?>
This array will be used to populate the HTML code below:
<?php foreach ($slides as $slide): ?>
<div class="swiper-slide" id="slider<?= $slide['id']; ?>">
<div class="card">
<div class="card_image">
<img src="<?= $slide['imgUrl']; ?>">
</div>
<div class="card_info">
<h3 class="card_title">
<?= $slide['title']; ?>
</h3>
<p>
<?= $slide['intro']; ?>
</p>
<button data-modal-target="#modal<?= $slide['id']; ?>" class="card_btn">
<h4>Learn More</h4>
</button>
</div>
</div>
</div>
<?php endforeach; ?>
My question is if someone can help me retrieve the data from my database and display it as per my PHP array list so that I can populate my HTML code, I have already established the connection with the db
What I have tried:
<?php
$slides = array(
array(
'id' => 1,
'title' => '',
'intro' => '',
'imgUrl' => '',
'content' => ''
),
array(
'id' => 2,
'title' => '',
'intro' => '',
'imgUrl' => '',
'content' => ''
?>
<?php foreach ($slides as $slide): ?>
<div class="swiper-slide" id="slider<?= $slide['id']; ?>">
<div class="card">
<div class="card_image">
<img src="<?= $slide['imgUrl']; ?>">
</div>
<div class="card_info">
<h3 class="card_title">
<?= $slide['title']; ?>
</h3>
<p>
<?= $slide['intro']; ?>
</p>
<button data-modal-target="#modal<?= $slide['id']; ?>" class="card_btn">
<h4>Learn More</h4>
</button>
</div>
</div>
</div>
<?php endforeach; ?>
I have tried using this method but it does not seem to work:
$sql = "SELECT (id, title, imgUrl, content, intro) from slider";
$data= $conn->query($sql);
if ($slides->num_rows > 0) {
$slides = $data->fetch_assoc();
foreach($slides as $slide){
echo $slide['id'];
}
}