Jump to content

Recommended Posts

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>";

  }

}

?>

Link to comment
https://forums.phpfreaks.com/topic/42717-case-statementwhile-loop-problem/
Share on other sites

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.

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!

<?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

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.