João Gonçalves João Gonçalves - 25 days ago 6
MySQL Question

Get specific data from mysql and replicate the boxes

Hey I´m building a website where users can post ads.I already create the publisform so they can post and it goes to the data base, and now i want the data from the database to be posted on the web. But not in a table, I want it to go with a specific layout, like the one I show above in the "box1"
Here is the code i have so far:



<body>

<div id="content">

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "userdata";

$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
echo die("Connection failed: " . $con->connect_error);
}


$result = mysqli_query($con,"SELECT * FROM posts");

echo "
<div class= 'box1'>
<p align='middle'>Mark Cuban</p>
<h3 align='middle'>Playing</h3>
<h3 align='middle' style= 'color:blue'>DATA i WANTT TO ENTER </h3>
<div><p align='middle' >Disponibilidade:</p>
<h3 align='middle' style='color:blue'>DATA i WANTT TO ENTER</h3></div>
<p></p>
<h3 align='middle' style='color:blue'>DATA i WANTT TO ENTER</h3>
<button align='middle' id='submit1' type='submit1'>
contact </button>
</div>";

mysqli_close($con);
?>

</div>
</body>
</html>


My question is: How i can select specific data(like row1/col2) from each field ("DATA i WANT TO ENTER").
My objective then is to replicate this to infinit boxes as my users post the ads.
I wonder if you can gave me any tip about this.
Thank you very much.

Answer

Here I will write the code you need:

$result = mysqli_query($con,"SELECT * FROM posts");
while($row=mysqli_fetch_assoc($result)){ ?>
<div  class= 'box1'>
<p align='middle'><?php echo $row['column1']; ?></p>
<h3 align='middle'><?php echo $row['column2']; ?></h3>
<h3 align='middle' style= 'color:blue'><?php echo $row['column3']; ?> </h3> // and so on
<div><p align='middle' >Disponibilidade:</p>
<h3 align='middle' style='color:blue'>DATA i WANTT TO ENTER</h3></div>
<p></p>
<h3 align='middle' style='color:blue'>DATA i WANTT TO ENTER</h3>
<button align='middle' id='submit1' type='submit1'> 
contact </button>
</div>
<?php
}
?>

This is how to fetch all the rows in a loop and echo each one of them in your HTML code.