Jump to content

For each loop from mysql


pgrevents

Recommended Posts

I dont understand the problem as it is a loop. I have two sets of image information so thats two images. I have set up a for each loop so that I can display these images as a thumbnail

 

here is the loop statement

 

<?php foreach($gallery as  $image) : ?>                      
<li>

<a class="thumb" href="<?=$imageloc?>" title="Title #0">
<img src="<?=$imagethumb?>" alt="Title #0" /></a>

						<div id="clear"></div>
                                <div class="caption">

							<div class="image-title"><?=$imagename?></div>

							<div class="image-desc">see larger</div>
						    </div>
					</li>

<?php  endforeach ; ?>
?>

 

now it does display but as I said I have only two images to show but it returns 8 loops of the same first image.

 

Here is the code php mysql code that i include

 

<?php
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$dbname = 'dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$query = mysql_query("SELECT * FROM gallery");
$gallery = mysql_fetch_array($query);
$imageloc = $gallery['image_loc'];
$imagename = $gallery['image_name'];
$imagethumb = $gallery['thumnail_loc'];

?>

There is no errors outputting so I am at a loss can any of you help?

Link to comment
Share on other sites

Try this:

<?php foreach ($gallery as $image) { ?>                      
<li>

<a class="thumb" href="<?php echo $imageloc; ?>" title="Title #0">
<img src="<?php echo $imagethumb; ?>" alt="Title #0" /></a>

						<div id="clear"></div>
                                <div class="caption">

							<div class="image-title"><?php echo $imagename; ?></div>

							<div class="image-desc">see larger</div>
						    </div>
					</li>

<?php } ?>

 

Link to comment
Share on other sites

The following line of code -

 

$gallery = mysql_fetch_array($query);

 

Fetches ONE row from the result set. From the php manual -

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows

 

Looping over $gallery is just looping over all the columns in that row. You must use the mysql_fetch_array in a while() loop to fetch all the rows.

Link to comment
Share on other sites

What about using while

<?php  while ($gallery = mysql_fetch_assoc($query)) { ?>                      
<li>

<a class="thumb" href="<?php echo $imageloc; ?>" title="Title #0">
<img src="<?php echo $imagethumb; ?>" alt="Title #0" /></a>

						<div id="clear"></div>
                                <div class="caption">

							<div class="image-title"><?php echo $imagename; ?></div>

							<div class="image-desc">see larger</div>
						    </div>
					</li>

<?php } ?>

 

Link to comment
Share on other sites

$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$dbname = 'dbname';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$query = mysql_query("SELECT * FROM gallery");
while ($gallery = mysql_fetch_array($query)) {
$imageloc = $gallery['image_loc'];
$imagename = $gallery['image_name'];
$imagethumb = $gallery['thumnail_loc'];?>                      
<li>

<a class="thumb" href="<?=$imageloc?>" title="Title #0">
<img src="<?=$imagethumb?>" alt="Title #0" /></a>

						<div id="clear"></div>
                                <div class="caption">

							<div class="image-title"><?=$imagename?></div>

							<div class="image-desc">see larger</div>
						    </div>
					</li>
					<?php } ?>

Ted

Link to comment
Share on other sites

hi thanks,

 

there wasnt a problem with the vars it does display correctly but instead of the 2 test images there is only one image being shown in the look. Saying that I did try your code but unfortunately it has the same result as before only showing one image

 

thanks

Link to comment
Share on other sites

This works it displays the content BUT it will not show the last entry

here is the DB code

<?php
$internal = $_GET['more'];
$dbhost = 'localhost';
$dbuser = 'bates';
$dbpass = 'pass';
$dbname = 'bates';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$query = mysql_query("SELECT * FROM contact WHERE internal= '$internal'");
$more = mysql_fetch_array($query);
?>

 

here is my page display code

<?php 
while($row=mysql_fetch_array($query, MYSQL_ASSOC)){ ;
foreach ($row as $onbox)?>        
<div class="id_box"><?=$row['internal']?></div>
<div class="from_box"><?=$row['name']?></div>
<div class="from_email_box"><?=$row['email']?></div>
<div class="msg_status_box"><?=$row['status']?></div>
<div class="reply_status_box">
<a href="?more=<?=$row['internal']?>">more</a>
    </div>
<div class="updated_box">
<? if(empty($row['reply_time'])){
print "".$row['time']." on ".$row['date']."";
}
else{
print "";
}
?>

 

what could be the reason that it wont show the latest entry?

Link to comment
Share on other sites

Because you are fetching a row into $more before your while loop and then not using it -

$more = mysql_fetch_array($query);
while($row=mysql_fetch_array($query, MYSQL_ASSOC)){ ;

 

Why do you have this - $more = mysql_fetch_array($query); in your code at all?

Link to comment
Share on other sites

http://www.barrybatemanphotography.co.uk/?id=155 now Im not the php programmer for the group im just a designer lol he is on holiday and this project has to be handed in like tommorow.

 

as you will see from the above it displays 2 images but they are the same there are two images in the database and they are different but its looping with the while statement at the top of the page

Link to comment
Share on other sites

$images = mysql_query("SELECT stuff FROM gallery");

while ($image = mysql_fetch_array($images))
{
  echo $image['column1']; // column 1 in the database
  echo $image['column2']; // column 2 in the database
  //... and so on
}

Link to comment
Share on other sites

Working from your display code, you have a semi-colon after the while and you do not need the foreach:

 

<?php 
while($row=mysql_fetch_array($query, MYSQL_ASSOC)) {
?>
<div class="id_box"><?=$row['internal']?></div>
<div class="from_box"><?=$row['name']?></div>
<div class="from_email_box"><?=$row['email']?></div>
<div class="msg_status_box"><?=$row['status']?></div>
<div class="reply_status_box">
<a href="?more=<?=$row['internal']?>">more</a>
</div>
<div class="updated_box">
<? if(empty($row['reply_time'])){
        print "".$row['time']." on ".$row['date']."";
    }
    else{
        print "";
    }
}
?>

 

Also, before the while loop, you can echo out the number of rows to make sure there are two rows returned:

 

echo mysql_num_rows($query);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.