Data not displaying correctly - php

I want my data to be displayed in a row.
Inside my database there are a lot iPhone pictures and some text associated to that particular picture.
I want that data to be shown left to right , I tried and it's working fine for picture NOT for text.
I would like the text to be displayed from left to right instead of top to bottom.
<h2 class="apple">Apple</h2>
<br>
<?php include 'config.php';
$sql="SELECT * FROM apple";
$result=mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0){
while ($fetch=mysqli_fetch_assoc($result)) {
?>
<img src="<?php ECHO $fetch["url"];?>"width="100" height="100" alt="">
<h1> Hello</h1>
<?php
}
}
?>

You can try to display the data in a table with table rows and table data inside it.
<?php
include ('config.php');
echo '<h2 class="apple">Apple</h2>';
$sql = "SELECT * FROM apple";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
echo '<table>';
while ($fetch = mysqli_fetch_all($result)) {
echo '<tr>';
foreach ($fetch as $values) {
echo '<td>'. $values['index of image'] . '</td>';
}
echo '</tr>';
echo '<tr>';
foreach ($fetch as $values) {
echo '<td>'. $values['index of names'] . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
This should get the job done.

Related

How to get my table data in a row without tables

I have a database and I am trying to display data from the table all in one box.
I am not sure if there is another way besides tables but I am trying to do something like this.
I am using roblox as an example.
Example:
Right now this is my code for the table.
<div class="row2">
<div class="column">
<?php
include_once("includes/config.php");
$sql = "SELECT * FROM communities";
$result = $conn->query($sql);
if ($result->num_rows > 1) {
while($row = $result->fetch_assoc()) {
echo '<hr />';
echo '<table>';
echo '<tr><td>Name: </td><td>'.$row["name"].'</td></tr>';
echo '<tr><td>Discription:</td><td>'.$row["description"].'</td></tr>';
echo '<tr><td></td><td><img src="'.$row["logo"].'" width="100px" /></td></tr>';
echo '</table>';
echo '<button name="button">Join</button>';
echo '<hr />';
}
}
?>
</div>

PHP - How to use Foreach to turn a list into a column rather than a row of items?

I have a database, and am trying to get a list using PHP with foreach, but the list are returning like this:
RiquelmeMacielLewBrMarcuus
But it like so:
RiquelmeMaciel
LewBr
Marcuus
My code:
<?php
include_once("conexao.php");
$select = "SELECT * FROM usuarios ORDER BY id";
$query = mysqli_query($conn, $select);
$rows = mysqli_fetch_assoc($query);
?>
<?php foreach($rows as $row){
echo '<td class="mdl-data-table__cell--non-numeric name">'.$row['nome'].'</td>';
} ?>
You need to insert table rows to create the rows:
<?php
//Incluindo a conexão com banco de dados
include_once("conexao.php");
$result_usuario = "SELECT * FROM usuarios ORDER BY id";
$resultado_usuario = mysqli_query($conn, $result_usuario);
$resultado = mysqli_fetch_assoc($resultado_usuario);
foreach($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric"></td>';
echo '<td class="mdl-data-table__cell--non-numeric"></td>';
echo '</tr>';
}
Now you just need to add in your variables for 'EMAIL' and 'SENHA'.
The problem in your foreach cycle - for eache element you output tag, but is a table cell tag.
If you want fill first column in your 3-column table, you must do something like this:
<?php foreach($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td>second column</td>';
echo '<td>third column</td>';
echo '</tr>';
} ?>
Your are displaying your data the wrong way, your <td> should be inside <tr> which should be inside a <table>:
<table>
<tr>
<th>Nome</th>
<th>Email</th>
<th>Senha</th>
</tr>
<?php
foreach ($resultado_usuario as $teste){
echo '<tr>';
echo '<td class="mdl-data-table__cell--non-numeric name">'.$teste['nome'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric email">'.$teste['email'].'</td>';
echo '<td class="mdl-data-table__cell--non-numeric senha">'.$teste['senha'].'</td>';
echo '</tr>';
}
?>
</table>
The data will then be displayed as you wanted it. Have a look at HTML - table.
If you want to change the order of your data from your SQL query, use the ORDER BY ... ASC|DESC.

How to check table cell in database and output results?

I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post

crud delete not working anymore

i used the crud delete method and it works for me, but when i tried it with a different database and table it keeps saying "Invalid argument supplied for foreach()"
this is my "vacature-verwijderen.php" where you should be able to delete it
<div>
<table id="tabel">
<thead>
<tr>
<th>Functie</th>
<th>Omschrijving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM vacature ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['functie'] . '</td>';
echo '<td>'. $row['omschrijving'] . '</td>';
echo '<td>'. $row['salaris'] . '</td>';
echo '<td width=250>';
echo '<a class="button " href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="button" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
and then when you click delete you go to "delete.php" which looks like this
<?php
require 'database.php';
$id = 0;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( !empty($_POST)) {
$id = $_POST['id'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM vacature WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
Database::disconnect();
header("Location: vacature-verwijderen.php");
}
?>
i don't know what is wrong since it used to work, also my database table looks like this
foreach ($pdo->query($sql) as $row) {
That's what is wrong. $pdo->query($sql) already returns your $row.
Use a while instead so that it do the query and returns you the new row as long as there is data to fetch :
while ($row = $pdo->query($sql)) {
Also :
$sql = "DELETE FROM vacature WHERE id = ?";
The table you provided doesn't contain any field named id, although there is one named vacature_id. You might want to change that too :
$sql = "DELETE FROM vacature WHERE vacature_id = ?";
Don't hesitate to come back if it doesn't solve your issue.

display 2 or 3 images per row using php mysql

hi i'm disaplyed images from mysql db table but it displays on by one means one row has one image. but i need 3 or 4 image per row. my coding is below. please give some idea.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
while($res=mysql_fetch_array($result))
{
?>
<?php echo $res['description'];?></p>
<img src="<?php echo $res['image'];?>" width="80" height="80"/>
<?php } ?>
Do it in table like this, You might need to fix it a little bit, but it way how it will work
<table>
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
$count = 0;
while($res=mysql_fetch_array($result))
{
if($count==3) //three images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<?php echo $res['description'];?></p>
<img src="<?php echo $res['image'];?>" width="80" height="80"/>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
</table>
use a <table> to display.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
$count = 0;
echo '<table>';
while($res = mysql_fetch_array($result))
{
if($count % 2 == 0) echo '<tr>';
?>
<td>
<p><?php echo $res['description'];?></p>
<img src="<?php echo $res['image']; ?>" width="80" height="80"/>
</td>
<?php
if($count % 2 == 0) echo '</tr>';
} ?>

Categories