Jump to content

[PHP5] A while in a foreach


Redapple

Recommended Posts

Hello everybody,

 

I have an problem debugging a script I made. I will explain what this script does and what its used for and hopefully someone can help me fix my problem.

 

I have a database table and in that table there is a row that is named login_ip.

 

Quick example of the current table

 

name

login_ip

 

example1

127.0.0.1-172.20.35.1

example2

127.0.0.1-172.20.35.1

example4

172.20.35.55

 

Now user `example1` is logged in on the website and wants to see on what IP's he have been logged in, So this script I made should display that but it is not working like it should be, It does not display all the names that matches but only example2 in the list if example1 is logged in.

 

You get what I want? The problem is that I want to let explode fetch all the names matching the IP's but I think it does not search all names because now it does only search if the first ip is the same as the currently of `example1` but I want to olso show `example2` in the list.

 

$sql = "SELECT login_ip FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
$login_ip = explode("-", "$row->login_ip");

foreach($login_ip as $name_array) {
  $sql = "SELECT * FROM users WHERE login_ip='$name_array'";
  $query = mysql_query($sql);

  while($row = mysql_fetch_array( $query )) {
    $usedname = $row['name'];
    echo " <tr>\n";
    echo "  <td bgcolor=\"#000033\"><font color=\"#999999\"><div align=\"center\"><a href=\"/viewprofile.php?user=$usedname\">$usedname</a></div></td>\n";
    echo " </tr>\n";
  }
}

Link to comment
https://forums.phpfreaks.com/topic/143194-php5-a-while-in-a-foreach/
Share on other sites

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.