display 2 or 3 images per row using php mysql - php

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>';
} ?>

Related

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

fetch data in rows/column using php

I want to make 4 columns in table. In code all the images are comes in single row and single column. But I want a single row containing 4 columns with 4 images (images fetching from database), then create another row and automatically add next 4 images & so on. I don't know how I do this can anyone please suggest me how I do this.
<form name="form">
<select id="sorting" style="width:140px" onChange="optionCheck()">
<option id="s">---Sort By----</option>
<option value="bydate">Sort By Date</option>
<option value="bytopic">Sort By Topic</option>
</select>
</form>
<br />
</div>
<?php include 'connection.php'; ?>
<div id="showByDefault">
<table style="width:60%">
<tr>
<?php include 'connection.php'; ?>
<div id="showByDefault">
<!--<table style="width:60%"><tr>-->
<?php
$sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
$i=0;
echo "<table><tr>";
while($row=mysqli_fetch_array($sql1))
{
if($i != 0 && $i%4 == 0) {
echo '<tr></tr>';
}
?> <td style="padding:20px;">
<img src="<?php echo $row["thumbnails"]; ?>" /></td><?php
echo '</tr>';
$i++;
}
?></tr></table>
</div>
<div id="hideall">
<div id="topic1">
<?php include 'pdf-sort-by-topic.php'; ?>
</div>
<div id="topic">
<?php include 'pdf-sort-by-date.php'; ?>
</div>
</div>
Try this one 100% working: Nice and easy.
<?php
$sql1=mysqli_query($con,"select * from `insert-n-retrive-pdf` ORDER BY date DESC") or die(mysqli_error($con));
$i = 0;
echo "<tr>";
while($row=mysqli_fetch_array($sql1)) {
if($i != 0 && $i%4 == 0) {
echo "</tr><tr>";
}
?>
<td style="padding:20px;"><img src="<?php echo $row["thumbnails"]; ?>" /></td>
<?php
$i++;
}
?>
Hope this helps!
You can try this code
$query = mysql_query("SELECT * FROM insert-n-retrive-pdf ORDER BY date DESC");
echo '<table width="960">';
$i = 0; //first, i set a counter
while($fetch = mysql_fetch_assoc($query)){
//counter is zero then we are start new row
if ($i==0){
echo '<tr>';
}
//here we creating normal cells <td></td>
$image_name = $fetch['thumbnails'];
$image_location = $fetch['path'];
echo '<td>'.'<img src="'.$image_location.'" alt="'.$image_name.'"/>'.'</td>';
//there is a magic - if our counter is greater then 4 we set counter to zero and close tr tag
if ($i>4){
$i=0;
echo '</tr>';
};
$i++; //$i = $i + 1 - counter + 1
}
echo '</table>';
You can fetch all your images into one-dimensional array and then use function array_chunk(). It will split an array into smaller parts you need. Here's a manual page.
Actually, You can get something like this:
<?php
$images = array();
while($row=mysqli_fetch_array($sql1))
{
$images[] = $row;
}
$images = array_chunk($images, 4);
?>
<?php foreach($images as $imagesChunk): ?>
<tr>
<?php foreach ($imagesChunk as $image): ?>
<td style="padding:20px;">
<a href="<?=$image["path"];?>" target="_blank">
<img src="<?=$image["thumbnails"];?>" />
</a>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<table style="width:60%">
<tr>
<?php
$counter = 0;
$sql1 = mysqli_query($con, "select * from `insert-n-retrive-pdf` ORDER BY date DESC"
) or die(mysqli_error($con));
while($row=mysqli_fetch_array($sql1))
{
?>
<td style="padding:20px;">
<a href="<?php echo $row["path"]; ?>" target="_blank">
<img src="<?php echo $row["thumbnails"]; ?>" />
</a>
</td>
<?php
if($counter == 4)
{
echo "</tr>";
echo "<tr>";
$counter = 0;
}
$counter++;
}
?>
</tr>
</table>

How to Limit the Amount of Columns in HTML

How would I go about Limiting the amount of Columns that an HTML Table can Show.The data is Pulled from an SQL table and it is then displayed in a table but I only want the table to Show 6 In a Row then start a New one Instead it Adds Infinate amounts to the single row how can I get around this?
<?php
$con=mysqli_connect("localhost","Username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM menus");
echo "<table width='400'>";
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
echo'<td>';
?>
<a href="<?php echo $row['link']; ?>" class="tip" alt="<?php echo $row['game']; ?>">
<img src="<?php echo $row['picture']; ?>" alt="" width="150" height="120" class="fade" />
</td>
<?php
}
mysqli_close($con);
?>
</table>
$cols = 0;
echo '<table><tr>';
while($row = fetch()) {
$cols++;
if ($cols >= 6) {
echo '</tr><tr>';
$cols = 0;
}
echo '<td> ...</td>';
}
echo '</tr></table>';

