Jump to content

[SOLVED] PHP While() with jQuery? it doesn't show second record!


samoi

Recommended Posts

Hello guys!

 

My idea is to pull private messages to the user using PHP from MySQL, then when the user click on <div> element, then it should fadeIn() the message for him.

 

Let's see it in action:

 

<script type="text/javascript">
$(document).ready(function(){


$("#subj").click(function(){
	$("#msgBox").css('display', 'inline').fadeIn(3000);	
	});
}
});		
</script>

<?
// PHP code!
// query to get the messages!
$SQL = mysql_query("SELECT * FROM msg WHERE to_id = '".$userid."' AND `read` = \"0\"")or die(mysql_error()); 


// loop and pull more msgs !
while($row = mysql_fetch_array($SQL)){
        // Only a function to get the username instead of user id! do not see it!
$sender = sender($row["from_id"],$row["to_id"] );
        // Out put format!
echo 'From: '.$sender[0].' |,| And To '.$row["to_id"].' |,| subject! is:';
        
        // Subject here! look in it has span id of *SUBJ* !
echo '<span id="subj"><font color="red">'.$row["subj"].'</font></span>';

        // The Private message!
echo '<span id="msgBox" style="display:none;">'.$row["msg"].'</span>';

}
?>

 

This couldn't work!

 

it only works with the *FIRST* record of private messages!

But doesn't with the second message!

 

 

Help would be appreciated! :)

Link to comment
Share on other sites

Hi there,

I can't help you much with the actual jquery code but your problem is probably due to the fact that you are using a non-unique "id" for the hidden messages.

ie. all the messages (and subject spans) have the same id so the javascript is only ever going to address the first one that it finds.

 

You are going to have be more creative with your naming (eg use the item $row["to_id"]) both for the subject span and the message span then, in your javascript (jquery) extract this value from the subject span when clicked to know which message you want to expand.

 

As I say, I am not hot on jquery but you could do something this like this:

$(".subjects").click(function(){
   // get rel value (this will be the id)
   msg_id=attr("id"); // not quite sure how you get the element id in jquery, in mootools you can just use "this.id"
   $("#msgBox_"+msg_id+"").css('display', 'inline').fadeIn(3000);
});

and the code in your while loop could look like this:

while($row = mysql_fetch_array($SQL)){
  // Only a function to get the username instead of user id! do not see it!
  $sender = sender($row["from_id"],$row["to_id"] );

  // Out put format!
  echo 'From: '.$sender[0].' |,| And To '.$row["to_id"].' |,| subject! is:';

  // Subject here! look in it has span id of *SUBJ* !
  echo '<span id="'.$row["to_id"].'" class="subjects"><font color="red">'.$row["subj"].'</font></span>';

  // The Private message!
  echo '<span id="msgBox_'.$row["to_id"].'" style="display:none;">'.$row["msg"].'</span>';

}

 

Not sure if my code will work but hopefully you will get the idea of how it could be done.

 

Chris

Link to comment
Share on other sites

Hi there,

I can't help you much with the actual jquery code but your problem is probably due to the fact that you are using a non-unique "id" for the hidden messages.

ie. all the messages (and subject spans) have the same id so the javascript is only ever going to address the first one that it finds.

 

You are going to have be more creative with your naming (eg use the item $row["to_id"]) both for the subject span and the message span then, in your javascript (jquery) extract this value from the subject span when clicked to know which message you want to expand.

 

As I say, I am not hot on jquery but you could do something this like this:

$(".subjects").click(function(){
   // get rel value (this will be the id)
   msg_id=attr("id"); // not quite sure how you get the element id in jquery, in mootools you can just use "this.id"
   $("#msgBox_"+msg_id+"").css('display', 'inline').fadeIn(3000);
});

and the code in your while loop could look like this:

while($row = mysql_fetch_array($SQL)){
  // Only a function to get the username instead of user id! do not see it!
  $sender = sender($row["from_id"],$row["to_id"] );

  // Out put format!
  echo 'From: '.$sender[0].' |,| And To '.$row["to_id"].' |,| subject! is:';

  // Subject here! look in it has span id of *SUBJ* !
  echo '<span id="'.$row["to_id"].'" class="subjects"><font color="red">'.$row["subj"].'</font></span>';

  // The Private message!
  echo '<span id="msgBox_'.$row["to_id"].'" style="display:none;">'.$row["msg"].'</span>';

}

 

Not sure if my code will work but hopefully you will get the idea of how it could be done.

 

Chris

 

 

You are awesome ! and genius!!

 

Great thinking!

 

Thank you very much!

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.