Click here to Skip to main content
15,036,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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'];
  }
}
Posted
Updated 1-Jun-21 6:49am

1 solution

Perhaps I missed where you do it but I do not see any code where you make a connection to the database
$data= $conn->query($sql);
. $conn is undefined.

You need to connect to the database to get any data from it.
See here: PHP MySQL Connect to database[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900