mercredi 5 juin 2019

Display data from mysql into html

I'm trying to pull data from MYSQL database and place that data into html tags so that it can be edited. What do I need to do to pull all data needed and maybe even assign a variable to it so that it can be used later in php?

I have tried using foreach loops to iterate through the data of the database, but using that I am only able to echo out the data using php's echo. That isn't helpful since I need my data inside html's tags.

$podatci = array();
$displaymax ="SELECT MAX(id) AS max FROM oglasi";
#Select max id to get total num of ads
$rowSQL = mysqli_query($conn, $displaymax) or die ('Error in query.');
     $row = mysqli_fetch_array($rowSQL);
     $largestNumber = $row['max'];
     #Print the biggest num (id) from database --> since database iz 
     automatically increasing by 1, the #highest number is the number of 
     adds
     echo "<br>";
     while ($largestNumber > 0) {
        #while that biggest number is not 0, iterate through the database
        $displaydate = "SELECT * FROM oglasi WHERE id LIKE 
        '$largestNumber'";
        // Take everything from database so that newest ads are on the 
        top
        $querydate = mysqli_query($conn, $displaydate) or die('Error 
        querying database.');
        $result = mysqli_fetch_row($querydate);

        $largestNumber -= 1;
        foreach ((array) $result as $result) {
               //For every result append it to a variable
               print $result . "<br>";
               array_push($podatci, $result);
          } ?>

        <h1><?php echo $result["id"]?></h1>

I want it to put the first ad's id inside h1, then the ad's name in next h and so on, when it finishes with that, I want it to go and do the same for the rest, I can do it by hard coding it in but i wanted to know is there a simpler way ?




Aucun commentaire:

Enregistrer un commentaire