Pagination php from array - php

I've been trying to pagginate my picture but very very unssucesfull i am hopping i can ghet some hints on how to do it. This is my code.
<?php
$result = mysql_query("SELECT * FROM images ORDER BY position");
while ($image = mysql_fetch_array($result)){
?>
<img src="images/uploads/thumbs/<?php echo $image['thumb'];?>" width="100%" height="100%" alt="" class="thumbnail" />
<input type='button' value='Preview' onclick="changedesc(<?php echo $image['image_id']; ?>); return false;" />
<?php }?>

This shoul'd do it..
<?php
$query = "SELECT * FROM images ORDER BY date";
$result = mysql_query($query);
while ($image = mysql_fetch_assoc($result)){
echo '
<img src="images/uploads/thumbs/'.$image['thumb'].'" width="100%" height="100%" alt="" class="thumbnail" />
';
}
?>

Related

PHP, CSS class issue

I've been dealing with this problem like 3 days.
I have a PHP code that echo es images from database. The class IMG is not responding, it should show margin-bottom: 60px but it is not. I've putted the class in img class-name src but still nothing.
Thank you
<table class="table-image">
<?php
$query = "SELECT * FROM images ORDER BY id DESC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td>
<img src="data:image/jpeg;base64,'.base64_encode($row['name'] ).'" height="480" width="550" class="img" " />
</td>
</tr>
';
}
?>
</table>
And CSS
.img{
width: 100%;
height: auto;
margin-bottom: 60px;
}
You have two quotes at the end.
class="img" " />
<table class="table-image">
<?php
$query = "SELECT * FROM images ORDER BY id DESC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)): ?>
<tr>
<td>
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['name'] ); ?>" height="480" width="550" class="img" />
</td>
</tr>
<?php endwhile; ?>
</table>

Showing data from category with php mysql

