Jump to content

Recommended Posts

What I am trying to do is I have a carousel on the profile section of the site, i want it to check the gallery table and see if the user has any uploaded images yet, if the have run the while loop to retrieve them and add the images to the carousel, if they havn't display the default image saying coming soon. The issue I am having is I can either get it to display the default image all the time or else not display anything so im guessing the issue is with the count statement. Can anyone help please?

 

		<br />
		<div class="content_box">
			<div class="content_box_h">Latest Artwork</div>
			<div class="content_box_carousel">
				<div id="slider">
					<ul>
					<?php

					include("Config.php");

					$User_id=$_GET['id'];
										             
					// Retrieve data from database 
					$sql = "SELECT count(User_id) as galcount FROM biggartfp9_gallery_itemst WHERE User_id='$User_id' ";
					$result = mysql_query($sql);

					$row = mysql_fetch_assoc($result);

					if($row['galcount'] != 1) {

						$User_id=$_GET['id'];

						// Retrieve data from database 
						$sql2 = "SELECT * FROM biggartfp9_gallery_itemst WHERE User_id='$User_id' ORDER BY Date_added DESC LIMIT 10";
						$result2=mysql_query($sql);

						// Start looping rows in mysql database.
							while($rows=mysql_fetch_array($result2)){
							?>
							<li>
								<div id="carousel_images">
								<a href="#"><img src="<?php echo $rows['Picture']; ?>" alt="<?php echo $rows['Title']; ?>"/></a>
								</div>
							</li>	
							<?php
							// close while loop 
							}
					}						   
					else {
					echo "
					<li>
						<div id='carousel_images'>
						<a href='#'><img src='../Gallery_Images/Default.jpg' alt='Artwork Coming Soon'/></a>
						</div>
					</li>	
					";
					}					   
					// close connection 
					mysql_close();
					?>
					</ul>
				</div>		
			</div>
		</div>

 

Link to comment
https://forums.phpfreaks.com/topic/203126-php-count-and-while-loop-issues/
Share on other sites

No need to do two queries. Just do one query to get the records and check the number of records returned. Also, you are giving multiple divs the exact same ID, you can't do that.

 

Here is a more logical approach

<?php

include("Config.php");

function createImageHTML($title, $src=false, $idx=false)
{
    $divID = ($idx!==false) ? "carousel_images_{$idx}" : 'carousel_images';
    if(!$src) { $src = '../Gallery_Images/Default.jpg'; }

    $html = "<li>\n";
    $html .= "  <div id=\"{$divID}\">\n";
    $html .= "    <a href=\"#\"><img src=\"{$src}\" alt=\"{$title}\" /></a>\n";
    $html .= "  </div>\n";
    $html .= "</li>\n";

    return $html;
}

$User_id = mysql_real_escape_string(trim($_GET['id']));
$query = "SELECT *
          FROM biggartfp9_gallery_itemst
          WHERE User_id='$User_id'
          ORDER BY Date_added DESC
          LIMIT 10";
$result = mysql_query($query);

$images = '';
if(mysql_num_rows($result)<1)
{
    //There were no results, show 'coming soon image'
    $images = createImageHTML("Artwork Coming Soon");
}
else
{
    //There were results, create image links
    $idx = 0;
    while($row = mysql_fetch_assoc($result))
    {
        $images .= createImageHTML($rows['Title'], $rows['Picture'], $idx);
        $idx++;
    }
}

mysql_close();
?>
            <br />
         <div class="content_box">
            <div class="content_box_h">Latest Artwork</div>
            <div class="content_box_carousel">
               <div id="slider">
                  <ul>
                        <?php echo $images; ?>
                  </ul>
               </div>      
            </div>
         </div>

Ok thank you this makes a lot more sense, It is working now and reconsing how many images should be displayed but its just showing the coming soon image and not the actual images that have been uploaded, so for example if that user has three entries it will display 3 coming soon images in the carousel. Any ideas?

I see the problem - it was a typo. I used $row when getting the record data, but used $rows when processing it.

 

Change the while loop to this:

    while($row = mysql_fetch_assoc($result))
    {
        $images .= createImageHTML($row['Title'], $row['Picture'], $idx);
        $idx++;
    }

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.