0

I am fairly new to PHP and Codeigniter so im sure this will be simple.

I have contact details stored in this format where type is either (1,2,3,4) mapping to (facebook, mobile, bbm, msn, twitter):

ID | USERID | TYPE | VALUE
1  |    8   |  2   | 076773635

I have set up a model to access these contact details, there may be none, or at best one of each type;

function getContactDetails($userid) {
    $query = $this->db->query("SELECT * FROM who_user_contact_info WHERE userid=".$userid);

    foreach ($query->result() as $row)
    {
        switch ($row->type) {
            case 1:
                $contact['facebook'] = $row->value;  
            case 2:
                $contact['mobile'] = $row->value; 
            case 3:
                $contact['bbm'] = $row->value; 
            case 4:
                $contact['msn'] = $row->value;  
            case 5:
                $contact['twitter'] = $row->value; 
        }

    }


    return $contact;
}

My controller then passed this data to my view;

<h3>Contact Info</h2>
<?php   
    echo "Facebook: ". $facebook ."<br />";
    echo "mobile:".  $mobile ." <br />";
    echo "bbm:".  $bbm ." <br />";
    echo "msn:".  $msn ." <br />";
    echo "Twitter:".  $twitter ." <br />";
?>

This seems to nearly work, but there must be a mistake, as for example say type 3 and 5 didn't exist, mobile and twitter shouldn't have been assigned anything! yet on my output it gives the value from the one before, eg bbm=mobile and twitter=msn.

How is it possible for these array to be there when the case has not set them? or is my logic wrong?

1
  • While your answer is any of the break; answers, you can return from the switch (and presumably remove the foreach), as your code suggests getContactDetails() will only ever accept a single $userid. If that were the case, you wouldn't need the break; as each case would itself return from the function. Commented Apr 4, 2012 at 13:23

2 Answers 2

7

Don't you need to put a break under each case?

case 1:
$contact['facebook'] = $row->value;  
break;

etc

Sign up to request clarification or add additional context in comments.

Comments

0

You need a break; in each case after setting the variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.