I am trying to make a textbased game, but I can't seem to figure out of why this code down is not working.
It has a mysqli connection in the core file.
It has values for the chances in the database.
Rank is set in the database.
I don't get any errors, only the "Success" and "Failure" messages not showing up.
Code:
<?php
include_once "core.php";
function checkRandom($chance){
return rand(1, 100) <= (int)$chance;
}
$userid = '1';
$getAllQuery = $data->query("SELECT * FROM players WHERE id = '$userid'") or die($data->error);
while ($getall = $getAllQuery->fetch_assoc()) {
$rank = $getall['rank'];
$chance1 = $getall['crime_chance1'];
$chance2 = $getall['crime_chance2'];
$chance3 = $getall['crime_chance3'];
$chance4 = $getall['crime_chance4'];
$chance5 = $getall['crime_chance5'];
$chance6 = $getall['crime_chance6'];
}
if (isset($_POST['crime'])) {
$choice = $_POST['crime'];
$pass = 0;
$fail = 0;
if ($choice == 1 && $rank <= 1) {
echo "3esd";
if (checkRandom($chance1)) {
echo "Success";
} else {
echo "Failure";
}
} else {
if ($choice == 2 && $rank <= 2) {
if (checkRandom($chance2)) {
echo "Success";
} else {
echo "Failure";
}
}
}
}
echo "<form method='POST' action='#'>";
if ($rank >= 1) {
echo "<label><input type='radio' name='crime' value='1'>Crime 1 " . $chance1 . "% chance</label><br />";
if ($rank >= 2) {
echo "<label><input type='radio' name='crime' value='2'>Crime 2 " . $chance2 . "% chance</label><br />";
if ($rank >= 3) {
echo "<label><input type='radio' name='crime' value='3'>Crime 3 100% chance</label><br />";
}
}
}
echo "<input type='submit'>
</form>";
?>
I would really appreciate some help. :D
This code seems to work fine except under the following cases:
1) Nothing is handling Rank 3+ as the IF statements are <= 1 or <= 2 and 3 is > then 1 and 3.
2) If you select the first option, and you are not rank 1, the code won't return anything, because the first IF statement is limited to the selected choice being 1 AND the Rank being <= 1.
3) If you select the second option, and you are not rank 2, the code won't return anything, because the second IF statement is limited to the selected choice being 1 AND the Rank being <= 2.
see the following addition
}ELSE{ # condition not handled
ECHO "WE ARE HERE";
if ($choice == 1 && $rank <= 1) {
echo "3esd";
if (checkRandom($chance1)) {
echo "Success";
} else {
echo "Failure";
}
} else {
if ($choice == 2 && $rank <= 2) {
if (checkRandom($chance2)) {
echo "Success";
} else {
echo "Failure";
}
}ELSE{ # condition not handled
ECHO "WE ARE HERE";
}
}
Related
Working on a quiz website where when users submits their answers $perQuestionScore equals true when the answer is correct but false when it is wrong. I'm trying to find the instances where $perQuestionScore equals true in other to total the score but it doesn't seem to work.
My code looks like below
<?php
$perQuestionScore = 0;
if (isset($_POST['grader'])) {
if(isset($_POST[$chosen]))
{
$choice= $_POST[$chosen];
if (strpos($choice, $correctOne) !== false) {
$perQuestionScore++;
echo $_POST[$chosen] . "" . " is the correct answer";
} elseif (strpos($choice, $correctOne) == false) { echo $_POST[$chosen] . "" . " is the Wrong answer";
} else {
echo "You did not choose an answer"; {
}
}
}
}
}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
}
$conn->close();
?>
I suggest simplifying it to this:
<?php
$perQuestionScore = 0; //initialize var
if ($choice == $correct) {
$perQuestionScore++; //add 1 if correct
}
echo $perQuestionScore * 10; //i guess you want it times 10
So I have a function that takes a number and outputs it as a placement. How do I go about making the function take in consideration ties. I have a ranking database and this php code echos out rankings on a table. right now it ranks but it doesn't consider ties. How do i go about doing this
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
?>
<?php
$link = mysqli_connect("localhost", "citricide", "321213123Lol", "juneausmashbros");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM rankings ORDER BY points DESC";
$result = mysqli_query($link, $query);
echo '<article class="content grid_6 push_3">';
echo '<h1>';
echo 'Project M Summer Ranbat Rankings';
echo '</h1>';
echo '<section>';
echo '<center>';
echo '<table style="width:400px" class="rankslist">';
echo '<tr>';
echo '<th width="15%"><b>Rank</b></th>';
echo '<th width="45%"><b>Name</b></th>';
echo '<th width="45%"><b>Alias</b></th>';
echo '<th width="15%"><b>Points</b></th>';
echo '</tr>';
$ass = 0;
while($row = $result->fetch_array()) {
$ass++;
echo '<tr>';
if ($ass == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($ass == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($ass == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($ass);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</section>';
echo '</article>';
?>
It seems like you need additional variables to track the rank and the previous value. By doing so, you can handle ties.
For example:
$ass = 0; // current record count
$rank = 0; // rank
$last_points = NULL; // variable to store last ranked value
while($row = $result->fetch_array()) {
$ass++;
// check if value changes and reset rank if it does
if ($row['points'] !== $last_points) {
$rank = $ass;
$last_points = $row['points'];
}
echo '<tr>';
if ($rank == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($rank == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($rank == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($rank);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
So say your points looked like this:
100
100
90
80
The $rank value would be:
1
1
3
4
I'm having a problem with a line of code. My teacher doesn't even see the problem and I've been fighting it for almost a week and a half. any help would be greatly appreciated.
The code:
{
if (count($_POST['CINS']) > 0)
{
echo "<h2>Your CINS picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINS'] as $element)
{
echo "\t<li>$element</li>\n";
} // end of FOREACH statement
echo "</ul>\n";
} // end of IF count CINS
if (count($_POST['CINT']) > 0 )
{
echo "<h2>Your CINT picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINT'] as $element2)
{
echo "\t<li>$element2</li>\n";
} // End of FOREACH CINT
echo "</ul>\n";
} // End of IF for CINT
else
{
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
echo __LINE__;
if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0))) // This is where the problem lies. It's showing up the echo statements even when CINS has a count of 1. but if CINT has a count of 1, the echo statements do not show up.
{
echo "<h2>No classes</h2>\n";
echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
}
} // END ELSE COUNT CINS
}
?>
misplaced brackets
if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0))) -> wrong
if ((count($_POST['CINT']) == 0) and (count($_POST['CINS']) == 0))
You don't need all those parentheses:
if(
count($_POST['CINT']) == 0 AND
count($_POST['CINS']) == 0
)
Look how some indentation goes a long way:
{
if (count($_POST['CINS']) > 0) {
echo "<h2>Your CINS picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINS'] as $element) {
echo "\t<li>$element</li>\n";
}
echo "</ul>\n";
}
if (count($_POST['CINT']) > 0 ) {
echo "<h2>Your CINT picks are:</h2>\n";
echo "<ul>\n";
foreach ($_POST['CINT'] as $element2) {
echo "\t<li>$element2</li>\n";
}
echo "</ul>\n";
}
else {
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
echo __LINE__;
if(
count($_POST['CINT']) == 0 AND
count($_POST['CINS']) == 0
) {
echo "<h2>No classes</h2>\n";
echo "<p>You need to pick a class from BOTH CINT and CINS to be a well rounded student.</p>\n";
echo "CINT = " . count($_POST['CINT']) . " CINS = " . count($_POST['CINS']) . "<br />\n";
}
}
I think, you want this:
if (count($_POST['CINS']) > 0)
{
…
} // End of IF for CINS
if (count($_POST['CINT']) > 0 )
{
…
} // End of IF for CINT
if (count($_POST['CINS']) == 0 || count($_POST['CINT']) == 0 )
{
…
}
Perhaps parentheses need attention...
Your
if ((count($_POST['CINT'] == 0)) and (count($_POST['CINS'] == 0)))
might work better as
if ((count($_POST['CINT']) == 0) && (count($_POST['CINS']) == 0))
Give it shot - reply with outcome.
I asked a question yesterday which was solved and I can now output WHILE loops which 2 results on a line, rather than 1 on a line. This is the code I've got at the moment:
if(mysql_num_rows($result2) > 0) {
$count=0;
$day = 1;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo "<b>";
if ($day=='1') { echo "Sunday - ";}
else if ($day=='3') { echo "Monday - "; }
else if ($day=='5') { echo "Tuesday - "; }
else if ($day=='7') { echo "Wednesday - "; }
else if ($day=='9') { echo "Thursday - "; }
else if ($day=='11') { echo "Friday - "; }
else if ($day=='13') { echo "Saturday - "; }
else { echo "";}
if (($row["open"] == 0) && ($row["close"] == 0))
{
echo "closed"
}
else
{
echo "</b>" . $row["open"] . "-" . $row["close"] . " ";
if ($count % 2 !=0 ){ echo "<br/><br/>";}
}
$count++;
$day++;
}
} else {
echo "Error";
}
With this code, when the branch is closed, then it returns the word 'closed' twice, where I need it to appear only once. Is this possible?
Just test on modulo the value within $day :
in place of :
if (($row["open"] == 0) && ($row["close"] == 0)) {
echo "closed";
}
do :
if ($row["open"] == 0 && $row["close"] == 0) {
if ($day % 2 == 1) {
echo "closed";
}
}
It may not be the most elegant solution but I would probably have a variable $closed = false, which gets set to true if the branch is closed. Then just test for $closed, if it's false, output "closed".
Use a for loop:
for($i=0`;`$i`<`count(row)`;`$i++)
{
}
I have this script that displays a max of 5 images for each row, but for some reason my <ul> tag won't close correctly if the number of items isn't an exact multiple of 5. How can I correct this problem so the <ul> tag will close even if the number of listed images is less then 5?
Here is my PHP code.
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
echo "</ul>";
}
$row_count++;
}
}
below the loop, check if
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
echo "</ul>";
}
$row_count++;
}
if ( (($row_count % 5) > 0) && (($row_count % 5) < 4))
echo "</ul>";
}
$multiple = false;
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
$multiple = true;
echo "</ul>";
} else {
$multiple = false;
}
$row_count++;
}
if($multiple == false) {
echo "</ul>";
}
}
if (!$dbc) {
print mysqli_error($mysqli);} else {
$row_count = 0;
//tank start
$total_rows = mysqli_num_rows($dbc);
//tank end
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
//tank start
if($row_count % 5 == 4 || $row_count==$total_rows) {
//tank end
echo "</ul>";
}
$row_count++;
}
Here's my updated solution. I think it looks a little clearer. I do setup a few variables to get rid of some IF statements, and replace them with FOR loops. Mainly for readability and it's the way i thought of doing it.
$itemsperrow = 5;
$items = mysqli_num_rows($dbc);
$rows = $items / $itemsperrow;
$itemcount = 0;
if (!$dbc)
{
echo(mysqli_error($mysqli));
}
else
{
for ($int = 0; $int < $rows; $int++)
{
echo "<ul>";
for ($item = 0; $item < $itemsperrow; $item++)
{
if ($itemcount >= $items)
{
echo "</ul>";
exit;
}
else
{
$row = mysqli_fetch_array($dbc);
echo "<li><a href='". $row["url"] . "'>";
echo "<img src='" . $row["src"] . "' title='" . $row["title"] . "'/></a></li>";
$itemcount++;
}
}
echo "</ul>";
}
}