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>
Related
I have an PHP for loop; which looks at a number in database and outputs the tables rows to number of times.
Here is my HTML:
<?php
for($x=0; $x < 5; $x++){
$row_count =1;
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="yellow-sign" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>
The above code display this:
How I can assign unique class to each of the rows?
Labelled Image
Example:
<?php foreach($data as $row) { ?>
<tr id="data<?php echo $row['id']; ?>"> </tr>
<?php } ?>
I hope it will help you.
If you are trying to get unique class to tr then do this
for ($x = 0; $x < 5; $x++) {
$row_count = 1;
$randClass = rand(1111,9999).$row_count;
?>
<table class="table table-bordered" id="ticktable">
<tr class="<?php echo $randClass; ?>">
<?php
for ($w = 0; $w < 5; $w++) {
?>
<td>
<img class="yellow-sign <?php echo $randClass; ?>" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count++;
}
rand(1111,9999) will generate random number between 1111 and 9999
EDIT
i have also added $row_count so that the number can not be repeated even if there is large data. What I've done is: <tr class="<?php echo rand(1111,9999).$row_count; ?>">
UPDATE
first store the random string in a variable $randClass = rand(1111,9999).$row_count;
then echo out where you want the variable like this:
$randClass = rand(1111,9999).$row_count;
and <img class="yellow-sign <?php echo $randClass; ?>" >
Here is the updated code for you. Check this
<?php
$color_class[] = array("yellow-sign","red-sign","green-sign","blue-sign","black-sign");
$row_count = 0;
for($x=0; $x < 5; $x++){
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="<?php echo $color_class[$row_count]; ?>"
src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>
I'm trying create loop in my table, there is 4 item, when column is 3 then create new row . The current output is like this:
x
x
x
x
Here's my code:
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if ($i <= 3) { ?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php }
}
echo "</tr>";
?>
</table>
What i expected is like this:
x|x|x
x
Thank you.
In the comment section of your question, Sirko is right.
Anyway you can do this like below;
<?php
$i = 0;
foreach ($list_items as $item) {
if($i % 3 == 0)
echo '<tr>';
echo '<td> bla bla bla </td>';
if($i % 3 == 0)
echo '</tr>';
$i++;
}
Change your code to below, it should work.
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if($i%3==0) echo echo "</tr><tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
</td>
<td>
<?php echo $item['qty'] ?>
</td>
<?php
}
if($i%3!=0)
echo "</tr>";
?>
</table>
use array_chunk()
<?php
foreach (array_chunk($list_items,3) as $items) {
echo '<tr>';
foreach($items as $item){
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
}
echo '</tr>';
}
?>
try this ==>
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item) { // there is 4 item
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will open
echo "<tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will close
echo "</tr>";
$i++;
}
?>
</table>
The course name php is available in the table one time but when I retrieve it from table it printing multiple time. I have tried the following code - please help me what's wrong with the code? (if I remove the drop down which is available below the heading it is printing single time in the table)
<?php
include("session.php");
?>
<?php
include("../view/common/head.php");
?>
<?php
include('../view/common/tab.php');
?>
<body class="bg-color">
<div class="container">
<h1> List Of Courses</h1>
<br/>
<?php
if (isset($_SESSION['message1']))
{
echo $_SESSION['message1'];
unset($_SESSION['message1']);
}
?>
<?php
if (isset($_SESSION['courseupdated']))
{
echo $_SESSION['courseupdated'];
unset($_SESSION['courseupdated']);
}
?>
<?php
if (isset($_SESSION['deletecourse']))
{
echo $_SESSION['deletecourse'];
unset($_SESSION['deletecourse']);
}
?>
<?php
if (isset($_SESSION['notdelete']))
{
echo $_SESSION['notdelete'];
unset($_SESSION['notdelete']);
}
?> <div class="col-md-6 form-group">
<select class="form-control">
<?php
include('../model/functions.php');
$table="courses";
$condition="";
$drop=Selectdata($table,$condition);
foreach($selectarray as $drop)
{
echo '<option value="'.$drop['course_id'].'">'.$drop['course_name'].'</option>';
}
?>
</select>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>Sno</th>
<th>Course Name</th>
<th>Course Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$per_page=5;
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page=1;
}
$table="courses";
$per_page=5;
$start_from = ($page-1) * $per_page;
$condition="limit ".$start_from.",".$per_page." ";
$limit=Selectdata($table,$condition);
if($rowcount==0)
{
echo "no records found";
}
else
{
$sno=($page-1) * $per_page;
foreach($selectarray as $course){
$sno++;
echo '<tr>';
echo '<td>'.$sno.'</td>';
echo '<td>'.$course['course_name'].'</td>';
echo '<td>'.$course['course_description'].'</td>';
echo '<td><button type="submit">edit</button><button Onclick="return ConfirmDelete();" type="submit">Delete</button></td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
Click here to add new course
<br/>
<br/>
Toggle Menu
</div>
<?php
$table = "courses";
$condition=" ";
$select=Selectdata($table,$condition);
$total_records = $rowcount;
$total_pages = ceil($total_records / $per_page);
echo '<div class="text-center">';
echo '<ul class="pagination">';
echo "<li><a href='courselist.php?page=1'>".'<<'.'</a></li>';
for ($i=1; $i<=$total_pages; $i++) {
echo "<li><a href='courselist.php?page=".$i."'>".$i.'</a></li>';
}
echo "<li><a href='courselist.php?page=$total_pages'>".'>>'.'</a></li>';
echo "</ul>";
echo '</div>';
?>
<?php
include("../view/common/footer.php");
?>
functions.php
function Selectdata($table,$condition="")
{
global $conn,$result,$selectarray,$rowcount;
$sql="SELECT * from ".$table." ".$condition." ";
$result=$conn->query($sql);
$rowcount=$result->num_rows;
while($row=$result->fetch_assoc())
{
$selectarray[]=$row;
}
return $result;
}
I hope this code is not intended to be used in a real project.
Just add
$selectarray = array();
before
while
in Selectdata function definition.
What happens is that each time you call that function it just amends the full rowset to the same array as it is global.
Try this :
foreach($selectarray as $row)
{
echo '<option value="'.$row['course_id'].'">'.$row['course_name'].'</option>';
}
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>';
} ?>
I have a database table and that table has 6 rows. What I want is to display that 6 rows in a html page using a 3 column and 2 row table.
I know how to work with php arrays and while loops. My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row.
this is what i have tried but didn't work
<div id="maincontent">
<!-- class one -->
<?php
$getSection = getSection();
$i=0;
while($allSection = mysql_fetch_array($getSection)){
?>
<div class="subconent">
<table width="937" border="0">
<tr>
<td>
<div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div>
</td>
</tr>
</table>
</div>
<?php
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td> </td>
<td> </td>
<td> </td></tr>
<tr><div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div><td>
<?php }
} ?>
</div>
Use modulo operator (%):
http://www.devchunks.com/web-development/using-the-php-modulus-operator/
something like this:
<table>
<?php
$i = 0;
while ( $row = mysql_fetch_array($result) ){
if ($i % 3 == 0){
echo '<tr>';
}
echo '<td>'.$row['column_name'].'</td>';
if ($i % 3 == 2){
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
echo '</tr>';
}
?>
</table>
At its base, you'll need something like this:
<table>
<tr>
<?
$count = 0;
foreach ($row) {
echo "<td>" . $row["value"] ."</td>";
$count++;
if (($count % 3) == 0) && ($count > 0) {
echo ("</tr><tr>");
}
}
?>
</tr>
</table>
Start printing out the header of your table, and then begin iterating through the dataset. Keep track of how many you've printed out, and if this is the third one, print the HTML to finish this row and start the next one. (I've used %, so it'll wrap on every third entry, not just the first one)
Well, you could correctly fetch those informations in your sql-query ( just one example that could fit http://en.wikibooks.org/wiki/MySQL/Pivot_table ).
Or simply fetch everything into PHP arrays.
Oldschool: mysql_query() and while( $row = mysql_fetch_array() )
Newchool: PDO ( http://de.php.net/manual/en/book.pdo.php )
Awesome! Thanks a lot. It works for me, Zend. You can try something like this.
<table width="1024px" border="0" cellspacing="2" cellpadding="2">
<?php
$i = 0;
foreach ($this->rows as $row )
{
$img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
if ($i % 3 == 0)
{
echo '<tr>';
}
?>
<td align="center">
<img src="<?php echo $img; ?>" width="300" height="215"><br/>
<?php echo $row->gly_title; ?>
</td>
<?php
if ($i % 3 == 2)
{
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0)
{
echo '</tr>';
}
?>
</table>
<?php if ($i++%$_columnCount==0): ?>
<tr>
<?php endif ?>
<td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
<?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
</tr>
<?php endif; ?>
<?php } ?>