php fantasy draft script [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm looking to create a snake draft script. I want the order to go up the array then back down the array.
$teams = array('mike','jim','chris','tim');
I want the order to be
Round 1
Mike, Jim, Chris, Tim
Round 2
Tim, Chris, Jim, Mike
That would be two rounds. I want that to be the order and repeat that but I cant figure it out with PHP.
I have tried getting the last pick from the draft in my database.
$lastpick = (the last team name submitted to my database);
if(empty($lastpick) && $teams(0) == 'mike'){
//do this
}else if($lastpick == $teams(0) && $teams(1) == 'jim'){
// do this
}else if($lastpick == $teams(1) && $teams(2) == 'chris'){
// do this
}else if($lastpick == $teams(2) && $teams(3) == 'tim'){
// do this
}else if($lastpick == $teams(3) && $teams(3) == 'tim'){
// do this
}
What would i do next? this gets the order mike,jim,chris,tim then tim again but I cant get it to back to the third team in my array.
Any help would be great thanks.

I'm not entirely sure why you need && $teams(x) == 'yyy' in each statement, you already know what they are because the order is defined, and if they change, you need to update all these statements.
You will need some form of flag to define which way you're currently moving, up or down.
boolean movingUp = true;
if(empty($lastpick)) { // first team }
else if(movingUp && $lastpick == $teams(0)) { // second team }
else if(movingUp && $lastpick == $teams(1)) { // third team }
else if(movingUp && $lastpick == $teams(2)) { movingUp = false; // fourth team }
else if(!movingUp && $lastpick == $teams(3)) { // fourth team }
else if(!movingUp && $lastpick == $teams(2)) { // third team }
else if(!movingUp && $lastpick == $teams(1)) { // second team }
else if(!movingUp && $lastpick == $teams(1)) { movingUp = true; $lastpick = null; // first team }
You don't need to have a $lastpick == $teams(3) because once you have picked the penultimate one you know the next action, whilst moving up, is the last team.

Related

Advanced Search Function with PHP

currently I have a form that allows users to fill in 3 input fields. 3 of them are text fields (title, author and ISBN) and 1 of it is a select option (categories).
What I would like to achieve is to allow users to fill in any number of the 4 fields, and return the respected values. This means that if users fills in 2 input fields, there will be 2 conditions to check in the backend. 3 inputs filled means 3 conditions.
What I have currenly is this (an example, not the actual code itself):
if($title == $allMyArray["title"]){
array_push($returningResult, $allMyArray);
}else if($author == $allMyArray["author"]){
array_push($returningResult, $allMyArray);
}else if($ISBN == $allMyArray["ISBN"]){
array_push($returningResult, $allMyArray);
}else if($categoreis== $allMyArray["categories"]){
array_push($returningResult, $allMyArray);
}else{
echo "nothing";
}
This set of code works when I only fill in one specific field. For example if I fill in only the author input, and leave the other 3 options blank, I will be returned wwith the values I want. However, if I attempt to fill in 2 or more fields at once, the returned value is incorrect.
So how else can I come up with an if else statement that will check which fields are filled, and set the conditions correctly based on the inputted fields?
Thank you all for your help in advanced! Much appreciated! :)
Changing to below code will work. The problem is when you use ELSE you are limiting your conditional statements to one result only.
Optionally, you can use switch/case statement if you find IF dirty.
But it may produce duplicates, so you need to work this out. (i dont know your code, it is just an assumption)
$atLeast1Result = false;
if($title == $allMyArray["title"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($author == $allMyArray["author"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($ISBN == $allMyArray["ISBN"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($categoreis== $allMyArray["categories"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if(!$atLeast1Result ) {
echo "nothing";
} else {
$returningResult = array_unique($returningResult); // this might now work on all versions, as i dont know what this array is.
}

How to use break in if (Command) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using $i string for limit in foreach loop.
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine'){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
}
if($menu_style == 'normal'){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}
I can't want to use LIMIT in SQL query.
And tell me when i use if($i == 5){ break; } then code display just 4
menu name
Tell me how is display menu name as my required.
Something like that?
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine' && $i <= 5){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
} else if($menu_style == 'normal' && $i <= 4){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}

PHP returning value of 1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i still cannot get this review system to work properly.
i know the average rating is 3.57 but as soon as i add the if() all i am getting is an output of 1?
// collect 5 lastest reviews for viewing
$sql = "SELECT AVG(rating) FROM reviews WHERE review_id = '$id'";
$query = mysqli_query($conn, $sql);
$AvgReview = mysqli_fetch_row($query);
// Here we have the toatal row count
$AvgReviews = $AvgReview[0];
if($AvgReviews = 1){
echo '1star';
}
//next else is new
else if($AvgReviews >= 1){
echo '2star';
}
//next else is new
else if($AvgReviews >= 2){
echo '3star';
}
//next else is new
else if($AvgReviews >= 3){
echo '4star';
}
//next else is new
else if($AvgReviews >= 4){
echo '5star';
}
You are getting '1star' because you are assigning a value of 1 to $AvgReviews.
Change your code to compare the value.
if ($AvgReviews == 1) {
Additionally, you need to reverse your code or you will end up getting 1 or 2 stars, only.
I would recommend to change it like that:
if ($AvgReviews > 4) {
echo '5star';
}
else if($AvgReviews > 3) {
echo '4star';
}
else if($AvgReviews > 2) {
echo '3star';
}
else if($AvgReviews > 1) {
echo '2star';
}
else {
echo '1star';
}
It's because you're assigning 1 to $AvgReviews. The assignment operator (=) takes the value on the right-hand side, assigns it to the name on the left-hand side, and then returns the value that was assigned. In this case, that's always 1, which PHP interprets as true.
Use the comparison operator instead:
if($AvgReviews == 1){
echo '1star';
}

How to determine the winner by validate prediction with actual score

To find out what the result/score is of a prediction I need to validate/process the prediction with the actual score of the soccermatch.
I will assign the following amount of points to a result of a prediction.
10 points: correct prediction
5 points: winner/draw prediction correct
2 points: score prediction home-team correct
2 points: score prediction away-team correct
To validate the correct prediction was not that difficult ;-)
I'm struggling at the moment how to validate the winner of a game?
Question: How do I validate the winner of a game based on the actual score and how do I validate this with the prediction of this game?
Below the piece of code where I started the validation, is this the correct way?
for ($i=0;$i<$arrQueryUitslag["number_of_rows"];$i++)
{
$intPoints = 0;
$IntHomeScore = $arrQueryUitslag[$i]['home_score'];
$IntAwayScore = $arrQueryUitslag[$i]['away_score'];
$IntHomePrediction = $arrQueryUitslag[$i]['home_prediction'];
$IntAwayPrediction = $arrQueryUitslag[$i]['away_prediction'];
//prediction is correct
if (($IntHomeScore == $IntHomePrediction) && ($IntAwayScore == $IntAwayPrediction))
{
$intPoints = 10;
}
//one part of prediction is correct
elseif ((($IntHomeScore == $IntHomePrediction) && ($IntAwayScore != $IntAwayPrediction)) || (($IntHomeScore != $IntHomePrediction) && ($IntAwayScore == $IntAwayPrediction)))
{
$intPoints = 2;
}
//Update of insert query om scores te op te slaan in db.
}
Basically correct. For the 5 points question just add an elseif before your elseif with this code:
elseif(($IntHomeScore > $IntAwayScore) && ($IntHomePrediction > $IntAwayPrediction) || ($IntHomeScore < $IntAwayScore) && ($IntHomePrediction < $IntAwayPrediction))

PHP seems to execute one particular line of code in a false if

I hope someone here can help me out because I'm stumped on this weird bug in my PHP code.
First, here's the relevant part of the code:
//STEP 1b - PREPROCESSING OF SUBMITTED FORM DATA (table level)
while($count_table++ != $num_tables && $submit != ""){
$table_show[$count_table] = mysql_escape_string($_POST[table_show_.$count_table]);
$ne_page[$count_table] = mysql_escape_string($_POST[ne_page_.$count_table]);
}
//STEP 2 - SUBMITTED?
if($submit!=""){
//The form has been submitted
//STEP 3 - VALIDATION
//Reset counts
$count_column = 0;
$count_table = 0;
//Check for empty fields
while($count_table++ != $num_tables){ if($table_show[$count_table] != ""){ //While there are tables, validate only if they are in included in NexEdit
This is where, in a scenario I'm testing, the if returns a false value. As a result, it should just go straight back to the while in front of it (I close the while and if at the same point further on).
if($ne_page[$count_table] == ""){
$error = "You forgot to give a NexEdit name to one or more of the tables you want to include in NexEdit.";
}
echo "Debug";
As expected, this is never echoed, because PHP never entered the if earlier on in the first place.
while($db_field[++$count_column]){ //Stay inside the loop until we run out of db fields
if($db_field[$count_column] == "" || $db_type[$count_column] == "" || $db_table[$count_column] == "" || $ne_name[$count_column] == "" || $ne_type[$count_column] == "" || $ne_order[$count_column] == ""){ //Check if all information is entered if the column is selected to be included in NexEdit
$error = "You didn't enter all required information. Required fields are indicated with (*).";
This is where the bug happens: even though PHP shouldn't have entered the if earlier on (as demonstrated with the debug echo), it still sets the value of $error here.
}
}
}}
//More code...
What am I missing? I've been looking at this for hours and even asked a friend developer for help, but I just can't find what I did wrong.
All code in one block:
//STEP 1b - PREPROCESSING OF SUBMITTED FORM DATA (table level)
while($count_table++ != $num_tables && $submit != ""){
$table_show[$count_table] = mysql_escape_string($_POST[table_show_.$count_table]);
$ne_page[$count_table] = mysql_escape_string($_POST[ne_page_.$count_table]);
}
//STEP 2 - SUBMITTED?
if($submit!=""){
//The form has been submitted
//STEP 3 - VALIDATION
//Reset counts
$count_column = 0;
$count_table = 0;
//Check for empty fields
while($count_table++ != $num_tables){ if($table_show[$count_table] != ""){ //While there are tables, validate only if they are in included in NexEdit
if($ne_page[$count_table] == ""){
$error = "You forgot to give a NexEdit name to one or more of the tables you want to include in NexEdit.";
}
echo "Debug";
while($db_field[++$count_column]){ //Stay inside the loop until we run out of db fields
if($db_field[$count_column] == "" || $db_type[$count_column] == "" || $db_table[$count_column] == "" || $ne_name[$count_column] == "" || $ne_type[$count_column] == "" || $ne_order[$count_column] == ""){ //Check if all information is entered if the column is selected to be included in NexEdit
$error = "You didn't enter all required information. Required fields are indicated with (*).";
}
}
}}
//More code...
My first thought would be a mismatched curly brace somewhere.
You say that "it should just go straight back into the while in front of it". Unless I am mistaken, it looks like that while loop closes before the if statement.
Would a more full/unchopped version of the method be possible to post? It may help diagnose
The value of $error is never cleared during the while loop(s). So, where you think it's being filled is actually the result of one of the previous iterations of the while loop.

Categories