My tables are:
barangtbl: id, judul_barang, judul_seo, keywords, deskripsi, id_kat, id_sub, id_supersub, kategori_seo, view, gambar
kategori: id_kat, nama_kat
subkategori: id_sub, id_kat, nama_sub
supersubkategori: id_supersub, id_sub, id_kat, nama_supersub
I have a problem with showing data in category from database with PHP, the problem is when i click link: localhost/test/category.php?name=HPI, it doesn't show any data, but if I change HPI with number: 15, it show all.
15 is id_supersub data on supersubkategori table where I join with barangtbl table. So, all i want is if someone click: localhost/test/category.php?name=HPI it will show data with HPI category inside. How solve this problem?
<?php
if (isset($_GET['name']))
{
$kategori = $_GET['name'];
}
include "config.php";
if ((isset($kategori)) =='')
{
$query = "SELECT * FROM barangtbl INNER JOIN supersubkategori on supersubkategori.id_supersub = barangtbl.id_supersub ORDER BY id DESC LIMIT 0,12";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
else
{
echo "
<table width=\"100%\">
<tr>
<td align=\"center\"><b><font color=\"red\" size=\"2.5\">[ ".$_GET['name']." ]</b></font></td>
</tr>
</table>";
$query = "SELECT * FROM barangtbl WHERE id_supersub = '$kategori' ORDER BY id";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
?>
<table cellpadding="10" cellspacing="2" align="center">
<tr>
<?php
$kolom=3;
$x = 0;
if($numrows > 0)
{
while($data=mysql_fetch_array($hasil))
{
if ($x >= $kolom)
{
echo "</tr><tr>";
$x = 0;
}
$x++;
?>
<th>
<div id="title">
<a href="product.php?id=<?php echo $data['id']; ?>">
<?php echo $data['judul_barang']; ?>
</a>
<br><br>
</div>
<div id="image">
<a href="product.php?id=<?php echo $data['id']; ?>">
<img width='150' height='150' valign='top' border='1,5' src="product/<?php echo $data['gambar']; ?>" />
</a>
<br><br>
</div>
<div id="action">
<?php
echo '
<a href="product.php?id='.$data['id'].'">
<img src="images/detail.jpg"\ title="Detail Barang" border="0" width=\"50\" height=\"30\">
</a>';
?>
</div>
<hr />
</th>
<?php
}
}
?>
</tr>
</table>
Try removing the quotes
$query = "SELECT * FROM barangtbl WHERE id_supersub = $kategori ORDER BY id";

Displaying random product in a related product table of 5

I am creating a row of "product related table of x5" and using MYSQLcode RAND to generate RANDOM products and a exception clause using id!='$id' - where id is existing product that is being viewed currently.
However the problem is that all the product images generated were all of the same similar product image.
Can anyone shed some light on this?
Pic for better reference:
include "MyOnlineStore/storescripts/connect_to_mysql.php";
$sql2 = mysql_query("SELECT * FROM bag WHERE id!='$id'ORDER BY RAND()LIMIT 5");
$productCount2 = mysql_num_rows($sql2); // count the output amount
if ($productCount2 > 0) {
?>
<table border="1">
<tr>
<?php
while ($row2 = mysql_fetch_array($sql2)) {
$idrelated = $row2["id"];
$imagerelated = $row2["image"];
$titlerelated = $row2["title"];
}
} else {
echo "That item does not exist.";
exit();
}
mysql_close();
HTML
<table border="1">
<tr>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
</tr>
</table>
Your code should be something like this
include "MyOnlineStore/storescripts/connect_to_mysql.php";
$sql2 = mysql_query("SELECT * FROM bag WHERE id!='$id'ORDER BY RAND()LIMIT 5");
$productCount2 = mysql_num_rows($sql2); // count the output amount
if ($productCount2 > 0) {
?>
<table border="1">
<tr>
<?php
while ($row2 = mysql_fetch_array($sql2)) {
$idrelated = $row2["id"];
$imagerelated = $row2["image"];
$titlerelated = $row2["title"];
?>
<td>
<a href="http://example.net/product.php?id=<?php echo $idrelated; ?>">
<img src="admin/product/uploaded_files/<?php echo $imagerelated; ?>" width="100" height="100" alt="<?php echo $titlerelated; ?>" />
</a>
</td>
<?php
}
?>
</tr>
</table>
As far as I can see from your code, the following variables:
$idrelated, $imagerelated, $titlerelated
remains unchanged after leaving the loop. So in each line you refer to the same variable and value.
If you want to keep use some array to keep all five values:
<?php
$index = 0;
while (...) {
$relatedA[$index] = ...;
$relatedB[$index] = ...;
$relatedC[$index] = ...;
$index++;
}
?>
...
<?php echo $relatedA[0]; ?>
<?php echo $relatedA[1]; ?>
...
<?php echo $relatedA[4]; ?>
The last you can put in the loop again to avoid redundant code.
UPDATE
Try to resist the temptation of putting echoes into the first loop where you fetch values - as it could result in hard to maintenance spaghetti-style code (unfortunately quite commonly seen in many php sources).

How to Display every 3 results in a row using mysql?

By using the below code i displayed 9 images, but all images are align side by side, how to display the every 3 images in one row and another3 images in another row ?
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<?php $sel = $db->query("select * from gallery order by gallery_cat_id asc limit 1,1");
while($row=mysql_fetch_array($sel)){ ?>
<tr><td align="left" valign="top"><table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody><?php $sel1 = $db->query("SELECT DISTINCT ( i.gallery_album_id )
FROM mov_gallery_album AS a, mov_gallery_images AS i
WHERE a.gallery_album_id = i.gallery_album_id
AND a.gallery_cat_id =".$row['gallery_cat_id']."
ORDER BY a.gallery_album_id DESC
LIMIT 0 , 9 "); if(mysql_num_rows($sel1)>0){ ?>
</tr><tr>
<?php
while($row1=mysql_fetch_array($sel1)){
$dis1 = $db->getRow("select * from ".ALBUMS." where gallery_album_id=".$row1['gallery_album_id']." limit 0,1");
$dis2 = $db->getRow("select * from ".GALLERY." where gallery_id=".$dis1['gallery_id']." limit 0,1");
$dis3 = $db->getRow("select * from ".ALBUMSIMAGES." where gallery_album_id=".$row1['gallery_album_id']." limit 0,1");
$dis4 = $db->getRow("select * from ".GALLERYCATEGORY." where gallery_cat_id=".$dis2['gallery_cat_id']." limit 0,1");
?>
<td align="left" width="100%"><table border="0" cellpadding="5" cellspacing="0">
<tbody><tr><td align="center" height="8" valign="middle" width="80">
<div style="border:0px;clear:both;padding-bottom:100px;margin-left:-110px;">
<div class="image_stack1"><a href="<?php echo SITE; ?>album/<?php echo ucfirst($dis1['gallery_cat_id']); ?>/<?php echo ucfirst($dis1['gallery_id']); ?>/<?php echo ucfirst($row1['gallery_album_id']); ?>/">
<img id="photo3" src="<?php echo SITE; ?>uploads/gallery/<?php echo $dis4['folder']; ? >/<?php echo $dis2['folder']; ?>/<?php echo $dis1['folder']; ?>/thumb/<?php echo $dis3['image']; ?>"width="80" height="80">
<div class="namehover1"><?php echo substr(ucfirst($dis1['name']),0,13); ?></div>
</a></div></div></td></tr></tbody></table></td><?php } ?></tr><tr><td class="midtitle" align="center" valign="middle"> </td></tr>
<tr><td style="padding-right: 10px;" align="right" colspan="4"><a href="<?php echo SITE; ?>gallery/<?php echo ucfirst($row['gallery_cat_id']); ?>/" class="midtitle">
<img src="http://www.img./viewall.png" border="0"/>
</a></td></tr><?php } ?></tbody></table></td></tr><?php } ?></tbody></table>
It would be wise to use div rather than table.
Some lines of css would do the trick. You wont have to logically determined to move to next line after 3 images. Here's how you can do with this in css
Lets have a div container for the all the images.
<style>
#photo_wrapper {
width:600px;
}
.photo {
width:150px;
height:150px;
display:block;
float:left;
border:6px #c5d0d6 solid;
margin-right:5px;
margin-bottom:5px;
overflow:hidden;
}
</style>
<div id="photo_wrapper">
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
<img src="image.jpg" class="photo"/>
</div>
this is generally done with the modulo (remainder after division) operator:
foreach ($results as $nr => $row) {
if (($nr % 3) == 0) echo '<tr>';
...
if (($nr % 3) == 0) echo '</tr>';
}
The concept is simple, use a counter and each time this counter MOD 3 is zero , start a new line.Or just count until 3 and reset the counter.
$i = 1;
while( $data = ....)
{
if($i == 1)
{
echo "<tr>"; //new line
}
echo "<td> .... </td>";
if($i == 3)
{
$i = 1;
echo "</tr>";
}
else
{
$i++;
}
}
//IMPORTANT! PLEASE NOTICE TO THIS PART: (OUT OF THE LOOP)
if($i < 3)
echo "</tr>";
you can put 9 photos in a div, then fix the width of that div to make it enough for only 3 photos in a line. And set float:left; for img tag in CSS.

php code not working inside html

I am trying to display an image. I have fetched my URL from the db storage. And i have used the php variable inside the image tag. But the code does'nt display any image .
what is the problem? exactly!
this is my code below
<?php $db =& JFactory::getDBO();
$query88=$sql = "SELECT file_url_thumb FROM fs01_virtuemart_medias WHERE virtuemart_media_id=1 LIMIT 0, 30 ";
$result88 = mysql_query($query88) or die(mysql_error());
?><img src="<?php while($row = mysql_fetch_array($result88)){
echo $row['file_url_thumb'];
echo "<br />";
} ?>" border="0" style="border: 0; vertical-align: top;" />
You are looping over your results and putting them all (each followed by a <br /> inside the src attribute of an img tag. It seems highly unlikely that that won't 404.
You probably want something more like:
<ul>
<?php while($row = mysql_fetch_array($result88)){ ?>
<li><img src="<?php echo htmlspecialchars($row['file_url_thumb']); ?>" /></li>
<?php } ?>
</ul>
(With some CSS from an external stylesheet to apply your presentation).
<?php
$db = &JFactory::getDBO();
$query88 = "SELECT file_url_thumb FROM fs01_virtuemart_medias WHERE virtuemart_media_id=1 LIMIT 0, 30 ";
$result88 = mysql_query( $query88 ) or die( mysql_error() );
while( $row = mysql_fetch_array( $result88 ) ) {
echo '<img src="' . $row[ 'file_url_thumb' ] . '" border="0" style="border: 0; vertical-align: top;" /><br />';
}
?>
use this
<?php
$db = &JFactory::getDBO();
$query88 = "SELECT file_url_thumb FROM fs01_virtuemart_medias WHERE virtuemart_media_id=1 LIMIT 0, 30 ";
$result88 = mysql_query( $query88 ) or die( mysql_error() );
while($row = mysql_fetch_array($result88)){
echo '<img src="'.$row['file_url_thumb'].'" style=" border="0" style="border: 0; vertical-align: top;"/>';
echo '</br>';
}
?>

Categories