Come to develop a small tanned algorithm logic and need help from someone to be able to finish it! Let's look at the code:
function likesresults($user_like,$total_like, $postid){
if($user_like == 1 && $total_like == 1){
$resposta_like = '<span id="you'.$_SESSION['user_id'].'">You like this.</span>';
}
if ($user_like == 1 && $total_like >= 2) {
$sub_like = $total_like-1;
if($sub_like -3 == 0) {
} else {
$total = $sub_like - 3;
$resto = ', and others '.$total.'';
}
$resposta_like = '<span id="you'.$_SESSION['user_id'].'">You, user1,user2,user3,user4'.$resto.' like this.';
}
if ($user_like == 0 && $total_like >= 1) {
if($total_like -3 == 0) {
} else {
$total = $sub_like - 3;
$resto = ', and others '.$total.'';
}
$resposta_like = 'user1,user2,user3,user4'.$resto.' like this.';
}
if($total_like == 0){
$resposta_like = "";
}
return $resposta_like;
}
It is simple and easy to understand, the problem is that I would like to show 4 users who also likes such content, and leave in order of "friendship", for example if the user likes it X1, beauty!, Displays it, but if X2 what the user likes and besides all still friends of the logged in user he has to appear before the X1, they are friends!, understood what I meant? I'm too lost in the logic of how to make this work, please give me a light, already thanks!
Resume: I would just like to list who likes that sort who likes content and order of friendship with the logged in user, ie who is friend appears before those who are not, as well as on facebook. .
Related
I have a variable, that gets the value from the URL.
if (isset($_GET['page'])) {
$getPage = $_GET['page'];
} else {
$getPage = "";
}
if ($getPage == "" || $getPage == "1") {
$page = 0;
} else {
$page = ($getPage * 6) - 6;
}
After it get this value it sends a query to the database, to ask for information. How can I make sure that the input is numeric and doesn't exceed the available amount?
Here is the query:
$query = "SELECT * FROM dbname LIMIT $page,6 ";
$select_all_list_items = mysqli_query($connection, $query);
Right now if i alter the url manually and put in a number that exceeds the page count, it shows nothing, or if I put in letters there it shows an error. In both cases I would like to redirect the user back to the first page.
To check numeric input and invalid page number,
$pageCount = $totalRecords / $recordsPerPage;
/* where $totalRecords is count of total records from db and $recordsPerPage is total rows per page */
if(!is_numeric($_GET['page']) || $_GET['page']>$pageCount){
$getPage = "1";
}
First you'll need to retrieve the total pages from your database:
$countQuery = 'SELECT COUNT(*) FROM dbname';
$countResult = mysqli_query($connection, $countQuery);
$countResultRow = mysqli_fetch_row($countResult);
$numPages = intval($countResultRow[0]);
Then you will need to implement some checks to your get variable:
if ($numPages < 0) {
throw new Exception('No pages to show!');
}
if (!is_numeric($getPage)) {
$getPage = 1;
}
if ($getPage > $numPages) {
$getPage = 1;
}
if ($getPage < 1) {
$getPage = 1;
}
Be careful passing GET values directly into your SQL query, this is a security risk as it can lead to database manipulation via URL. Read up about SQL injection for more information about "escaping" your data pre-query.
Something, like this
$page = 1; //default page 1
if(isset($_GET['page']) && preg_match('/[a-z]/i', $_GET['page'])){
// if set and alpha
header('Location: url of first page');
exit;
}else{
//not sure of this part but ( that's what the OP has so i'll go with it )
$page = ( $_GET['page'] * 6 ) - 6; ///could be 0 on page 1 6*1-6, just saying
}
Personally I'd run a query first to count the total rows, divide that by rows per page, for the total pages, and use that in the if statement... etc.
On the page I'm creating there is graphic representation of pigeon-holes with five compartments for new comments. If there is new unread comment it should show up as graphic in one random compartment. But it won't happen if all of the 5 are
already occupied. So if someone writes new comment I like the code to check for if there is already five of them taken, and if not, than to randomly occupied one of the remaining ones. So "new_c" stands for number of
unread (new) comments, and c1-c5 stands for compartments. Value 0 means empty compartment for each "c". I was trying to make code that would first separate new_c from rest of the array after knowing the number is smaller than 5,
so I'm only working with 5 compartments. And than to determine which one is/are empty. And then randomly choose one empty to occupy. It's not really working as it is I probably am not using that array_keys properly as c2 is changed to some other value than 0 but still is being echoed, there is probably also a better/more efficient way to achieve that. I will really appreciate some input.
<?php
$tucQuery = "SELECT new_c,c1,c2,c3,c4,c5 FROM users WHERE id = '{$author_id}' LIMIT 1";
$result_tuc = mysqli_query($connection, $tucQuery);
$tucArray = mysqli_fetch_assoc($result_tuc);
mysqli_free_result($result_tuc);
$new_c = $tucArray[0];
if($new_c < 5){
$new_array = array_keys((array_slice($tucArray,1)), 0);
$rand_zero = array_rand($new_array, 1);
echo $rand_zero + 1;
}
?>
Bellow code works but it doesn't seem to be efficient and there is probably a better way.
if($new_c < 5){
$empties = array();
if($tucArray['c1'] == 0){
$empties[] = 1;
}
if($tucArray['c2'] == 0){
$empties[] = 2;
}
if($tucArray['c3'] == 0){
$empties[] = 3;
}
if($tucArray['c4'] == 0){
$empties[] = 4;
}
if($tucArray['c5'] == 0){
$empties[] = 5;
}
print_r($empties);
$rand_zero = array_rand((array_flip($empties)), 1);
echo $rand_zero;
}
I want the program to ignore zero totals. I'm trying to stop auto awards to non-participants with zero totals. I added the >= expression after the && operators. Is this correct for kills and networth?
$fetch_nor_killers = mysql_query("SELECT wk,bk,hk,dk,pk,mk, code, p, (wk+bk+hk+dk+pk+mk) as totalKills FROM r$round[round]_p WHERE status = '". normal ."' ORDER BY totalKills DESC LIMIT 3");
$killa_rank = 0;
while($killa = mysql_fetch_array($fetch_nor_killers))
{
$killa_rank++;
if($killa_rank == 1 && $killa['totalKills'] >= 1)
{
$killa_award = "freeopkiller1";
}
else
if($killa_rank == 2 && $killa['totalKills'] >= 1)
{
$killa_award = "freeopkiller2";
}
else
if($killa_rank == 3 && $killa['totalKills'] >= 1)
{
$killa_award = "freeopkiller3";
}
Same thing here with networth. Just trying to avoid awarding a inactivate user. #Jagrut
$fetch_top_subs = mysql_query("SELECT * FROM r$round[round]_p WHERE subscription != '". none ."' ORDER BY networth DESC LIMIT 3");
$sub_rank = 0;
while($sub = mysql_fetch_array($fetch_top_subs))
{
$sub_rank++;
if($sub_rank == 1 && $sub_rank['networth'] >= 1)
{
$sub_award = "first_sub";
}
else
if($sub_rank == 2 && $sub_rank['networth'] >= 1)
{
$sub_award = "second_sub";
}
About to test these.
First, i would like you to be more clear with the question. But from what i have understood, this can be the problem with your code:
You are using the $totalKills directly. You can either assign it to a variable and then use it or you need to use $killa for accessing $totalKills, i.e.
Use it as:
$anyVar = $killa['totalKills'];
and then use $anyVar for your operations or directly use $killa['totalKills'] in place of $totalKills.
Hope this helped.
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");
We have a system on our website wherein you can have multiple accounts and earn points on each. Those points you can transfer between the accounts on this webpage that I've noticed an error on. Basically, if I have a certain combination of accounts, namely a first and second, it won't let me transfer, it'll just say "Please fill in with a number.". If I have a first and third or all three, it works fine. I've been looking through it for about two hours now and can't find what isn't working... any help would be IMMENSELY appreciated :D
<?php if($a == "exchange")
{
$GetUserInfo = mysql_query("SELECT * FROM members WHERE id = '$userid'") or die(mysql_error());
$GetUserInfo = mysql_fetch_object($GetUserInfo);
$cols = 1; //determines colspan
$status = 1;
$GetMultipleInfo = mysql_query("SELECT * FROM members WHERE id = '".$GetUserInfo->mult_uid."'") or die(mysql_error());
if(mysql_num_rows($GetMultipleInfo) != 0)
{
++$cols;
++$status;
}
$GetMultipleInfo = mysql_fetch_object($GetMultipleInfo);
$GetAdMultipleInfo = mysql_query("SELECT * FROM members WHERE id = '".$GetUserInfo->mult_admin."'") or die(mysql_error());
if(mysql_num_rows($GetAdMultipleInfo) != 0)
{
++$cols;
$status = ($status == 2 ? 4 : 3);
}
$GetAdMultipleInfo = mysql_fetch_object($GetAdMultipleInfo);
// Sparks Transfer
if (isset($_POST['spartrans']))
{
$order = $_POST['sparrecipients'];
if ($order == 'first')
{
$tpoints2 = $_POST['tpoints2'];
$tpoints3 = $_POST['tpoints3'];
$tpoints = $tpoints2 + $tpoints3;
if ($status == 2)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || empty($tpoints1) || empty($tpoints2))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
elseif ($status == 3)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints3)) || empty($tpoints1) || empty($tpoints3))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
elseif ($status == 4)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || (!is_numeric($tpoints3)) || empty($tpoints1) || empty($tpoints2) || empty($tpoints3))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($tpoints2 > $GetMultipleInfo->tpoints)
message("" . getName($GetMultipleInfo->id) . " does not have enough sparks.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($tpoints3 > $GetAdMultipleInfo->tpoints)
message("" . getName($GetAdMultipleInfo->id) . " does not have enough sparks.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($GetUserInfo->mult_uid != 0)
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints - $tpoints2,0) WHERE id = '".$GetMultipleInfo->id."'") or die(mysql_error());
if ($GetUserInfo->mult_admin != 0)
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints - $tpoints3,0) WHERE id = '".$GetAdMultipleInfo->id."'") or die(mysql_error());
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints + $tpoints,0) WHERE id = '$userid'") or die(mysql_error());
message("Successfully transferred $tpoints Sparks to ".getName($userid).".","Enchanted Hogwarts","$PHP_SELF?a=exchange");
}
}
}
?>
In your code here you are establishing a couple of variables.
if ($order == 'first')
{
$tpoints2 = $_POST['tpoints2'];
$tpoints3 = $_POST['tpoints3'];
$tpoints = $tpoints2 + $tpoints3;
But then here you are checking the values of different variables. $tpoints1 was never established.
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || empty($tpoints1) || empty($tpoints2))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
EDIT: That is probably not correct. You probably have register_globals on which would be setting that variable automatically. Not recommended but not the question you asked.
Bottom line you need to figure out which branch of your code is being executed. My guess based on your description is that it is the $order == first branch and then $status == 3. But without actually submitting the form and knowing which selections were made it is impossible for me to tell. And the fact that all the error messages are the same "Please fill in with a number." is not very helpful.
I personally would take this time to refactor some of your code. If I don't understand something I break it apart into smaller chunks until I do. Otherwise you will always be sifting through this page every time something is wrong trying to figure it out.
Every selection of $order validates the values of $tpoints1, $tpoints2, and $tpoints3 exactly the same way. Start by moving that into a function and go from there.