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
And this is the code to retrieve new data (javascript)
I tried adding up to 200 messages but it says slow script.
Any ideas.
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
Code:
$no_of_msgs = $_POST['No_of_msgs'];
$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!");
$MsgNum = count($randMsg);
$MsgGenNum = floor(rand(1,count($randMsg)));
if ($randMsg[$MsgGenNum] != "")
{
$msg = $randMsg[$MsgGenNum];
}
else
{
$msg = "There was an empty message";
}
$time = mysql_query("SELECT max( Date_Time ) as date_time FROM messages WHERE Simulation_ID = 5")or die (mysql_error());
$result_time = mysql_fetch_array($time);
$time_to_insert = $result_time['date_time'] + 60;
for($i=0;$i<$no_of_msgs;$i++)
{
$sql_from = mysql_query("SELECT User_ID, users.Group_ID FROM simulationgroups, users
WHERE Simulation_ID = ".$_SESSION['sim_id']."
AND users.Group_ID = simulationgroups.Group_ID
ORDER BY rand( )");
$sql_to = mysql_query("SELECT User_ID, users.Group_ID FROM simulationgroups, users
WHERE Simulation_ID = ".$_SESSION['sim_id']."
AND users.Group_ID = simulationgroups.Group_ID
ORDER BY rand( )");
$sql_label = mysql_query("SELECT class FROM labels ORDER BY rand()");
$result_from = mysql_fetch_array($sql_from);
$result_to = mysql_fetch_array($sql_to);
$result_label = mysql_fetch_array($sql_label);
$insert = mysql_query("INSERT INTO messages (Date_Time,From_User,To_user,Msg_body,Group_ID,To_Group_ID,Simulation_ID,Type_of_msg)
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());
$time_to_insert+=60;
}
Code:
function updateMessage()
{
gettime();
checkSimulationStatus();
$.post("db.php",
{MsgNo :$("#no_of_msgs").val()},
function(data)
{
$(data).find("message").each(function() {
var msg_id = $(this).find("msg_id").text();
var date_time = $(this).find("date_time").text();
var from_user = $(this).find("from_user").text();
var from_user_id = $(this).find("from_user_id").text();
var from_group = $(this).find("from_group").text();
var to_user = $(this).find("to_user").text();
var to_user_id = $(this).find("to_user_id").text();
var to_group = $(this).find("to_group").text();
var msg_type = $(this).find("msg_type").text();
var msg = $(this).find("msg").text();
var from_grp_abr = $(this).find("from_grp_abr").text();
var to_grp_abr = $(this).find("to_grp_abr").text();
var flagged = $(this).find("flagged").text();
var onlydate = getonlydate(date_time);
$("#no_of_msgs").val(msg_id);
if (flagged == 'true')
{var class_flag = 'flagged';}
else {var class_flag = 'unflag';}
if (from_group == $("#login").val())
{
var reply = '';
}
else {var reply = 'reply';}
if(from_user == "")
{
var handle_reply = from_grp_abr;
}
else
{
var handle_reply = from_user;
}
var html = "<tr id='row_"+msg_id+"'>";
html += "<td><a class='bullet' onclick='changeStatus(\""+msg_id+"\")'> <\/a><\/td>";
html += "<td><a class='"+reply+"' onclick=\"reply('"+escape(handle_reply)+"','"+escape(to_user)+"',"+from_user_id+","+to_user_id+");\"> <\/a><\/td>";
html += "<td class='time'>"+date_time+"<\/td>";
html += "<td>"+from_user+" ["+from_grp_abr+"]"+"<\/td>";
html += "<td>"+to_user+" ["+to_grp_abr+"]"+"<\/td>";
html += "<td><a href='#' class="+msg_type+"><\/a><\/td>";
html += "<td><a id='flag_"+msg_id+"' class='"+class_flag+"' onclick='flagMsg("+msg_id+")'> <\/a><\/td>";
html += "<td>"+msg+"<\/td>";
html += "<td>"+from_grp_abr+"<\/td><\/tr>";
$('#no_of_msgs').val(msg_id);
$("#tbody1").prepend(html);
updatetabledata('t1');
alternateRows('t1');
//append data to tab2 messages received
if (to_group == $("#login").val())
{
$("#tbody2").prepend(html);
updatetabledata('t2');
alternateRows('t2');
}
//append data to tab3 sent messages
else if (from_group == $("#login").val())
{
$("#tbody3").prepend(html);
updatetabledata('t3');
alternateRows('t3');
}
if(from_group != $("#login").val())
{
$("#tbody"+from_group).prepend(html);
updatetabledata('t'+from_group);
alternateRows('t'+from_group);
}
if(to_group != $("#login").val())
{
$("#tbody"+to_group).prepend(html);
// updatetabledata('t'+to_group);
alternateRows('t'+to_group);
}
if (class_flag == 'flagged')
{
$("#tbody4").prepend(html);
updatetabledata('t4');
alternateRows('t4');
}
});
});
setTimeout('updateMessage()',3000);
}
Any ideas.