Below is my while loop table, currently it's showing just one item per row. I can't seem to figure out how to get 4 items/columns to show before it shows a new row below. Any help with this would be much appreciated, thank you!
Jerome
<table style="background-color: white">
<?php
while ($row = mysql_fetch_array($rs)) {
?>
Collapse | Copy Code
<tr <?php //if ($i % 2) echo ' style="background-color: #ECECFB;"';?>>
<td class="rows"><font color="4D4D4D" size="3"><? echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank>'.$row["Name"].'</a>' ?><br />
<? echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank><img src='.$row["Picture"].' height=210px width=141px></a>' ?><br />
<? echo '('.$row["Type"].')' ?> <? echo $row["Year"] ?> <? echo $row["Rating"] ?> <? echo date('H:i', mktime(0,$row["Length"])); ?><br />
<? echo $row["Genre"] ?><br />
<? if ($row["Queue"]==='x') {
echo
"<center><a title=Remove From Watch List href='deletequeue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Minus.png'></a></td></font></center>";
}
else {
echo "<center><a title=Add To Watch List href='addqueue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Plus.png'></a></td></font></center>";
}
?>
</td></tr></font>
<?php $i++?>
<?php
}
?>
</table>
Try it
<table style="background-color: white">
<?php
while ($row = mysql_fetch_array($rs))
{
?>
Collapse | Copy Code
<tr <?php //if ($i % 2) echo ' style="background-color: #ECECFB;"';?>>
<td class="rows">
<font color="4D4D4D" size="3"><?php echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank>'.$row["Name"].'</a>'; ?><br />
<?php echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank><img src='.$row["Picture"].' height=210px width=141px></a>'; ?><br />
<?php echo '('.$row["Type"].')' ?>
<?php echo $row["Year"] ;?>
<?php echo $row["Rating"]; ?>
<?php echo date('H:i', mktime(0,$row["Length"])); ?><br />
<?php echo $row["Genre"] ;?><br />
<?php
if ($row["Queue"]=="x") {
echo
"<center><a title=Remove From Watch List href='deletequeue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Minus.png'></a></td></font></center>";
}
else
{
echo "<center><a title=Add To Watch List href='addqueue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Plus.png'></a></td></font></center>";
}
?>
</td></tr></font>
<?php $i++?>
<?php
}
?>
</table>
Related
Multiple deletion used to work just fine before adding the upload page and condition
am sure the problem comes from a in one of the echo's
I didn't manage to figure out a way to get them both working at the same time.
You can check by yourself here: agencedevoyages.tk
Enter the
email: abc#gmail.com
Password: 2
You'll notice that upload does work and multiple deletion doesn't work anymore
Code:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Accueil</title>
</head>
<body>
<?php
if(isset($_SESSION['email']) AND isset($_SESSION['password']))
{
echo "<b>Welcome ".$_SESSION['prenom']." ".$_SESSION['nom']." !";
echo "<div align='right'><a align='right' href='deconnect.php'><button type='button'>Se déconnecter</button></a></div>";
?>
<table border="1" align="center">
<caption><b><h1>La liste des étudiants</h1></caption>
<tr><td align="center"><b>Photo</td>
<td align="center"><b>Matricule</td>
<td align="center"><b>Nom</td>
<td align="center"><b>Prénom</td>
<td align="center"><b>Adress</td>
<td align="center"><b>Birthday</td>
<td align="center"><b>E-mail</td>
<td align="center" colspan="2"><b>Option</td>
<td align="center"><b>Multiple Deletion</td>
<td align="center"><b>Upload</td></tr>
<?php
include 'connect.php';
$sql = "SELECT * FROM etudiant";
$res = mysqli_query($connect, $sql);
if (mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res))
{
echo "<tr><td>";
?>
<?php
if(empty($row['photo']))
{
?>
<img src='photos/image.jpg' width="100" height="100">
<?php
}
else
{
?>
<img src='photos/<?php echo $row['photo'] ?>' width="100" height="100">
<?php
}
echo "</td>";
echo "<td align='center'>".$row['matricule']."</td>";
echo "<td align='center'>".$row['nom']."</td>";
echo "<td align='center'>".$row['prenom']."</td>";
echo "<td align='center'>".$row['adresse']."</td>";
echo "<td align='center'>".$row['date_naissance']."</td>";
echo "<td align='center'>".$row['email']."</td>";
if($_SESSION['email']==$row['email'] OR $_SESSION['email']=='abc#gmail.com')
{
echo "<td><a href='modif.php?id=".$row['id']."'><button type='button'>Edit</button></a></td>";
echo "<td><a href='supp.php?id=".$row['id']."'><button type='button'>Delete</button></a></td>";
echo "<td align='center'><form method='post' action='supp3.php'><input type='checkbox' name='sup[]' value='".$row['id']."'></form></td>";
echo "<td align='center'><form method='post' action='upload.php' enctype='multipart/form-data'><input type='file' name='fichier'><input type='submit' value='Confirm'></td><input type='hidden' name='id' value='".$row['id']."'></td></tr>";
}
}
echo "<tr><td colspan='9'></td><td colspan='1' align='center'><button>Multiple Deletion</button></td></tr></form>";
}
else
{
echo "<tr><td colspan ='10' align='center'>No users.</td></tr>";
$sql = "TRUNCATE etudiant";
$res = mysqli_query($connect, $sql);
}
?>
</table>
<p align="center"><button>Add a user</button></p>
<?php
}
else
{
echo "<div align='center'><b><h1>Not allowed.";
echo "<br><a href='index.php'><button type='button'>Back</button></a></div>";
}
?>
</body>
</html>
I've gotten into trouble while working on a school project, and I'm stuck.
I'm making a website where you can easily search for amusement parks.
I made a while loop with PHP and put information from the DB into the table which I looped. I've put all the information in a TD and I want to create a-href so when I click on one of the first parks it will redirect to an example: example.php?park=1 for the second park example: example.php?park=2.
Here is the while:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
Each pretpark has an ID, the DB table of it is called ParkId.
Can someone help me where I have to put the a href. And how I can do it like its like this example: example.php?park= ParkId
I hope everything is understandable what I just said :/
Thanks,
You can do like this:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "<td><a href='example.php?park=".$pretpark['Parkid']."'> More Details</a></td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
**echo "<td><a href='detailpage.php?id=".$pretpark['ParkId']."'></td>";**
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
I have added a line with bold formatting. You can get clue from this.
instead of the line
echo "<td>".$pretpark['ParkName']."</td>";
it would be something like..
echo "<td>".$pretpark['ParkName']."</td>";
backslashes are needed to escape the quotes so they come out in the HTML and are not interpreted by php
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 } ?>
I'm working on a php gallery. I'm displaying image from mysql db using php, but my images are displaying in one by one. which means the first image in first row and second image in second row. but I want to display my image as 3 or 4 per row. what coding changes can I make. my php code as shown below.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
while($res=mysql_fetch_array($result)){ ?>
<table width='200'>
<tr>
<td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
</tr>
<tr>
<td>
<div id="news-image">
<?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
<?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
</div>
</td>
</tr>
</table>
<?php } ?>
array_chunk() is a function to split an array into a collection of X items for you to loop through without having to keep counters (you can then use array_pad() on the last item in the list if you need padding)
if($array = array_chunk(mysql_fetch_assoc($result),4))
{
foreach($array as $row)
{
echo '<div class="row">';
foreach($row as $col)
{
echo '<div class="item">' . $col['image'] . '</div>';
}
echo '</div>';
}
}
You're outputting a table per image. At minimum, your code should be more like this:
<table>
<tr>
<?php while($res etc...) { ?>
<td>
<img src="<?php echo ......?>" />
</td>
<?php } ?>
</tr>
</table>
Now you'll get all the images in a single row of a single table. Making it have multiple rows is left as an exercise to the OP.
follow this example
<table>
<tr>
<?php
$i = 1;
do{
echo "<td>" . $i . "</td>";
//Num of Columns
if( $i%3 == 0 ){
echo "</tr><tr>";
}
$i++;
}while($i<=10);
?>
</tr>
<table>
will return the result something like you want..
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
?>
<table>
<tr>
<?php
$cnt = 0;
while($res=mysql_fetch_array($result))
{
if($cnt == 3){
echo "</tr><tr>";
}
?>
<td>
<table width='200'>
<tr>
<td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
</tr>
<tr>
<td>
<div id="news-image">
<?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
<?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
</div>
</td>
</tr>
</table>
</td>
<?php
$cnt++;
}
?>
</tr>
</table>
Use the following code.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM images");
?>
<table>
<tr>
<?
$varcount=0;
while($res=mysql_fetch_array($result))
{
$varcount++;
if($varcount == 4) // Count of images per row. 3 or 4
{
$varcount=0;
?>
</tr><tr>
<?
}
?>
<td>
<table width='200'>
<tr>
<td><?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?><?php echo $res['imagename']?><?php echo"</a>"?></td>
</tr>
<tr>
<td>
<div id="news-image">
<?php echo"<a href='indimage.php?imageid=$res[imageid]'>"?>
<?php echo'<img src='.$res['image'].' width="250" height="100">'?><?php echo"</a>"?>
</div>
</td>
</tr>
</table>
</td>
<?php
}
?>
</tr>
</table>
You could instead output the images as a list of divs, or just divs, and then use CSS to show the images in two columns. Your layout should not be that hardwired.
<style>
div.gallery {
width: 650px;
}
div.gallery ul li {
list-style: none;
float: left;
}
div.image {
height: 500px;
width: 300px;
}
</style>
<div class="gallery">
<ul>
<li>
<div class="image">
<span class="image_title">Some title</span><br/>
<img src="foo.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="bar.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="foo.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="bar.png"/>
</div>
</li>
<li>
<div class="image">
<span class="image_title">Another title</span><br/>
<img src="foo.png"/>
</div>
</li>
</ul>
</div>
Result:
Your code should look something like:
<div class="gallery">
<ul>
<?php
include_once("config.php");
$result = mysql_query("SELECT * FROM images");
while($res = mysql_fetch_array($result)) {
?>
<li>
<div class="image">
<a class="image_title" href="indimage.php?imageid=<?php echo $res['imageid']?>"><?php echo $res['imagename']?></a><br/>
<img src="<?php echo $res['image']?>" />
</div>
</li>
<?php
}
?>
</ul>
</div>
Here is the Solution i think you want that:
there are three pages.
1. index.php (which has the form for uploading the image)
2. upload.php (which save the image in directory and its path in database)
3. showimage.php (Finally, which will show the image)
here is the code
(index.php)
<form method="post" action="upload.php" enctype="multipart/form-data">
<label>Choose File to Upload:</label><br />
<input type="hidden" name="id" />
<input type="file" name="uploadimage" /><br />
<input type="submit" value="upload" />
</form>
(upload.php)
<?php
$target_Folder = "upload/"; // directory where images will be saved
$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );
$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );
$file_name = $_FILES['uploadimage']['name'];
if(file_exists('upload/'.$file_name))
{
echo "That File Already Exisit";
}
else
{
// Database
$con=mysqli_connect("localhost","user_name","pasword","database"); // Change it if required
//Check Connection
if(mysqli_connect_errno())
{
echo "Failed to connect to database" . mysqli_connect_errno();
}
$sql = "INSERT INTO image (id,image, image_name)
VALUES ('$uid','$target_Folder$file_name','$file_name') ";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added successfully in the database";
echo '<br />';
mysqli_close($con);
// Move the file into UPLOAD folder
move_uploaded_file( $_FILES['uploadimage']['tmp_name'], $target_Path );
echo "File Uploaded <br />";
echo 'File Successfully Uploaded to: ' . $target_Path;
echo '<br />';
echo 'File Name: ' . $_FILES['uploadimage']['name'];
echo'<br />';
echo 'File Type: ' . $_FILES['uploadimage']['type'];
echo'<br />';
echo 'File Size: ' . $_FILES['uploadimage']['size'];
}
?>
Show Image
(showimage.php)
<?php
$con=mysqli_connect("localhost","user_name","password","database_name"); // Change it if required
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM image " );
while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['image'] . '" width="200" />';
echo'<br /><br />';
}
mysqli_close($con);
?>
Features
It will check the names of file if that name file already exisit it will not uplad the file and alert the user.
Database Structure
id int(4) Auto Increment - image varchar(100) - image_name varchar(50)
i use this foreach to build youtube video gallery but i want to display 3 videos for each line..so how to specify to loop 3 times and then move to the next line and loop 3 times...
i dont need to build all loops in one line..
thanks for helping
table id="tblThumbsLayout" cellpadding="0" cellspacing="6">
<tr>
<?php foreach ($vcats as $vcat) { ?>
<td class="tdImg">
<div>
<?php echo '<iframe class="youtube-player" type="text/html" width=" 200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
<div class="text"><?php echo '<b>'. $vcat['Video']['title'].'</b>' ; ?></div>
</div>
</td>
<?php } ?>
</tr>
</table>
Try this:
<?php
$i = 1;
foreach ($vcats as $vcat) {
if($i%3 == 0){
echo "</tr><tr>";
}
?>
<td class="tdImg">
<div>
<?php echo '<iframe class="youtube-player" type="text/html" width=" 200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
<div class="text"><?php echo '<b>'. $vcat['Video']['title'].'</b>' ; ?></div>
</div>
</td>
<?php $i++; } ?>
I would add a div id=video to each item and in the style sheet for that div id #video use display: inline;
Then set the width of the div to allow 3 per row.
This way you don't have to worry too much about the loop.
<table id="tblThumbsLayout" cellpadding="0" cellspacing="6">
<tr>
<?php $counter = 1; $last = count($vcats)-1; ?>
<?php foreach ($vcats as $vcat) { ?>
<td class="tdImg">
<div>
<?php echo '<iframe class="youtube-player" type="text/html" width=" 200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
<div class="text"><?php echo '<b>'. $vcat['Video']['title'].'</b>' ; ?></div>
</div>
<?php if($counter%3==0 && $counter != $last): ?>
<br>
<?php $counter++; ?>
<?php endif; ?>
</td>
<?php } ?>
</tr>
</table>
Keep a count of the loop iterations. Then using the modulus operator, check whether this iteration divided by 3 has a remainder of 0. If it does, then add a break line or new table row to move onto the next line.
Like so:
<table id="tblThumbsLayout" cellpadding="0" cellspacing="6">
<tr>
<?php
$counter = 1;
foreach ($vcats as $vcat) {
?>
<td class="tdImg">
<div>
<?php echo '<iframe class="youtube-player" type="text/html" width=" 200 " height=" 100 " src="http://www.youtube.com/embed/' . $vcat['Video']['link'] . '"></iframe>' ;?>
<div class="text"><?php echo '<b>'. $vcat['Video']['title'].'</b>' ; ?></div>
</div>
</td>
<?php
if($counter % 3 == 0){
echo '<br/>';
}
++$counter;
}
?>
</tr>
</table>