Jump to content

Displaying enum MySQL data on a profile, HELP NEEDED?


jdowen2211

Recommended Posts

I have a social network with user profiles and when a user signs up, they enter whether they are a student, parent or teacher. That is then sent to a MySQL database and is collected successfully. I want that data to display on their profile but in the database it is stored like this: s for student, t for teacher and p for parent. I managed to get it on the profile saying something like this, "username is a s". I want that s to say student and if they are a teacher then I want teacher and so on. Could someone give me some code that will help because everything I have tried so far has failed. Help will be much appreciated.

<?

$i <-  user status row

switch ($i) {

    case "s":

        echo "student";

        break;

    case "t":

        echo "teacher";

        break;

    case "p":

        echo "parents";

        break;

}

?>

Try gogle it ' php switch'...

I tried this before and it just said James is a s but I want the s to be Student:

 

if ($person_type == "") {

    $person_type = "";

        } else {

        $person_type = '<br /><br /><strong>' . $username . ' is a ' . $person_type . '';

        }

It's not working, it's just throwing errors at me. Do I need to put it in the part of the profile where I want it to display or do I put it with the other PHP code at the top of the file that renders the page then echo it out with <?php echo $person_type; ?>

Well this is the code I'm using and I'm trying to echo it out with <?php echo $person_type; ?> but instead at the top of the profile where errors display it just says 'student' which is a start and when I want it to display in the profile is says 's' still. Once I get it to where I want I'm going to add ' . $username . " is a student/teacher/parent. For now I'm going to put the code below onto the profile where I want it to display.

 

if ($person_type == "") {

    $person_type = "";

} switch ($person_type) {

    case "s":

        echo "student";

        break;

    case "t":

        echo "teacher";

        break;

    case "p":

        echo "parents";

        break;

        }

I just went to the profile, which in my case is profile.php, and added the following code where I want it to display on the page:

 

<?php if ($person_type == "") {

    $person_type = "";

} switch ($person_type) {

    case "s":

        echo "$username is a Student";

        break;

    case "t":

        echo "$username is a Teacher";

        break;

    case "p":

        echo "$username is a Parent";

        break;

        }

?>

Here's a way to do it with less code, if you're interested.

 

$types = array('s' => 'Student', 't' => 'Teacher', 'p' => 'Parent');

if( !empty($person_type) ) {
     echo "$username is a {$types[$person_type]}.";
} else {
     echo "$username has not had a person type assigned." // you can drop the else{} clause if not needed . . .
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.