Jump to content

[SOLVED] PHP/MYSQL query result variable $cant issue (double for loop inside function in


nightkarnation

Recommended Posts

Hey!

 

I have a dumb question (i guess it is)

 

for a query result...can i change the variable name of $cant ???

because i have a double for loop inside a function in flash, and both are retrieving the same $cant values when they have to retrieve different ones...

for ex here one of the 2 query results which im using the $cant:

 

$result = mysql_query("SELECT chat, messageTime FROM chat WHERE messageTime > '$connectedMinute' ORDER BY `chat`.`messageTime` DESC"); 
        $cant = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "chat$cant=$row[chat]&messageTime$cant=$row[messageTime]&"; 
            $cant++;
        } 
        echo "cant=$cant";

 

(i tried with another random variable name instead of $cant...but when i do that, it doesnt receive the information from mysql)

 

Any ideas and/or solutions?

 

Thanx in advance for the help!

 

Cheers,

Link to comment
Share on other sites

what do you mean by "nothing?"  what exactly isn't working?  that single line of code should work fine if you substitute $x in for $cant instead:

 

$result = mysql_query("SELECT chat, messageTime FROM chat WHERE messageTime > '$connectedMinute' ORDER BY `chat`.`messageTime` DESC"); 
        $x = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "chat$x=$row[chat]&messageTime$x=$row[messageTime]&"; 
            $x++;
        } 
        echo "cant=$x";

 

if that doesn't work how you want it, we'll need to see more code, because it's obviously somewhere else that's causing the issue.

Link to comment
Share on other sites

Flash Code:

 

this is the for loop that has nothing to do but it could be useful since its interfering with the below for loop:
for (var i = 0; i<this.cant; i++) {
			txtUsersConnected.text = txtUsersConnected.text+this["user_id"+i]+newline;
		}

for (var e = 0; e<this.cant; e++) {
txtChat.text = txtChat.text+this["chat"+e]+newline;
trace(e); //e always reads the value from the for loop that is above this code which is retrieving another information
trace(content_lv.chat1); //this is actually working! so its getting the $chat information from php
		}

 

But if i try now with the new suggested x

 

for (var e = 0; e<this.x; e++) {
txtChat.text = txtChat.text+this["chat"+e]+newline;
trace(e); //e is not giving any value
trace(content_lv.chat1); //not giving any value
		}

Link to comment
Share on other sites

Ok, let me explain something to better understand the goal here...

 

txtChat.text should display the messages that have been stored in mysql only from 1 minute ago to now

thats why: WHERE messageTime > '$connectedMinute' (this is working just fine)

 

Ok flash info:

 

		for (var e = 0; e<this.cant; e++) {
			txtChat.text = txtChat.text+this["chat"+e]+newline;
			trace(e);
			trace(content_lv.chat0);
			trace(content_lv.chat1);
			trace(content_lv.chat2);
		}

 

(in this example...mysql had 4 msgs stored in less than 1 minute ago) if there are no msgs stored in less than a minute...content_lv.chat0 will be undefined, which is perfect.

Flash Trace output:

 

0

USERNAME: tester

USERNAME: test

USERNAME: testing

 

(Note that its giving 0 because there is only 1 user connected...if there were 2, e will be = 1) would be the same but:

1

USERNAME: tester

USERNAME: test

USERNAME: testing

 

This is because as i said before is retrieving the value from:

for (var i = 0; i<this.cant; i++) {
			txtUsersConnected.text = txtUsersConnected.text+this["user_id"+i]+newline;
		}

 

Im guessing its the this.cant from the e for() loop, but whenever i change this on php and flash, it just doesnt work

 

Hope this info helps, let me know if i wasnt clear in any aspect

Link to comment
Share on other sites

this is still fairly muddy, but it might just be me.  let's say you have four messages, that means cant should come back as 3.  that will assign users this[user_id0], this[user_id1], this[user_id2], this[user_id3] to the usersConnected text.  it will also assign this[chat0], this[chat1], this[chat2], this[chat3] to the txtChat text.  at least, it SHOULD - is that not what's happening, and if it IS happening, what's wrong with that?

 

my apologies if i'm not understanding properly.

Link to comment
Share on other sites

Yes...what ur saying there u perfectly understood, and yes...it is happening and working that way...

 

BUT...here's the thing,

I dont really want that to happen...

I want and need  this[chat0], this[chat1], this[chat2], this[chat3] to the txtChat text to have the values from the e for() loop...not from the i for() loop

 

And right now...since this.cant is the same in both loops... its giving the same value to both i & e, so its actually giving the value of how many users are connected instead of how many msgs were saved in less than the actual minute.

 

So here's the thing...the problem might still be on php,

 

Here is the complete relevant php code:

 

This one here is the one that i tried with $x ... and when i try that...it doesnt work

 

