PHPnewby! Posted March 14, 2007 Share Posted March 14, 2007 Hi guys, hoping you can help me out here! I am trying to select from a database table "Wishlist" and print each destination added for the logged in user, incorporating a while and case statement to link a destination to information that I have on that destination within another web page...if that makes sense!?? At the moment....the code is printing the correct number of rows and displaying the link correctly, but say I have 5 different destinations within the table, it is printing 5 of the destination names which match the first database row...instead of 5 different destinations. Code is below...any ideas anyone??? Much appreciated!! Claire. <?php require_once('Connections/TCO.php'); $get_memberdetails_sql = "SELECT * FROM Wishlists WHERE Username = '{$User}'"; if ($result = mysql_query($get_memberdetails_sql)) { if (mysql_num_rows($result)) { print "The following destinations have previously been saved to your travel wishlist:"; print "<p>"; while($thisrow=mysql_fetch_row($result)) { $get_memberdetails_res = mysql_fetch_array(mysql_query($get_memberdetails_sql)); $Destination = $get_memberdetails_res['Destination']; switch($Destination) { case "South Africa": echo "<a href=\"africa.php\">South Africa</a><br>"; break; case "Greece": echo "<a href=\"europe.php\">Greece</a><br>"; break; case "Amsterdam": echo "<a href=\"europe.php\">Amsterdam</a><br>"; break; case "India": echo "<a href=\"asia.php\">India</a><br>"; break; default: echo $Destination. "<p>"; break; } } } else { echo "You have never added any destinations to your wish list. To add some destinations, please add them through the <a href=\"holidaysearch.php\">Holiday Search </a>feature.</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/ Share on other sites More sharing options...
papaface Posted March 14, 2007 Share Posted March 14, 2007 Use [ code] [ /code] tags when you post your code please. Quote Link to comment https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/#findComment-207244 Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 your code doesn't make any sense... and there is a lot of unnecessary code. it's difficult to understand what you're trying to do between your description and your code... try replacing your code with this: <?php require_once('Connections/TCO.php'); $get_memberdetails_sql = "SELECT * FROM Wishlists WHERE Username = '{$User}'"; $result = mysql_query($get_memberdetails_sql); if (mysql_num_rows($result) > 0) { echo "The following destinations have previously been saved to your travel wishlist:"; while($thisrow = mysql_fetch_row($result)){ switch($thisrow['Destination']){ case "South Africa": echo "<p><a href=\"africa.php\">South Africa</a></p>\n"; break; case "Greece": echo "<p><a href=\"europe.php\">Greece</a></p>\n"; break; case "Amsterdam": echo "<p><a href=\"europe.php\">Amsterdam</a></p>\n"; break; case "India": echo "<p><a href=\"asia.php\">India</a></p>\n"; break; default: echo "<p>You have never added any destinations to your wish list. To add some destinations, please add them through the <a href=\"holidaysearch.php\">Holiday Search</a> feature.</p>"; break; } } } ?> fyi, it is bad coding practice to switch off between echo and print. keep your code consistent and well organized. Quote Link to comment https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/#findComment-207259 Share on other sites More sharing options...
PHPnewby! Posted March 14, 2007 Author Share Posted March 14, 2007 Hi guys, thanks for the tips! I have just tried the above code and it just prints "You have never added any destinations to your wish list. To add some destinations, please add them through the Holiday Search feature." as many times as there are rows in the database. Any more ideas? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/#findComment-207283 Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 <?php require_once('Connections/TCO.php'); $get_memberdetails_sql = "SELECT * FROM Wishlists WHERE Username = '{$User}'"; $result = mysql_query($get_memberdetails_sql); if (mysql_num_rows($result) > 0) { echo "The following destinations have previously been saved to your travel wishlist:"; while($thisrow = mysql_fetch_row($result)){ switch(strtolower($thisrow['Destination'])){ case "south africa": echo "<p><a href=\"africa.php\">South Africa</a></p>\n"; break; case "greece": echo "<p><a href=\"europe.php\">Greece</a></p>\n"; break; case "amsterdam": echo "<p><a href=\"europe.php\">Amsterdam</a></p>\n"; break; case "india": echo "<p><a href=\"asia.php\">India</a></p>\n"; break; default: echo "<p>You have never added any destinations to your wish list. To add some destinations, please add them through the <a href=\"holidaysearch.php\">Holiday Search</a> feature.</p>"; break; } } } ?> Also from the very first post, you may want to learn some html buddy. Made the case switch to lowercase incase there is a difference in the database. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/#findComment-207293 Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 @frost - good idea. Quote Link to comment https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/#findComment-207305 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.