Php Session issue? - php

I have stored an error message in session variable and it gets displayed on page when condition becomes false. Now, i want that on refreshing the page the message disappear thus , freeing up the Session Variable.
Code : To show the error message
<?php
if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php endif; ?>
Storing the error
$_SESSION['er'] = array("Salary Must be between 10000 and 80000.");
header("Location:addSalary.php");

Just unset it right after you printed error. So it will be shown only once
<?php if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php unset($_SESSION['er']); ?>
<?php endif; ?>

Then once you have used/sent the error message you can remove it.
<?php
if (isset($_SESSION['er'])): ?>
echo '<div class="form-errors">';
foreach ($_SESSION['er'] as $mistake):
echo "<p>$mistake</p>";
endforeach;
echo '</div>';
unset($_SESSION['er']);
endif;
?>

Related

Display MySQL request in PHP

I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help
At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.
When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>

Hide / show the part of code using php, if the value contains the letters

I have a problem with this scenario. There is a certain field (textarea) - $ COMMENTS. If the information in the $COMMENTS starts with a number (the first is a number and then any letter) - the script works fine (hide / show "some HTML and PHP code"), but if the $COMMENTS starts with the letter - the code does not work at all. Please tell me what is my mistake. I suffer already the 5th day.
Here is the script code, unfortunately it did not work correctly:
<?php $comments=$hm->Zb('rs:def:comments');
if ($comments!=0): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
also didn't work like this:
<?php $comments=$hm->Zb('rs:def:comments');
if ($comments!=""): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
Thank you all in advance for your help.
use isset() with if()
<?php $comments=$hm->Zb('rs:def:comments');
if (isset($comments)): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>
EDIT
Please try below code.
<?php $comments=$hm->Zb('rs:def:comments');
echo $comments; //for debugging to ensure $comments have value.
if ($comments){ ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php } else { ?>
<div class="sorry">Sorry, there is no comments</div>
<?php } ?>
http://php.net/manual/en/function.isset.php
try it : empty()
<?php $comments=$hm->Zb('rs:def:comments');
if (!empty($comments)): ?>
<div class="txt"><?php echo $hm->Zb('rs:def:comments'); ?></div>
<?php else : ?>
<div class="sorry">Sorry, there is no comments</div>
<?php endif; ?>

PHP - unset function unsets my var before my code

I would like to clear my session vqriqble "flash' using the unset function but now I can't print my flash message because the unset function is called before my previous code.
It is called in my header template : header.php wich is called in every page at the beginning of the code.
This is the code :
<?php if(isset($_SESSION['flash'])): ?>
<?php foreach($_SESSION['flash'] as $type => $message): ?>
<div class="alert alert-<?= $type; ?> bk-fullwidth-alert">
<p><?= $message; ?></p>
</div>
<?php endforeach; ?>
<?php unset($_SESSION['flash']); ?>
<?php endif; ?>
Thank you guys for your help !
Try this. The problem is that unset() was inside the loop, so the variable was being unset-ed on the first loop execution.
<?php if(isset($_SESSION['flash'])): ?>
<?php foreach($_SESSION['flash'] as $type => $message): ?>
<div class="alert alert-<?= $type; ?> bk-fullwidth-alert">
<p><?= $message; ?></p>
</div>
<?php endforeach; ?>
<?php unset($_SESSION['flash']); ?>
<?php endif; ?>
Edit: try this one
<?php
if(isset($_SESSION['flash'])) {
$flash = $_SESSION['flash'];
unset($_SESSION['flash']);
foreach($flash as $type => $message) {
?>
<div class="alert alert-<?=$type?> bk-fullwidth-alert">
<p><?=$message?></p>
</div>
<?php
}
}
?>

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

php multiple if's but not related

I have a WP site but this is a PHP question, I have the following code:
f(!empty($c_dates1_new)) { ?>
<p class="text">Dates: From <?php echo $c_dates1_new; ?> to <?php echo $c_datee1_new; ?></p>
<?php } if(!empty($c_dates2_new)) { ?>
<p class="text">Dates 2: From <?php echo $c_dates2_new; ?> to <?php echo $c_datee2_new; ?></p>
<?php } if(!empty($c_dates3_new)) { ?>
<p class="text">Dates 3: From <?php echo $c_dates3_new; ?> to <?php echo $c_datee3_new; ?></p>
<?php } if(!empty($c_dates4_new)) { ?>
<p class="text">Dates 4: From <?php echo $c_dates4_new; ?> to <?php echo $c_datee4_new; ?></p>
<?php }
Now this only shows the content from $c_dates_1 where it should display the content from all of them.
I cannot workout what I need to put between them e.g. it's not an else, it's not an OR and it's not an AND as they are all separate checks, not related to each other.
any input is appreciated

Categories