简体   繁体   中英

Break out of foreach loop php

I have a script which prints out messages from within a database.

Each message is written by a different user.

What I would like is for only users who are friends with the message creator, be able to view the message.

The script I have is:

foreach($updatesarray as $data)
 {
 $qu=mysql_query("SELECT frid FROM friends WHERE usid='$uid'")or die(mysql_error());
 $dataq=array();
 $qurow=mysql_fetch_array($qu);
 $dataq[]=$qurow['frid'];
foreach($dataq as $value){
  if($value!=$data['uid_fk'])
    continue;
}

/ Continues the output of the message

The first foreach takes the data from the mysql query array. Then within that foreach I perform another mysql query to identify the list of friends for the currently logged in user. I put the results into an array and create a new foreach. If any of the loops are found NOT to be a user, it will then continue... But it isn't continuing how I would like it to. I need it to skip the whole output of the message, instead of continuing the second foreach.

The variable $data['uid_fk'] is the message creator stored in the database.

Database query:

if($viewtype=="friends"){
         $viewquery="M.uid_fk=U.uid";
         $query = mysql_query("SELECT M.msg_id, M.uid_fk, M.message, M.created,       U.username, U.firstname, U.middlename, U.surname, M.uploads FROM messages M, users U  WHERE     $viewquery $morequery order by M.msg_id desc limit " .$this->perpage) or     die(mysql_error());

        }

您必须在查询中加入消息和好友表,并且一举两得。

continue时设置一个标志,如果设置了该标志->不输出消息。

Try this to skip your first loop:

continue 2;

This 2 parameter controls which of the nested loops will continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM