473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Message generator

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
0 1103

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

Similar topics

3
by: Mike | last post by:
I need to be able to store a list of schedulable items, either regular callables or generator-producing functions -- is there a way to test if a fn is generator-producing? (In the non-generator...
9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
4
by: Wai Yip Tung | last post by:
I'm attempting to turn some process than uses callback to return result into a more user friendly generator. I'm hitting some road block so any pointer would be appriciated. Let say there is an...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
12
by: Thomas Lotze | last post by:
Hi, I'm trying to figure out what is the most pythonic way to interact with a generator. The task I'm trying to accomplish is writing a PDF tokenizer, and I want to implement it as a Python...
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
11
by: vbgunz | last post by:
I am afraid that this is the first time in which I would probably need something explained to me as if I were a little child. I am having a hard time getting this through my thick skull. What in...
9
by: Kugutsumen | last post by:
I am relatively new the python language and I am afraid to be missing some clever construct or built-in way equivalent to my 'chunk' generator below. def chunk(size, items): """generate N items...
14
by: castironpi | last post by:
I'm actually curious if there's a way to write a generator function (not a generator expression) in C, or what the simplest way to do it is... besides link the Python run-time.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.