By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Message generator

P: 17
Hello guys
i want to create a random message generator to add new messages to database, but without causing any slow scripts.
Here are my trials till now
php to generate new messages
Expand|Select|Wrap|Line Numbers
  1. $no_of_msgs = $_POST['No_of_msgs'];
  2.  
  3. $randMsg = array("Hi, welcome to my page!", "Hey you! Thanks for visiting!","Bonjour, mon ami!", "So, do you like the site?", "Come one, come all!");
  4. $MsgNum = count($randMsg);
  5. $MsgGenNum = floor(rand(1,count($randMsg)));
  6.  
  7. if ($randMsg[$MsgGenNum] != "")
  8. {
  9.     $msg = $randMsg[$MsgGenNum];
  10. }
  11. else 
  12. {
  13.     $msg = "There was an empty message";
  14. }
  15.  
  16. $time = mysql_query("SELECT max( Date_Time ) as date_time FROM messages WHERE Simulation_ID = 5")or die (mysql_error());
  17. $result_time = mysql_fetch_array($time);
  18. $time_to_insert = $result_time['date_time'] + 60;
  19.  
  20. for($i=0;$i<$no_of_msgs;$i++)
  21. {
  22. $sql_from = mysql_query("SELECT User_ID, users.Group_ID FROM simulationgroups, users
  23.                           WHERE Simulation_ID = ".$_SESSION['sim_id']."
  24.                           AND users.Group_ID = simulationgroups.Group_ID
  25.                           ORDER BY rand( )");
  26.  
  27.  $sql_to = mysql_query("SELECT User_ID, users.Group_ID FROM simulationgroups, users
  28.                          WHERE Simulation_ID = ".$_SESSION['sim_id']."
  29.                          AND users.Group_ID = simulationgroups.Group_ID
  30.                          ORDER BY rand( )");
  31.  
  32.  $sql_label = mysql_query("SELECT class FROM labels ORDER BY rand()");
  33.  
  34.   $result_from = mysql_fetch_array($sql_from);
  35.   $result_to = mysql_fetch_array($sql_to);
  36.   $result_label = mysql_fetch_array($sql_label);
  37.  
  38.  
  39.     $insert = mysql_query("INSERT INTO messages (Date_Time,From_User,To_user,Msg_body,Group_ID,To_Group_ID,Simulation_ID,Type_of_msg)
  40.                            VALUES ('".$time_to_insert."',".$result_from['User_ID'].",".$result_to['User_ID'].",'".$msg."',".$result_from['Group_ID'].",".$result_to['Group_ID'].",".$_SESSION['sim_id'].",'".$result_label['class']."')") or die(mysql_error());
  41.  
  42.     $time_to_insert+=60;
  43.  
  44. }
  45.  
And this is the code to retrieve new data (javascript)
Expand|Select|Wrap|Line Numbers
  1. function updateMessage()
  2. {
  3.     gettime();
  4.     checkSimulationStatus();
  5.     $.post("db.php",
  6.             {MsgNo :$("#no_of_msgs").val()},
  7.             function(data)
  8.             {
  9.                 $(data).find("message").each(function() {
  10.                 var msg_id = $(this).find("msg_id").text();
  11.                 var date_time = $(this).find("date_time").text();
  12.                 var from_user = $(this).find("from_user").text();
  13.                 var from_user_id = $(this).find("from_user_id").text();
  14.                 var from_group = $(this).find("from_group").text();
  15.                 var to_user = $(this).find("to_user").text();
  16.                 var to_user_id = $(this).find("to_user_id").text();
  17.                 var to_group = $(this).find("to_group").text();
  18.                 var msg_type = $(this).find("msg_type").text();
  19.                 var msg = $(this).find("msg").text();
  20.                 var from_grp_abr = $(this).find("from_grp_abr").text();
  21.                 var to_grp_abr = $(this).find("to_grp_abr").text();
  22.                 var flagged = $(this).find("flagged").text();
  23.                 var onlydate = getonlydate(date_time);
  24.                 $("#no_of_msgs").val(msg_id);
  25.  
  26.                 if (flagged == 'true')
  27.                 {var class_flag = 'flagged';}
  28.                 else {var class_flag = 'unflag';}
  29.  
  30.                 if (from_group == $("#login").val())
  31.                 {
  32.                     var reply = '';
  33.                 }
  34.                 else {var reply = 'reply';}
  35.  
  36.                 if(from_user == "")
  37.                 {
  38.                     var handle_reply = from_grp_abr;
  39.                 }
  40.                 else 
  41.                 {
  42.                     var handle_reply = from_user;
  43.                 }
  44.  
  45.                 var html = "<tr id='row_"+msg_id+"'>";
  46.                 html += "<td><a class='bullet' onclick='changeStatus(\""+msg_id+"\")'>&nbsp;<\/a><\/td>";
  47.                 html += "<td><a class='"+reply+"' onclick=\"reply('"+escape(handle_reply)+"','"+escape(to_user)+"',"+from_user_id+","+to_user_id+");\">  <\/a><\/td>";
  48.                  html += "<td class='time'>"+date_time+"<\/td>";
  49.                   html += "<td>"+from_user+"&nbsp;["+from_grp_abr+"]"+"<\/td>";
  50.                   html += "<td>"+to_user+"&nbsp;["+to_grp_abr+"]"+"<\/td>";
  51.                   html += "<td><a href='#' class="+msg_type+"><\/a><\/td>";
  52.                 html += "<td><a id='flag_"+msg_id+"' class='"+class_flag+"' onclick='flagMsg("+msg_id+")'>  <\/a><\/td>";
  53.                 html += "<td>"+msg+"<\/td>";
  54.                 html += "<td>"+from_grp_abr+"<\/td><\/tr>";
  55.  
  56.                $('#no_of_msgs').val(msg_id);
  57.  
  58.                $("#tbody1").prepend(html);
  59.                 updatetabledata('t1');
  60.                 alternateRows('t1');
  61.         //append data to tab2 messages received
  62.         if (to_group == $("#login").val())
  63.               {
  64.                    $("#tbody2").prepend(html);
  65.                    updatetabledata('t2');
  66.                    alternateRows('t2');
  67.              }
  68.           //append data to tab3 sent messages
  69.              else if (from_group == $("#login").val())
  70.              {
  71.                 $("#tbody3").prepend(html);
  72.                 updatetabledata('t3');
  73.                 alternateRows('t3');
  74.             }
  75.  
  76.              if(from_group != $("#login").val())
  77.             {
  78.                 $("#tbody"+from_group).prepend(html);
  79.                 updatetabledata('t'+from_group);
  80.                 alternateRows('t'+from_group);
  81.             }
  82.              if(to_group != $("#login").val())
  83.             {
  84.                 $("#tbody"+to_group).prepend(html);
  85.             //    updatetabledata('t'+to_group);
  86.                 alternateRows('t'+to_group);    
  87.             }
  88.             if (class_flag == 'flagged')
  89.             {
  90.                 $("#tbody4").prepend(html);
  91.                 updatetabledata('t4');
  92.                 alternateRows('t4');
  93.             }
  94.         });
  95.     });
  96.        setTimeout('updateMessage()',3000);    
  97. }
  98.  
I tried adding up to 200 messages but it says slow script.
Any ideas.
Apr 26 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.