PHP while loop to make a table of images

I am attempting to create a PHP script that will build a table of images from a database. I'm having a hard time figuring out how to properly nest my while loops to make a 6x(how ever many are in the db) table. I understand the concept but I am new to PHP and just can't wrap my head around doing it here.
<?php
mysql_connect("localhost","root","");
mysql_select_db("images");
$res=mysql_query("SELECT * FROM table1");
echo "<table>";
while($row=mysql_fetch_array($res)){
echo "<tr>";
echo "<td>";?>
<img src="<?php echo $row["images1"]; ?>" height="150" width="150">
<?php echo "</td>";
echo "</tr>";}
echo "</table>";
?>
if you keep a count of how many images you have processed you can use if($count % 6 == 0) to test if you are at the 6th item in the list. So your loop would look like the following:
$c = 0; //our count
while($row = mysql_fetch_array($res)){
if(($count % 6) == 0){ // will return true when count is divisible by 6
echo "<tr>";
}
echo "<td>";
?>
<img src="<?php echo $row["images1"]; ?>" height="150" width="150">
<?php
echo "</td>";
$c++; // Note we are incrementing the count before the check to close the row
if(($count % 6) == 0){
echo "</tr>";
}
}

Go to next row when HTML table is full

I am using an HTML table to display data from a MySQL table using PHP. I need it so once the table has 10 columns, it will move on to the next row.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
while($row = mysqli_fetch_array($result))
{
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
How can this be done?
Untested but something like this should work or get you started in a good direction:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr width="100%">';
$x=0;
while($row = mysqli_fetch_array($result))
{
if($x==0){
echo "<tr>\n";
}elseif($x%10){
echo"</tr><tr>\n";
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
$x++;
}
echo "</tr></table>";
mysqli_close($con);
?>
Add a counter to your loop starting at one.
Each time through the loop if the remainder after dividing the counter value by 10 is 1
add the <tr>. If the remainder is 0 then add a </tr> Then after the loop a </tr> if the remainder is not evenly divisible by 10.
<?php
echo '<table width="100%" border="1px"><tr width="100%">';
$i = 0;
while($row = mysqli_fetch_array($result))
{
$i++;
?>
<?php if ($i%10 ==1): ?><tr><?php endif; ?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php if ($i%10 ==0): ?></tr><?php endif; ?>
<?php
}
if ($i%10 != 0) echo "</tr>";
echo "</tr></table>";
Using modulo (%)
After each 10th cell, if a new cell is added, the current row is closed and a new row is opened first, before outputting the cell.
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = 0;
while($row = mysqli_fetch_array($result))
{
if ($cell++ % 10 == 0 && $cell > 1)
{
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
The extra condition && $cell > 1 seems to be a little odd, but without it, you will get an empty row to start with. Eliminating it by putting ++ before $cell will cause the first row to be 9 cells instead of 10. Putting $cell > 0 && in front of the modulo will cause cell never to be incremented, because the first part of the expression is always false. Moving the if to execute it after outputting the cell, would cause the risk of ending with an empty row. It could be solved using a do..while loop, but you'd have to check up front if you have one row at least.
Long story short: use the code above. :)
Using a simple counter and reset it after each row
I think it's even more readable without the modulo, though you'd have to initialize $cell to -1 to prevent the first row to be 9 cells. Nevertheless, I think this is cleaner:
<?php
$result = mysqli_query($con,"SELECT * FROM table");
echo '<table width="100%" border="1px"><tr>';
$cell = -1;
while($row = mysqli_fetch_array($result))
{
if (++$cell == 10)
{
$cell = 0;
?>
</tr><tr>
<?php
}
?>
<td width="10%"><?php echo $row['Name']; ?></td>
<?php
}
echo "</tr></table>";
mysqli_close($con);
?>
<table>
<tr>
<?php
$endRow = 0;
$columns = 10; // number of columns
$hloopRow1 = 0;
do {
if($endRow == 0 && $hloopRow1++ != 0) echo "<tr>";
?>
<td>
<?php echo $row['Name']; ?>
</td>
<?php $endRow++; if($endRow >= $columns) { ?>
</tr>
<?php $endRow = 0; }
} while ($row = mysql_fetch_assoc($result));
if($endRow != 0) {
while ($endRow < $columns) {
echo("<td> </td>");
$endRow++;
}
echo("</tr>");
}
?>
</table>
This should work fine. Hope this helps.

Categories