Separating single grouped WHILE loop results - php

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++)
{
}

Related

Count the instances where a variable equals TRUE

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

PHP rank and choice actions

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";
}
}

ELSE statement acting funny

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.

php mysql_fetch_array every 25 items wrap a div

I want to mysql_fetch_array 100 items from mysql database. then every 25 items wrap a div.
also, I make a total items result check.
My code as below, but it will add <div> to every item, no as my require. So how to make it easier? Thanks.
if (mysql_num_rows($result) != 0) {
$total = mysql_num_rows($result);
$num = 1;
while ($row = mysql_fetch_array($result)) {
if ($num === 1) {
echo '<div>';
}
echo $row['title'] . '<br />';
if ($total < 26) {
echo '</div>';
}
else {
if ($num === 26) {
echo '<div>';
}
if ($total < 51) {
echo '</div>';
}
else {
if ($num === 51) {
echo '<div>';
}
if ($total < 76) {
echo '</div>';
}
else {
if ($num === 75) {
echo '<div>';
}
if ($total < 101) {
echo '</div>';
}
}
}
}
$num++;
}
}
You can use the mod operator.
See: http://php.net/manual/en/internals2.opcodes.mod.php
echo '<div>';
while($row = mysql_fetch_array($result)){
....
if (($num % 25) === 1) { echo '</div><div>' }
$num++;
}
echo '</div>';
Try This...
if(mysql_num_rows($result)!=0){
$total = mysql_num_rows($result);
$num=1;
while($row = mysql_fetch_array($result)){
if($num===1)
echo '<div>';
echo $row['title'].'<br />';
if($num==25){
echo '</div>';
$num=1;
}
$num++;
}
}
use the mod operator.
$num =1;
while($row = mysql_fetch_array($result)){
if (($num % 25) === 1) { echo '<div>' }
-------here data ----
if (($num % 25) === 0) { echo '</div>' }
$num++;
}
My advice is to separate your concerns... Give it a shot more like this:
// first get all your data.
$results = array();
while($row = mysql_fetch_array($result)){
$results[] = $row;
}
// now you have an array of all of your records.
if (!empty($results)) {
// total count of the array up front.
$total = count($results);
$interval = 0;
// save the data in a buffer for echoing later.
$buffer = array('<div>');
foreach($results as $row) {
$buffer[] = $row['title'] . '<br />';
if (++$interval === 24) { // notice the prefix ++
$interval = 0;
// close and re-open divs
$buffer[] = '</div><div>';
}
}
// one final close tag
$buffer[] = '</div>';
// now we zip it up and echo the content.
echo implode('', $buffer);
}
Here is how I do it...
$count = 0;
while($row = mysql_fetch_array($result)){
if ($count%$25 == 0)
{
echo "<div>";
}
//whatever your data goes here
count++
if ($count%$25 == 0)
{
echo "</div>";
}
}

PHP looping problem?

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>";
}
}

Categories