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;
}
Related
I have a foreach statement in my app that echos a list of my database results:
<?php
foreach($featured_projects as $fp) {
echo '<div class="result">';
echo $fp['project_name'];
echo '</div>';
}
?>
I would like to:
On every third result, give the div a different class. How can I achieve this?
You can use a counter and the modulo/modulus operator as per below:
<?php
// control variable
$counter = 0;
foreach($featured_projects as $fp) {
// reset the variable
$class = '';
// on every third result, set the variable value
if(++$counter % 3 === 0) {
$class = ' third';
}
// your code with the variable that holds the desirable CSS class name
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
foreach ($featured_projects as $i => $fp) {
echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
If the $featured_projects array is based on incremental index you could simply use the index and the modulo % operator.
Otherwise you would have to add a counter.
http://php.net/manual/en/language.operators.arithmetic.php
add a counter in this loop and check if counter equals three and apply class.
Using a counter and modulo operator this is easy to implement
<?php
foreach($featured_projects as $fp) {
if(++$i % 3 === 0) {
$class = ' something';
} else {
$class = '';
}
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
$i = 0;
foreach($featured_projects as $fp) {
echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
echo $fp['project_name'];
echo '</div>';
}
?>
What leaves your code mostly in tact would be
<?php
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>
But you may want to consider using a for or while construct around "each($featured_projects)" (see http://php.net/manual/en/function.each.php) which may result in neater code.
<?php
$counter = 0;
foreach ($featured_projects as $fp) {
echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
You can add a counter in loop ...try the following...
<?php
$i = 0;
foreach($featured_projects as $fp) {
$i = ++$i;
if(($i%3) == 0)
{
$class1 = 'test1';
}
else
{
$class1 = 'test2';
}
echo '<div class="'.$class1.'">';
echo $fp['project_name'];
echo '</div>';
}
?>
This is the working version, sorry for my prev version:
<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";
$class[0] = "class1";
$class[1] = "class2";
$i=0;
foreach($featured_projects as $fp) {
$i++;
if ($i == 3) {
$c = $class[1];
$i=0;
} else {
$c = $class[0];
}
echo "<div class=\"$c\">";
echo $fp['project_name'];
echo "</div>\n";
}
?>
Produces:
<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>
I am new in the world of web development.
I am trying to redirect the user to a link based on the some conditions using IF statement.
but the inside the IF block href is not being recognized.
Any advise so as how can I accomplish this ?
Thanks
EDIT 1
<?php if ($len == 12){
track;
}
else {
track;
}
?>
Use concatenation. It sure makes things easier.
<?php
$url1 = 'track';
$url2 = 'track';
if ($len == 12) {
echo $url1;
} else {
echo $url2;
}
?>
try this:
<?php
if ($len == 12){
echo "<a href='http://www.dddd.in/" . $row_Recordset1['TRACKING_NO'] . "1213'> track </a>";
} else {
echo "<a href='http://www.aaa.in/" . $row_Recordset1['TRACKING_NO'] . "1231'> track </a>";
}
?>
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 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>";
}
}
Let's say I have a user that entered 12 links into the database but when they are displayed I want to break up the links into groups of 3 and repeat the groups of three until all 12 kinks are displayed for example.
<div>
<p>Link 1</p>
<p>Link 2</p>
<p>Link 3</p>
</div>
Here is part of the code I'm working with. How should my code look like?
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
echo '<div>';
echo '<p>' . $row['category'] . '</p>';
echo '</div>';
}
}
Something like this gets you three links per div. We add the extra conditional echo at the end for the case that there's not a multiple of 3 links
$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
if ($ctr%3 == 0) {
echo '<div>';
}
$ctr ++;
echo '<p>' . $row['category'] . '</p?';
if ($ctr%3 == 0) {
echo '</div>';
}
}
}
if ($ctr%3 != 0) {
echo '</div>';
}
if you want to avoid incrementing/resetting i every time you reach three, you can just use the modulus operator:
http://php.net/manual/en/language.operators.arithmetic.php
Something like:
if( $i%3 == 0 ){
// divisible by 3...
}
An easier method would be more like:
while(true) {
$c = 0;
echo "<div>";
while ($row = mysqli_fetch_assoc($dbc) && $c++ < 3) {
if (!empty($row['category']) && !empty($row['url'])) {
echo '<p>' . $row['category'] . '</p>';
}
}
echo "</div>";
if(empty($row))
break;
}