andrew_biggart Posted May 27, 2010 Share Posted May 27, 2010 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> Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 27, 2010 Author Share Posted May 27, 2010 Ive tried changing the if statement to != 0 and also <1 but its still not working.. Any suggestions? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2010 Share Posted May 27, 2010 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> Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 27, 2010 Author Share Posted May 27, 2010 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2010 Share Posted May 27, 2010 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++; } Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted May 27, 2010 Author Share Posted May 27, 2010 Legend, thank you it is working perfectly now! Appreciate your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.