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.
Related
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 have a page on my site that shows a member directory. I want the members to be listed 3 per row, before it goes to the next row. The code I have, does this for the very first row - but on the second row, its all on one line and doesnt carry down.
The profiles are showing up like this:
uuu
uuuuuuuuuuuuuuuuuuuuuuuuuuu
When they should be doing this:
uuu
uuu
uuu
uuu
This is what my code looks like:
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td></tr>
<tr>
<td>
<?php
while($row = mysql_fetch_array($result)) {
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td><td>';
if ($i++ == 2) echo '</td></tr><tr><td>';
}
?>
</td>
</tr>
</table>
Any help would be greatly appreciated, thanks!
Use
if (++$i % 3 == 0) echo '</td></tr><tr><td>';
Explanation:
First of all ++$i first increments $i, and then uses it in whatever is next, this makes for more readable code.
Second, the % is the modulus, which means it sortof subtracts 3 from $i until it is not possible anymore. E.g. 9 % 3 == 0, and 11 % 3 == 2 and so on. This means we know that we have printed 3 rows whenever $i % 3 equals 0.
try this one
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td>
</tr>
<?php
$count=0;
while($row = mysql_fetch_array($result))
{
$count+=1;
if($count%3==1)
{
echo '<tr>';
}
echo '<td>';
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td>';
echo '<td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td>';
if($count%3==0)
{
echo '</tr>';
}
}
if($count%3!=0)
{
echo '</tr>';
}
?>
</table>
You didn't reset the value of $i, so it kept increasing; another issue is that if you have only seven items, the last row should have four empty cells. So the loop condition needs to be augmented with a row completion status:
$i = 0;
while (($row = mysql_fetch_array($result)) !== false || $i != 0) {
if ($i == 0) {
echo '<tr>'; // start of new row
}
if ($row !== false) {
echo '<td>';
if (empty($row['profile'])) {
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'];
echo '</td>';
} else {
echo '<td></td><td></td>'; // no more data
}
$i = ($i + 1) % 3; // advance
if ($i == 0) {
echo '</tr>'; // end of the row
}
}
I have written some script that works great, BUT I have tried to add an elseif statement in which appears to do absolutely nothing, can't figure out where I am going wrong. (Yes I am a PHP newbie, but I am trying! :)
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
}
Everything up the elseif statement works perfectly.
Must be something simple I am missing or misunderstanding!
$i will never be 0 at this location.
You increase it in the if-statement before the test (++$i).
Note that elseif and else if will only be considered exactly the same when using curly brackets.
if (++$i == 5) break;
elseif ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
Won't work, change it to:
if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
elseif (++$i == 5) {
break;
}
elseif vs. else if
I see a lot of people discussing this in the thread.
These are both correct and will both work.
The difference being :
else if() {
}
Translates to:
else {
if(){
}
}
And can't be used in code blocks using colons like this:
if:
stuff
else if()://won't work
//stuff
endif;
basic syntax error ? you can't use the non braced short if's when doing an else if
if (++$i == 5) {
break;
} else if ($i == 0) {
echo '<img src="img/no-vac.png"/>';
}
notice the colour highlighting on this block compared to yours
Here is the working outcome, thanks for all your help!
$i = 0;
foreach($arrJobs as $job)
{
echo "<div class=\"job-ind\">\n";
echo '<p>' . $job->title . ', ' . $job->location . "</p>\n";
echo '<p class="smaller">Reference: ' . $job->reference . "</p>\n";
echo '<p><strong>' . $job->salary . "</strong></p>\n";
echo "</div>\n";
}
if ($i = 0) {
echo "<div class=\"job-ind\">\n";
echo '<img src="img/no-vac.png"/>';
echo "</div>\n";
}
else if (++$i == 5) {
break;
}
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>";
}
}