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?
break;answers, you can return from theswitch(and presumably remove theforeach), as your code suggestsgetContactDetails()will only ever accept a single$userid. If that were the case, you wouldn't need thebreak;as eachcasewould itself return from the function.