I have a question: I have used the following queries to get my arrays from Database:
$news = $db->QueryFetchArrayAll("SELECT login,id FROM `users` ORDER BY id DESC LIMIT 9");
$imgs = $db->QueryFetchArrayAll("SELECT usrpic,id FROM `users` ORDER BY id DESC LIMIT 9");
Now i need $news['login'] and $imgs['usrpic'] in one for each loop.
For example:
foreach($news as $new, $imgs as $img){
<img style="border: 1px solid #8C0E0E;"src="'.$img['usrpic'].'" width="20" height="20" border="0" />
<img style="border: 1px solid #8C0E0E;"src="'.$new['login'].'" width="20" height="20" border="0" />
}
Can anyone help me how i can do this ?
You can use next() function : http://php.net/manual/en/function.next.php
but don't forget to make sure news and img have same size :
if(count($news) == count($imgs)) {
foreach($news as $new) {
$img = next($imgs);
<img style="border: 1px solid #8C0E0E;"src="'.$img['usrpic'].'" width="20" height="20" border="0" />
<img style="border: 1px solid #8C0E0E;"src="'.$new['login'].'" width="20" height="20" border="0" />
}
}
If those are your actual queries, you should combine them into one:
$users = $db->QueryFetchArrayAll("SELECT id, login, usrpic FROM `users` ORDER BY id DESC LIMIT 9");
Then iterate the result array and output the images:
foreach ($users as $user) {
echo "<img src='{$user['usrpic']}' ... />";
echo "<img src='{$user['login']}' ... />";
}
Otherwise (keeping your original queries), you should make sure $news and $imgs have the same length, and then iterate one of them:
$size = count($imgs);
for ($i = 0; $i < $size; $i++) {
$img = $imgs[$i];
$new = $news[$i];
echo "<img src='{$img['usrpic']}' ... />";
echo "<img src='{$new['login']}' ... />";
}
Related
Hi I've this simple php script for showing images from a folder.My problem is that images are displayed very closely .I want to add some space between images and border for each image.Pls help me.
$files = glob("images/gallery/thumb/*.*");
for ($i=1; $i<count($files); $i++)
{
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" />';
}
Or, if you want a sort of grid:
$files = glob("images/gallery/thumb/*.*");
$count = count($files);
for($i = 1; $i < $count; $i++){
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" style="border:2px solid black; margin: 5px; float: left;" />';
}
Use plain HTML line break <br/> for the space between two images.
Use CSS style="border:2px solid black;" (change it to suit your need) for the border.
And I’ll suggest you to store count($files) in a variable; otherwise it’ll have to be evaluated in every iteration of your for loop, causing your script to run slower.
Now your entire code will be something like the following:
$files = glob("images/gallery/thumb/*.*");
$count = count($files);
for($i = 1; $i < $count; $i++){
$image = $files[$i];
echo '<img src="'.$image .'" alt="Random image" style="border:2px solid black;"/><br/>';
}
I would attach a class to to the images.
echo '<link rel="stylesheet" href="gallery.css" type="text/css"';
...
$files = glob("images/gallery/thumb/*.*");
foreach ($files as $image){
echo '<img src="'.$image .'" alt="Random image" class="galleryImage"/>';
}
and then create a .css file with the style for the class. This will allow you to easily change the style via .css without having to touch your php code, and will make your rendered html file slightly smaller.
gallery.css
img.galleryImage{
border: 2px solid #888888;
padding: 10px;
}
hi it just wount return all the pic I had this problem before and I don't realy know what keeps causing it whenever i put my code in class files
<?php
class get_pic{
function thumb($id){
$piclist = '';
if (isset($_GET['id'])) {
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM productpic WHERE id='$id' ");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$path1 = $row["path1"];
$piclist ='<li id="thumb"><img id="thumb" style=" position:relative; border:#666 1px solid; " src="'.$path1.'" alt="' . $id . '" width="120" height="160" border="1" /></li>';
}
}
}
return $piclist;
}
}
?>
Your $piclist has only the last value. You should concatenate strings.
Make the following changes:
...
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)) {
$piclist .= '<li>...';
Changing this:
$piclist = '';
...
$piclist ='<li id="thumb"><img id="thumb" style=" position:relative; border:#666 1px solid; " src="'.$path1.'" alt="' . $id . '" width="120" height="160" border="1" /></li>';
...to this might fix it:
$piclist = array();
...
$piclist[] ='<li id="thumb"><img id="thumb" style=" position:relative; border:#666 1px solid; " src="'.$path1.'" alt="' . $id . '" width="120" height="160" border="1" /></li>';
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.
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>';
}
?>
Why am I getting an undefined under the large photo when I click on a thumbnail here and how do I fix this? I have tried commenting out everything here to no effect? Here is the code and a URL for review:
http://tinyurl.com/6pbkhg7
<td><div id="loadarea" style="width: 400px; height: 300px;">
<img width="400" height="300" src="http://www.mydomain.com/feeds/fmfl/rets_images/<?php echo(rawurlencode($row['ListingRid'])); ?>_1.jpg" alt="<?php echo(rawurlencode($row['MLNumber'])); ?>" align="left" border="0">
</div>
<?
$image = "<br>";
$ListingRid = $row['ListingRid'];
$img_cnt = 1;
//$image .= "<img src=/feeds/fmfl/rets_images/{$ListingRid}_1.jpg alt='' width='100' height='75' border='0' /> ";
for ($c=1;$c<10;$c++) {
if ($c<10)
$c_ext = $c;
else
$c_ext = $c;
if (file_get_contents("http://www.mydomain.com/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg"))
$image .= "<img src=/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg alt='' width='100' height='75' border='0' /> ";
else
$c=12;
$img_cnt++;
if ($img_cnt == 5) {
$image .= "<br>";
$img_cnt = 0;
}
}
?>
Because you are missing the title tag
try this with title:
$image .= "<img src=/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg alt='' width='100' height='75' border='0' /> ";
or this without title:
$image .= "<img src=/feeds/fmfl/rets_images/{$ListingRid}_{$c_ext}.jpg alt='' width='100' height='75' border='0' /> ";
lightbox requires a title, you should set title="" if you don't want anything there.