I'm creating a CRM where I want to assign coaches to members on signup. I'm struggling to find a logic for the below scenarios.
1) At the moment we have 50 users and 5 coaches.
2) We want to assign coach 1st to user 1st, coach 2nd to user 2nd and so on.
3) Coach 1st will then assigned to user 6th. By using this approach each coach will get equal users.
I'm using below code to achieve it, it works for me, but I don't know how reliable is this.
static function getACoach($userId) {
$totalCoach = 5;
if($userId > $totalCoach) {
$coachId = $userId % $totalCoach;
} else {
$coachId = $userId;
}
}
Thank you!
This simple approach worked for me.
function getACoach($userId) {
$totalCoach = 5;
if($userId > $totalCoach) {
$coachId = $userId % $totalCoach;
} else {
$coachId = $userId;
}
return $coachId;
}
Thank you!
Related
I am trying to perform a wildcard search query using CI.
I am checking for loggedin sessions. If a user is logged into more than one device, a popup is displayed. To check if the user is logged in from any other device, I perform a wildcard query to check the existence of its user id in the user_data column and return the number of rows. The problem I am facing is, even if I am logging in for the first time, it goes to the else clause and not to if or if-else clause.
In my controller, I am checking if it reaches the else clause, display a popup. However, it is going to the else clause even if someone is logging in for the first time.
Model:
public function update_bool($id) {
$this->db->select('user_data');
$this->db->from('ci_sess');
$this->db->like('user_data', $id, 'both');
$counter = $this->db->get()->row();
if (empty($counter)) $counter = 0;
else if($counter === 1) $counter = 1;
else $counter = 3;
return $counter;
}
Controller:
$counter = $this->ion_auth_model->loggedin_status_update_bool($userId);
if($this->ion_auth_model->loggedin_status_update_bool($userId) === 3) {
warning('multiple_session_title', 'multiple_session_text');
}
You need to count number of rows return by the query. And based on no of records, your condition will work. Currently its returning one row of type array. And $counter as array to match else if condition will always fail.
I hope this will help you.
....
$query = $this->db->get();
$counter = $query->num_rows();
if (empty($counter)) {
$counter = 0;
}else if($counter === 1) {
$counter = 1;
} else {
$counter = 3;
}
return $counter;
I think your shorthand might be off a little and if I understand what you mean then there would be multiple sessions for the same user in the table meaning you couldn't get it using ->row().
Try like this:
public function update_bool($id) {
$this->db->select('user_data');
$this->db->from('ci_sess');
$this->db->like('user_data', $id, 'both');
$query = $this->db->get();
return $query->num_rows(); // This will give you now many times this user exists in the database, eg 0, 1, 2, 3
}
Then clean up your controller so you're not calling the database twice:
$counter = $this->ion_auth_model->loggedin_status_update_bool($userId);
if($counter == 3) {
warning('multiple_session_title', 'multiple_session_text');
}
Sorry for my English,
I need to display a banner ad from a database every third row in php. Banner ads and rows with the classified are selecting from database. To this moment I create something like this:
Rows with the classifieds from database:
while(#$row_select_kat = mysql_fetch_assoc($results_select_kat))
{
... //classifieds from database
if(($row_select_kat['id_ogloszenia']%3)==0)
{
$lim = 1;
echo"<div id=\"rek_poz_a\">";
modAddsDisplayHelper::wyswietlReklamyPozA();
echo"</div>";
}
}
Selecting banner ads from databes:
public static function wyswietlReklamyPozA()
{
$baza_danych = &JFactory::getDbo();
$query_sel_all_banners = "SELECT * FROM juxhv_banners";
$baza_danych->setQuery($query_sel_all_banners);
$baza_danych->query();
$banner_ads = $baza_danych->loadObjectList();
echo$query_sel_all_banners;
echo"Liczba x= ".$l_ogloszen;
foreach($banner_ads as $banner_ad)
{
if($banner_ad->state==1)
{
echo"$banner_ad->name<br/><img src=\"images/$banner_ad->username/bannerwerbung/$banner_ad->zdjecie\" width=\"434px\" height=\"94px\" />";
}
else
{
echo"No banner";
}
}
}
When I doing this in that way at every three rows displaying all three banner ads. How can I display only one banner ad every three rows?
URL: http://www.nachbarhilft.de
Greetings
You need to use external counter variable which will be incremented on each iteration and depending on its condition you can check modulo condition: Please see the following updated code.
[code]
$recordCount = 0;
while(#$row_select_kat = mysql_fetch_assoc($results_select_kat))
{
if(($recordCount%3)==0)
{
echo"<div id=\"rek_poz_a\">";
modAddsDisplayHelper::wyswietlReklamyPozA();
echo"</div>";
}
$recordCount++;
}
if($banner_ad->state == 1) {
echo"$banner_ad->name<br/><img src=\"images/$banner_ad->username/bannerwerbung/$banner_ad->zdjecie\" width=\"434px\" height=\"94px\" />";
break;
}
In this way you only print one banner, but always will be the first with state == 1 in the database.
Maybe is better improve the query:
$query_sel_all_banners = "SELECT * FROM juxhv_banners WHERE state = 1 LIMIT 1";
That will give you the same result, I think.
I hope it helps
I am a bit stuck with my code.
I am practising and would like to achive the following.
Before an user sign up, i want to check the users username exists, if exists than increase the name by one.
So my logic works like this. User gives his/her first and last name, generates an username based on that.
function
function unique_name($first_name, $last_name) {
$username = strtolower($first_name.'.'.$last_name)
$check = mysqL_query(" SELECT username WHERE username = '".$username."' ");
if(count($check == 1)) {
$i = 2;
foreach($check as $user) {
return $user['username'].$i;
$i++;
}
} else {
return $username;
}
}
$username = unique_name($_POST['first_name'], $_POST['last_name']);
// saveing the increased username
So actually the script should check if the generated unique username exsits if exsits increase, and encrease it even if it exsits with a number
example
Tom Anderson signs up, username will be tom.anderson, saved as tom anderson
Second Tom Anderson signs up, username will be tom.anderson, it already exists, save the new one as tom.anderson2.
The third user signs up he is tom anderson too, username tom.anderson* exsits twice, increase to tom.anderson3.
So i dont have a problem checking if the user exsits and saveing it, my problem is, how to increase every username if exist?
I am not asking anybody to write this for me i am nust looking for a hint
Thank you
EDIT
Thank you for the replies but this does not work
if(count($check) == 1) {
$i = 2;
foreach($check as $user) {
return $user['username'].count($check);
$i++;
}
} else {
return $username;
}
This only check the username with out a number, so if username tom.anderson exists it increases to tom.anderson1 but if tom.anderson1 exists too it wont increase to tom.anderson2
Can you have something that just appends the count of the rows returned, unless it's 0?
if(count($check) > 0) {
return $user['username'].count($check);
} else {
return $username;
}
change the SQL:
" SELECT username WHERE username LIKE '".$username."%' "
Try to give like this
if(count($check) == 1) {
$i = 2;
foreach($check as $user) {
return $user['username'].count($check);
$i++;
}
} else {
return $username;
}
You may add new field into user table, and called it base_username.
Them fetch count rows with given username, and increase every username on count found
Something like this
`function unique_name($first_name, $last_name) {
$unique_name = $username = strtolower($first_name.'.'.$last_name)
$cnt = get_row(" SELECT COUNT(*) from `users` WHERE base_username = '".$username."' LIMIT 1 ");
if($cnt){
$unique_name = $username . $cnt + 1;
}
return $unique_name;
}`
I think my mind must be going through a Boxing Day mess.
I am building a basic comment section for every game a sports team plays.
So, when no comments are entered (in the MySQL DB), I simply want to display "Be the first to enter a comment"; otherwise, display the comment results table in html format.
I can easily display the comment result table.
For some reason, I can't get the IF no comments to work properly. Feel so amateurish right now . . . :-)
I have declared row count:
$row_count = 0;
I am adding to the count inside the while statement
while($row = mysql_fetch_array($result))
{
// adding to count
$row_count++;
My count is working as I can display the row number to the screen.
Here is IF / ELSE my code:
if ($row_count === 0) {
echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
} else {
// no need to show code as this already works!
you can use
mysql_num_rows($queryReference)
Hope this helps.
Thanks.
Please do one thing print this value using below function and tell me what is output
var_dump($row_count);
or you can use == instead of ===
$query = mysql_query("SELECT * FROM comments");
$c = mysql_num_rows($query);
if($c==0) {
echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
}
else {
while($row = mysql_fetch_array($query))
{
$vars = $row[index];
}
}
$row_count = 0;
I am adding to the count inside the while statement
while($row = mysql_fetch_array($result))
{
// adding to count
$row_count++;
}
My count is working as I can display the row number to the screen.
Here is IF / ELSE code:
if ($row_count == 0) {
echo "<p>Be the first to enter a game comment and earn points toward your next fan badge.</p>";
} else {
// no need to show code as this already works!
}
My program is checking for a winner in my blackjack game after the user clicks "stand".
When I try calling the winner with AJAX, I want to see if there is a winner. If yes, then alert the winner. Else, allow the dealer to make his move. Any help would be greatly appreciated.
Sample result/output after stand: User hand = dQ, hJ; val = 20 - alerts " wins! in stand"
Expected result after this hand: - go to ajaxDealer(user);
php code:
($checkPlayerHandValue works fine, i.e. if User has C5,HQ, will return 15)
function checkWinner($u, $d, $uValue) {
if($uValue > 21)
return $d;
}
if($checkPlayerHandValue > 0) {
$checkPlayerHandValue = checkPlayerHandValue($user);
$winner = checkWinner($user, $dealer , $checkPlayerHandValue);
}
$row = mysql_fetch_array($checkNextTurn);
$playerTurn = $row['user'];
echo($playerTurn."|");
echo($winner);
js code:
xmlhttpB.onreadystatechange=function() {
if (xmlhttpB.readyState==4 && xmlhttpB.status==200) {
elements = xmlhttpB.responseText.split("|");
user = elements[0];
winner = elements[1];
//alert(winner);
if(winner != "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");
}
clickButton = true;
}
Do yourself a favor and start using the jQuery library with it's AJAX module.
As far as your question goes, can you tell us what the current problem with your code is (what is not happening that should be happening, or vice versa).
Also, I think this logic is incorrect:
if(winner != "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");
And should be flipped:
if(winner == "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");