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>";
}
?>
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>
Here is my code snippet:
for($x=0;$x<=$flaga1;$x++){
$index++;
echo "<div class=\"item\" data-slide-number=".$index.">".$sth."</div>";
}
It iterates the code depending on $sth. But I want to add active class to the first occurence of the code, so it will be:
<div class="active item" data-slide-number="0">"sth"</div>
<div class="item" data-slide-number="1">"sth"</div>
<div class="item" data-slide-number="2">"sth"</div>
I'm trying to do something like this, but it doesn't work:
for($x=0;$x<=$flaga1;$x++){
$active=0;
$divclass="<div class=\"item\"";
do {
$divclass="<div class=\"active item\"";
$active++;
} while ($active<=0);
$index++;
echo "".$divclass." data-slide-number=".$index.">".$sth."</div>";
}
As per code submitted by you. You can try this.
for($x=0;$x<=$flaga1;$x++){
$active = "";
if($x === 0 ) {
$active = "active";
}
$index++;
echo "<div class=\"item\ <?php echo $active; ?>" data-slide-number=".$index.">".$sth."</div>";
}
Or you can do this, as suggested by Niet the Dark Absol.
for($x=0;$x<=$flaga1;$x++){
$index++;
echo "<div class=\"item\ <?php if($x === 0) {echo "active"; } ?>" data-slide-number=".$index.">".$sth."</div>";
}
You can also set this up in your for loop definition if you like:
for ($x = 0, $a = 'active'; $x < $flaga1; $x++, $a = '') {
echo "<div class=\"item $a\" data-slide-number=". ++$index .">" . $sth . "</div>\n";
}
This is my pagination code:
<?php
while($pagenumber != $totalpages)
{
echo"<a href='?page=" . ++$pagenumber. "' >" . $pagenumber. "</a> ";
}
?>
The above code will look like this in normal html:
1
2
I want to add class="active" with this condition
If ($_GET['page'] == " . $pagenumber . ")
echo "class="active" ";
I do not know, how to insert if statement inside echo command.
`
Here you are:
<?php
while ($pagenumber != $totalpages) {
++$pagenumber;
$class = ($_GET['page'] == $pagenumber) ? 'active' : '';
echo "<a href='?page={$pagenumber}' class='{$class}'>{$pagenumber}</a>";
}
I think this might help:
if ($_GET['page'] == $pagenumber) echo "<a href='?page=$pagenumber' class='active'>$pagenumber</a>";
Please try:
<?php
while($pagenumber != $totalpages) {
$active = ($_GET['page'] == $pagenumber) ? ' class="active"' : '' ;
echo '<a href="?page='.++$pagenumber.'"'.$active.'>'.$pagenumber.'</a>';
}
?>
You will have a very clean HTML code without the class attribute empty on the non-active elements.
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
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;
}