edawg Posted January 28, 2007 Share Posted January 28, 2007 Hi, I'm trying to update a current pictures site from html into php with a mysql backend, im pretty new to php and mysql and have run into a problem I cant seem to remedy??, any help would be great!.I have set up the database, and in the database I have 2 tables so far, one is for the pictures categories which has the names of the categories, and the urls, and the other is for the pictures which has the title of the pictures, and filenames and description, so I linked them both with a associated primary/foreign key relationship.Now my problem is with a section on my site I would like the latest pictures posted to be shown, so I used a href so you can click on anyone and it will take you to that pictures category, the only problem is I get the latest pictures showing up fine, but when I put the mouse on them the url is the same all the way down, rather than change for each individual picture category, it stays the same link?, ive tried as much as I know so far, but still the same problems?, what can I do?.CODE #######################mysql_select_db($database, $connDB) OR DIE ('Unable To Select That Database' . mysql_error() );$query_sPictures = "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC"; $sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());$row_sPictures = mysql_fetch_assoc($sPictures);$totalRows_sPictures = mysql_num_rows($sPictures); [color=red]<-- This part works fine[/color]?> <?php$query_sCategories = "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id"; $sCategories = @mysql_query($query_sCategories, $connDB) or die(mysql_error());$row_sCategories = mysql_fetch_assoc($sCategories);$totalRows_sCategories = mysql_num_rows($sCategories);?><?php do { ?><a href="<?php echo $row_sCategories['sc_url'];?>"> [color=red]<-- This is where the problem is??.[/color]<img src="pictures/<?php echo $row_sPictures['sp_filename_TN']; ?>" alt="<?php echo $row_sPictures['sp_ALT'];?>"></img></a><?php } while ($row_sPictures = mysql_fetch_assoc($sPictures));mysql_close();?>Any help would be great, im pulling my hair out with this!!!!!!. >:( Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/ Share on other sites More sharing options...
ikmyer Posted January 28, 2007 Share Posted January 28, 2007 what is the [/url] in there for... im guess this should be a </a> Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-170928 Share on other sites More sharing options...
chronister Posted January 28, 2007 Share Posted January 28, 2007 When you post here, wrap your code in code tags using the # button above the smileysYou are using a do..while loop, and I have always seen a while...do loop used here. Try the code I have below. I replaced your do with the while portion you had at the end[code]<?php mysql_select_db($database, $connDB) OR DIE ('Unable To Select That Database' . mysql_error() );$query_sPictures = "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC"; $sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());$row_sPictures = mysql_fetch_assoc($sPictures);$totalRows_sPictures = mysql_num_rows($sPictures); //<-- This part works fine?><?php$query_sCategories = "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id"; $sCategories = @mysql_query($query_sCategories, $connDB) or die(mysql_error());$row_sCategories = mysql_fetch_assoc($sCategories);$totalRows_sCategories = mysql_num_rows($sCategories);?><?php while ($row_sPictures = mysql_fetch_assoc($sPictures){ ?><a href="<?php echo $row_sCategories['sc_url'];?>"> <img src="pictures/<?php echo $row_sPictures['sp_filename_TN']; ?>" alt="<?php echo $row_sPictures['sp_ALT'];?>"></img></a><?php } mysql_close();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-170937 Share on other sites More sharing options...
edawg Posted January 28, 2007 Author Share Posted January 28, 2007 Thanks for your post, but still no luck unfortunatly??. the link still stays the same for all of the pictures regardless of what category there in??. what else can I try??. Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-171404 Share on other sites More sharing options...
trq Posted January 28, 2007 Share Posted January 28, 2007 Your only iterating through the $sPictures result, you'll need to nest another loop within the original to loop through the categories. Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-171415 Share on other sites More sharing options...
.josh Posted January 28, 2007 Share Posted January 28, 2007 @chronister: the do..while loop is technically fine. The difference between do..while and while... is that the stuff inside the do...while loop will always be executed at least once: the first time. With the while... loop, it will only execute (even the first time) if the condition is met. @edawg: when you execute a query, it returns a result source. You must then use a function to grab the info from the result source (mysql_fetch_assoc is one such function). mysql_fetch_assoc grabs the info one row at a time, so you that's why you loop the actual function call. example:[code]$sql = "select column1 from table";$result = mysql_query($result);while ($list = mysql_fetch_assoc($result)) { echo $list['column1'] . "<br/>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-171416 Share on other sites More sharing options...
edawg Posted January 28, 2007 Author Share Posted January 28, 2007 This is the problem page -->> [url=http://www.cellphonepictoria.com/test/index.php]http://www.cellphonepictoria.com/test/index.php[/url]The problem is with the right hand bar "Latest Pictures", as you hoover over them, I want the pictures to point to there own category url, rather than have the same link as the first picture in the list.And I tried what you said about the nesting, but was not too sure how to do this, so i tried this with the code?, is this what you meant?[code]<?phpmysql_select_db($database, $connDB) OR DIE ('Unable To Select That Database' . mysql_error() );$query_sPictures = "SELECT site_pictures.sp_filename_TN, site_pictures.sp_ALT FROM site_pictures ORDER BY site_pictures.sp_timestamp ASC";// Print $query_sCategories;$sPictures = @mysql_query($query_sPictures, $connDB) or die(mysql_error());$row_sPictures = mysql_fetch_assoc($sPictures);$totalRows_sPictures = mysql_num_rows($sPictures);?><?php$query_sCategories = "SELECT site_categories.sc_url FROM site_categories, site_pictures WHERE site_pictures.sc_id = site_categories.sc_id";// Print $query_sCategories;$sCategories = @mysql_query($query_sCategories, $connDB) or die(mysql_error());$row_sCategories = mysql_fetch_assoc($sCategories);$totalRows_sCategories = mysql_num_rows($sCategories);?><?php do { ?><?php do { ?><a href="<?php echo $row_sCategories['sc_url'];?>"><?php } while ($row_sCategires = mysql_fetch_assoc($sCategories)); ?><img src="pictures/<?php echo $row_sPictures['sp_filename_TN']; ?>" alt="<?php echo $row_sPictures['sp_ALT'];?>"></img></a><?php } while ($row_sPictures = mysql_fetch_assoc($sPictures));mysql_close(); ?>[/code]I really appreciate all your help too!!!!. Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-171420 Share on other sites More sharing options...
edawg Posted January 29, 2007 Author Share Posted January 29, 2007 Any more ideas or advice would be great, if i cant figure this one out im gonna { do (hit my head against a brick wall) } while { (crying out loud until its fixed) } ;D thanks everyone!. Quote Link to comment https://forums.phpfreaks.com/topic/36019-do-while-loops-problem/#findComment-171857 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.