Jump to content

[SOLVED] Only 1 results shows


Lamez

Recommended Posts

alright similar to my last post, but different problem. My script shows who is online, their account name, and their character name. But when there are more than one account logged in, it only shows one person online, when there are more than one.

 

I have no idea what so ever why this is doing this, perhaps you guys do.

code:

<?php
define(ACC_SERVER, "localhost");
define(ACC_USER, "user");
define(ACC_PASS, "pass");
define(ACC_NAME, "name");

$connection_a = mysql_connect(ACC_SERVER,
                            ACC_USER,
                            ACC_PASS);

	 $sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error());
	 $char = mysql_fetch_array($sql);
	 $id = $char['acct'];
	 $name = $char['name'];
         $sql = mysql_query("SELECT * FROM ".ACC_NAME.".accounts WHERE `acct` = '$id'")or die(mysql_error());
     $on_num = mysql_num_rows($sql);

?>
<font face="Arial, Helvetica, sans-serif">
<h1 align="center">People Online</h1>

<table width="50%" border="0" bgcolor="#666666" align="center">
  <tr>
    <td>
<?php

if($on_num == (0)){
echo "There are no players online.";
}
              while($char = mysql_fetch_array($sql)){
	  echo $char['login'];
	  echo "(".$name.")";
	  echo " ";
	 }

?></td>
  </tr>
  <tr>
    <td><strong>Total Online</strong>: <font color="#FFFFFF"><?php echo $on_num; ?></font></td>
  </tr>
</table>
</font>

Link to comment
https://forums.phpfreaks.com/topic/116317-solved-only-1-results-shows/
Share on other sites

I cleaned up the code a bit. Nothing Different.

 

<?php
define(ACC_SERVER, "localhost");
define(ACC_USER, "usr");
define(ACC_PASS, "pass");
define(ACC_NAME, "name");

mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS);

$sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error());
        $char = mysql_fetch_array($sql);

$id = $char['acct'];
$char_name = $char['name'];

        $sql_r = mysql_query("SELECT * FROM ".ACC_NAME.".accounts WHERE `acct` = '$id'")or die(mysql_error());
    $on_num = mysql_num_rows($sql_r);      
?>
<font face="Arial, Helvetica, sans-serif">
<h1 align="center">People Online</h1>

<table width="50%" border="0" bgcolor="#666666" align="center">
  <tr>
    <td>
<?php
  if($on_num == (0)){
   echo "There are no players online.";
  }
      while($acc = mysql_fetch_array($sql_r)){
    $acc_name = $acc['login'];
         echo $acc_name."(".$char_name.") ";
       }
?></td>
  </tr>
  <tr>
    <td><strong>Total Online</strong>: <font color="#FFFFFF"><?php echo $on_num; ?></font></td>
  </tr>
</table>
</font>

try to clean upper <?php ?> part a bit

 

<?php
define(ACC_SERVER, "localhost");
define(ACC_USER, "usr");
define(ACC_PASS, "pass");
define(ACC_NAME, "name");

mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS);

$sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error());
$on_num = mysql_num_rows($sql);      
?>

ok so what I am trying to do is see who is online, but I have a problem. The name of the account is in one table, and to tell if they are online is in a different table

 

accounts-

acct(id) login(acc name)

 

characters

id char_name online

 

so I wanna take the the id, and find the account name. Then I wanna find out which char is online.

So I think I might have to make 2 loops

Just a note, I'm trying to figure everything out based on what you say works in your code and the PHP manual, since I haven't started working with MySQL in PHP.

 

By the looks of it, to iterate through ALL the rows, you'd have to loop for the first mysql_fetch_array() call. But consider that $id and $char_name would be overwritten each time — you'll have to nest the second loop into the first one, I think.

 

You're also checking for no players online but still going through with outputting the users afterwards.

 

I'd do something like this.

 

<?php
define(ACC_SERVER, "localhost");
define(ACC_USER, "usr");
define(ACC_PASS, "pass");
define(ACC_NAME, "name");

mysql_connect(ACC_SERVER, ACC_USER, ACC_PASS);

$sql = mysql_query("SELECT * FROM ".ACC_NAME.".characters WHERE `online` = '1'")or die(mysql_error());	
$on_num = mysql_num_rows($sql_r);
if($on_num == (0)){
$on_list = "There are no players online.";
}
else {
while($char = mysql_fetch_array($sql)) {
	$id = $char['acct'];
	$char_name = $char['name'];

	$sql_r = mysql_query("SELECT * FROM ".ACC_NAME.".accounts WHERE `acct` = '$id'")or die(mysql_error());
	while($acc = mysql_fetch_array($sql_r)){
		$acc_name = $acc['login'];
		$on_list .= $acc_name." (".$char_name.") ";
	}
}
}?><font face="Arial, Helvetica, sans-serif">
<h1 align="center">People Online</h1>

<table width="50%" border="0" bgcolor="#666666" align="center">
 <tr>
   <td><?php echo $on_list; ?></td>
 </tr>
 <tr>
   <td><strong>Total Online</strong>: <font color="#FFFFFF"><?php echo $on_num; ?></font></td>
 </tr>
</table>
</font>

 

Also, your HTML code is invalid for HTML 4.01. You can't use block elements (h1, table, etc.) in an inline element (in this case, font); if you want the whole page to use those fonts, use CSS on <body> tag; otherwise, use a <div>, which is a block element, and CSS.

 

@sader: I assume he wants to keep the data separate because char_name != login, and perhaps that a given user account can have multiple characters (though probably only one "active" at a time if that's the case).

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.