PHP while loop to make a table of images - php

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>";
}
}

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

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

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.

PHP Boostrap Accordion - How to display dynamic data?

So I'm trying to get the bootstrap accordion to dynamically show the Top 5 teams judged by most wins, I also use it for other rankings.
The problem is whenever I click tab 2, 3, or 4 it only opens tab 1 in the accordion. I know the issue is from "href='#collapse41'" being the same from every loop. I assume this will require a unique ID for each accordion div tab but I don't know how to implement it correctly. I could be 100% wrong, if so please correct me!
I've done about 15 searches on this and only found one answer, which wasn't very detailed or helpful. Any help or pointers to learn how to do this would be much appreciated.
(The reason I renamed it to #collapse41 is because I have multiple accordions on the same page.)
<div class="accordion" id="accordion2">
<?php
include 'db.php'; //connect to database
$result = mysql_query("SELECT * FROM teams ORDER BY wins DESC LIMIT 5");
$rank = 1;
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) {
echo "<div class='accordion-group'>";
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion4' href='#collapse41'>";
echo "<td>{$row['name']} <br /> </td>";
echo "</a>";
echo "</div>";
echo "<div id='collapse41' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";
echo "<td>Rank: {$rank} <br /> </td>";
echo "<td>Wins: {$row['wins']} <br /> </td>";
echo "<td>Losses: {$row['losses']} <br /> </td>";
echo "</div>";
echo "</div>";
echo "</div>";
$rank++;
}
}
?>
</div>
You have to break the loop up....and loop once to get headings, and twice to get body...
So you need to echo the group div first....
echo "<div class='accordion-group'>";
Then start the loop to output headings...
Then run another loop to output divs....
if (mysql_num_rows($result)) {
//LOOP 1
while ($row = mysql_fetch_assoc($result)) {
//Build heading pieces
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion-".$row['id']." href='#accordian-".$row['id']."'>";
echo "<td>{$row['name']} <br /> </td>";
echo "</a>";
echo "</div>";}
//LOOP 2
//Build inner pieces
while ($row = mysql_fetch_assoc($result)) {
echo "<div id='accordian-".$row['id']."' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";
echo "<td>Rank: {$rank} <br /> </td>";
echo "<td>Wins: {$row['wins']} <br /> </td>";
echo "<td>Losses: {$row['losses']} <br /> </td>";
echo "</div>";
echo "</div>"; }
echo "</div>";
$rank++;
}
}
Im not actually too sure of how your accordian is to be structured, so this may or may not be correct information, but whenever I do twiiter-bootstrap accordians, this is how I do it. If you could post your expected HTML output (template for accordian), that would help me greatly, and I can revamp my answer to suit what you actually need

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