Forum Moderators: coopster
$checkgroupid = mysql_query("SELECT group_id FROM messages WHERE to_user = '$to' and from_user = '$from' ORDER BY group_id LIMIT 1");
$cgidcount = "0";
while ($line = mysql_fetch_assoc($checkgroupid)) {
$cgidcount++;
}
if ($cgidcount == "1") {
$cgid1 = mysql_query("SELECT group_id FROM messages WHERE to_user = '$to' and from_user = '$from' LIMIT 1");
while($row = mysql_fetch_array($cgid1)) {
$group_id = $row['group_id'];
}
}
if ($cgidcount == "0") {
$checkgroupid2 = mysql_query("SELECT group_id FROM messages WHERE to_user = '$from' and from_user = '$to' ORDER BY group_id LIMIT 1");
$cgid2count = "0";
while ($line = mysql_fetch_assoc($checkgroupid2)) {
$cgid2count++;
}
if ($cgid2count == "1") {
$cgid2 = mysql_query("SELECT group_id FROM messages WHERE to_user = '$from' and from_user = '$to' ORDER BY group_id LIMIT 1");
while($row = mysql_fetch_array($cgid2)) {
$group_id = $row['group_id'];
}
}
if ($cgid2count == "0") {
// The length we want the unique reference number to be
$unique_ref_length = 10;
// A true/false variable that lets us know if we've
// found a unique reference number or not
$unique_ref_found = false;
// Define possible characters.
// Notice how characters that may be confused such
// as the letter 'O' and the number zero don't exist
$possible_chars = "1234567890";
// Until we find a unique reference, keep generating new ones
while (!$unique_ref_found) {
// Start with a blank reference number
$unique_ref = "";
// Set up a counter to keep track of how many characters have
// currently been added
$i = 0;
// Add random characters from $possible_chars to $unique_ref
// until $unique_ref_length is reached
while ($i < $unique_ref_length) {
// Pick a random character from the $possible_chars list
$char = substr($possible_chars, mt_rand(0, strlen($possible_chars)-1), 1);
$unique_ref .= $char;
$i++;
}
// Our new unique reference number is generated.
// Lets check if it exists or not
$query = "SELECT group_id FROM messages
WHERE group_id='".$unique_ref."'";
$result = mysql_query($query) or die(mysql_error().' '.$query);
if (mysql_num_rows($result) == 0) {
// We've found a unique number. Lets set the $unique_ref_found
// variable to true and exit the while loop
$unique_ref_found = true;
$group_id = $unique_ref;
}
}
}
}
// ends overall checking for message group id
$group_id=null; // initialize
$unique_ref_found=null;
$startnum = 10000000;
$endnum = 9999999999;
$testnum = 0;
//
$query = "select group_id FROM messages where to_user = '$to' and from_user = '$from' order by group_id limit 1";
$result = mysql_query($query) or die("Cannot check for group id " . mysql_error());
// While = multiple records, only need "if" if there's one expected
if ($row = mysql_fetch_array($result)) { $group_id = $row['group_id']; }
//
if (! $group_id) {
while (! $group_id) {
$testnum = mt_rand($startnum, $endnum);
// Numeric queries do NOT need quoting
$query = "select group_id from messages where group_id=$testnum";
$result = mysql_query($query) or die(" error at checking testnum mysql_error().' '.$query);
if (mysql_num_rows($result) == 0) { $group_id=$testnum; }
} // end while
} // end no group id
//
// echo "Group id: $group_id";
$group_id=null; // initialize
$unique_ref_found=null;
$startnum = 10000000;
$endnum = 9999999999;
$testnum = $cnt = 0;
$max_loop=1000;
//
$query = "select group_id FROM messages where to_user = '$to' and from_user = '$from' order by group_id limit 1";
$result = mysql_query($query) or die("Cannot check for group id " . mysql_error());
// While = multiple records, only need "if" if there's one expected
if ($row = mysql_fetch_array($result)) { $group_id = $row['group_id']; }
//
if (! $group_id) {
while (! $group_id) {
$cnt++;
if ($cnt > $max_loop) { break; } // stop me from infinite loop
$testnum = mt_rand($startnum, $endnum);
// Numeric queries do NOT need quoting
$query = "select group_id from messages where group_id=$testnum";
$result = mysql_query($query) or die(" error at checking testnum mysql_error().' '.$query);
if (mysql_num_rows($result) == 0) { $group_id=$testnum; }
} // end while
} // end no group id
//
// echo "Group id: $group_id";