$connectedMinute = time() - 100;
//INFORMATION APARTE - SENDS LAST CHAT and messageTime FROM MYSQL DATABASE to FLASH (hacer nuevo: solamente los que estan bajo el minuto de conectados)
        $result = mysql_query("SELECT chat, messageTime FROM chat WHERE messageTime > '$connectedMinute' ORDER BY `chat`.`messageTime` DESC"); 
        $cant = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "chat$cant=$row[chat]&messageTime$cant=$row[messageTime]&"; 
            $cant++;
        } 
        echo "cant=$cant";

 

Here's the code concerning to retrieve which users are connected (this works perfect)

$result = mysql_query("SELECT user_id, lastConnection FROM tbl_auth_user WHERE lastConnection > '$lastRange'"); 
        $cant = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "user_id$cant=$row[user_id]&lastConnection$cant=$row[lastConnection]&"; 
            $cant++;
        } 
        echo "cant=$cant"; 
}

 

So here u can see the conflict of having two $cant

Link to comment
Share on other sites

i see - so the second block grabs all the users, the first block grabs all the messages.  try changing the php block for the users to this:

 

$result = mysql_query("SELECT user_id, lastConnection FROM tbl_auth_user WHERE lastConnection > '$lastRange'"); 
        $usrs = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "user_id$usrs=$row[user_id]&lastConnection$usrs=$row[lastConnection]&"; 
            $usrs++;
        } 
        echo "usrs=$usrs"; 
}

 

then change the for() loop for the users to this:

 

for (var i = 0; i<this.usrs; i++) {
			txtUsersConnected.text = txtUsersConnected.text+this["user_id"+i]+newline;
		}

 

see if that helps you out.  by the by, time() - 100 will access all messages in the last 1:40, not the last minute.

Link to comment
Share on other sites

PHP:

 

//mandar actual horario en el cual el usuario esta conectado - USER ONLINE OR OFFLINE INFORMATION
if ($action == "lastConnection")
{
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("learning") or die(mysql_error());
$name=$_POST['name']; 
$time = time();
//send new user time connection
mysql_query("UPDATE `tbl_auth_user` SET lastConnection = '$time' WHERE user_id = '$name'"); 

//RETRIEVE LAST CONNECTION FROM USER FROM DATABASE/MYSQL

$s = mysql_query("SELECT lastConnection FROM tbl_auth_user WHERE user_id = '$name'");
while($r=mysql_fetch_row($s)){
    $last = $r[0];
}

$connectedMinute = time() - 100;
//SEND LAST CHAT and messageTime FROM MYSQL DATABASE to FLASH (hacer nuevo: solamente los que estan bajo el minuto de conectados)
        $result = mysql_query("SELECT chat, messageTime FROM chat WHERE messageTime > '$connectedMinute' ORDER BY `chat`.`messageTime` DESC"); 
        $cant = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "chat$cant=$row[chat]&messageTime$cant=$row[messageTime]&"; 
            $cant++;
        } 
        echo "cant=$cant";


	//-USER CONNECTION, IF RECENT OR NOT
$range = time()-600;
if($range < $last){
//hacer un valor del ultimo horario - 600, asi abarcan solamente los usuarios activos
$lastRange = $last - 100;
//---------
    echo "User is online!";
echo "&connected=".'You Are Online: ' .$name."&";
} else {
    echo "User is offline!";
echo "&connected=".'You Are Offline'."&";
}


//DISPLAY ONLY RECENT CONNECTIONS USER NAMES:
$result = mysql_query("SELECT user_id, lastConnection FROM tbl_auth_user WHERE lastConnection > '$lastRange'"); 
        $usrs = 0; 
        while($row=mysql_fetch_array($result)){ 
            echo "user_id$usrs=$row[user_id]&lastConnection$usrs=$row[lastConnection]&"; 
            $usrs++;
        } 
        echo "usrs=$usrs"; 
}

 

FLASH CODE:

 

function lastConnection() {
//LOAD INFORMATION EVERY 1 SECOND, diciendo el time() del usuario
content_lv.action = "lastConnection";
content_lv.name = myUserId;
content_lv.sendAndLoad("http://localhost/Test/phpAndMysql/flash%20user%20admin%201.4/login.php", content_lv, "POST");
//ACA RETRIEVES INFORMATION
content_lv.onLoad = function(succes) {
	if (succes) {
		trace("looping funcionando");
		//avisa si el usuario esta conectado o no
		txtStatus.text = content_lv.connected;
		//Cada vez que carga los users...que limpie el txt asi no se repiten los users
		txtUsersConnected.text = "";
		//LOAD user_id connectados

		for (var i = 0; i<this.usrs; i++) {
			txtUsersConnected.text = txtUsersConnected.text+this["user_id"+i]+newline;
		}
		//RETRIEVE CHAT TEXT:
		txtChat.text = "";
		for (var e = 0; e<content_lv.cant; e++) {
			txtChat.text = txtChat.text+this["chat"+e]+newline;
			trace(e);
			trace(content_lv.chat0);
			trace(content_lv.chat1);
			trace(content_lv.chat2);
		}
	}
};
}
var nInterval:Number = setInterval(lastConnection, 1000);

 

Hope this helps to decipher whats going wrong

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.