Echo value from only assigned array value - php

Please check following codes and tell me how can i echo $error[] array when each error receives a value when my code conditions meet. or is there any short solution for that? i want to echo error message when conditions meet. whats the best short hand solution for that?
if (isset($_POST['submit_update'])) {
if (empty($_POST['old_pass']) || empty($_POST['new_pass'])) {
$error[] = '<div class="alert alert-danger">Error: Field can not be empty</div>';
}elseif ($user->check_old_admin_pass($_POST['old_pass'])==true) {
if($user->update_pass($_POST['new_pass'])==true){
$error[] = '<div class="alert alert-success">Success: New password is set</div>';
}else{
$error[] = '<div class="alert alert-danger">Error: Fail to update</div>';
}
}else{
$error[] = '<div class="alert alert-danger">Error: Wrong old password</div>';
}
}

You could just try something very simple like so:
<?php
$strError = '';
if(isset($error) && !empty($error)){
$strError .= '<div class="error_wrapper">';
$strError .= implode("\n", $errors);
$strError .= '</div>';
}
echo $strError;

Related

How may I write foreach for that sql query to show messages sender&receiver

I want to show messages as receiver on right and sender on left here's my query below:
<?php
$mesajsor=$db->prepare("SELECT * FROM mesaj where kullanici_gon=:send_id and kullanici_gel=:receiver_id
order by mesaj_zaman ASC");
$mesajsor->execute(array(
'send_id' => $_GET['kullanici_gon'],
'receiver_id' => $_GET['kullanici_gel']
));
$say=0;
while($mesajcek=$mesajsor->fetch(PDO::FETCH_ASSOC)) {
$say++;?>
How may I implement the foreach to that query?
foreach($messages as $message){
if($message->kullanici_gel == receiver_id){
<div class="right">$message</div>
}elseif($message->kullanici_gon == send_id){
<div class="left">$message</div>
}
}
You probably want to do this in two separate loops, like this:
echo '<div class="right">';
foreach($messages as $message){
if($message->kullanici_gel == $receiver_id) {
echo '<div class="message">$message</div>';
}
}
echo '</div><div class="left">';
foreach($messages as $message){
if($message->kullanici_gon == $send_id) {
echo '<div class="message">$message</div>';
}
}
echo '</div>';
To prevent writing code twice you could use functions. For instance:
function displayMessage($message)
{
echo '<div class="message">$message</div>';
}
echo '<div class="right">';
foreach($messages as $message){
if($message->kullanici_gel == $receiver_id) {
displayMessage($message);
}
}
echo '</div><div class="left">';
foreach($messages as $message){
if($message->kullanici_gon == $send_id) {
displayMessage($message);
}
}
echo '</div>';
Especially because displaying a message is probably more complicated than you let us believe.

PHP/HTML - Echo an error in html div that has another echo in php?

thanks for your help in advance, noob here.
So, i have this error div of a form.
<div><?php echo $error; ?></div>
I'm trying to validate an email with php, but i want its error to change depending of the language selected by the user. I have another two php files with arrays with the translated text. What i'm tryng to do is to echo the error that contains the echo for the array variable so it can be interpreted as multilingual text in the html. I know that you can't echo an echo, but i need a workaround for this. Here is the php part.
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= '<?php echo $lang["EMAIL_INVÁLIDO"]; ?>';
};
if ($error != "") {
$error = '<p>' . $error . '</p>';
};
};
?>
Thanks!
instead of direct echo, try assigning it to the variable and then echo the variable.
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= $lang["EMAIL_INVÁLIDO"];
};
if ($error != "") {
$error = '<p>' . $error . '</p>';
};
};
?>
The following should suffice:
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= $lang["EMAIL_INVÁLIDO"];
};
You do not need to put quotation marks around variables(if you do they will be treated as text, not variables) and you were already working within <?PHP ?> tags.
instead just echo it later in the code:
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= $lang["EMAIL_INVÁLIDO"];
}
if ($error != "") {
echo '<p>' . $error . '</p>';
}
}
?>
As you say, you can´t echo an echo because PHP is server-side. You must put your echo after you now the final value of error:
<?php
$error = '';
if ($_POST) {
if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false) {
$error .= '$lang["EMAIL_INVÁLIDO"]';
};
if ($error != "") {
$error = '<p>' . $error . '</p>';
};
echo $error;
};
?>

How can i shorten this PHP code? (if - elseif)

I have this simple if else function and i wonder how i could shorten this?
<?php
if( $count_bananas == '1' )
{ echo '<div class="stage-columns-1">'; }
elseif ($count_bananas == '2')
{ echo '<div class="stage-columns-2">'; }
elseif ($count_bananas == '3')
{ echo '<div class="stage-columns-3">'; }
elseif ($count_bananas == '4')
{ echo '<div class="stage-columns-4">'; }
?>
The code works but i wonder if there is a way to code this shorter and more elegant?
You really didn't see the pattern?
echo '<div class="stage-columns-' . $count_bananas . '">';
It can be done without condition checks. For the code snippet you are posted, if the values are same then -
echo '<div class="stage-columns-' . $count_bananas . '">';

How to add variable in error message (php) multilanguage?

Need error message in two languages, and have some trouble with single quotes...
This is how it looks like:
else if(trim($FullName) == '')
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
And how when I put variable
else if(trim($FullName) == '')
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>';
}
So, when I put it like this, syntax becomes incorrect because single quotes..
did u try
else if(trim($FullName) == '')
$error = '<div class="errormsg">'. $lang["error"] .'</div>';
}
EDIT : echo is a php instruction that does not require you to re-open and re-close php tags (<?php and ?>). You can compute strings thanks to concatenation which is some basic stuff in PHP
I would use empty, i.e.:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">Please enter your Full Name!</div>';
}
the following, doesn't make sense since you're inside a php block:
$error = '<div class="errormsg"><?php echo $lang['error']; ?></div>
Change for:
else if(empty(trim($FullName)))
$error = '<div class="errormsg">'.$lang['error'].'</div>';
}

Display errors of a form in different places without setting a variable for every input field

I hope you understand my title.
Instead of displaying a list of all the errors with the submitted form, I would like to show them beside the fields in the form:
This is a short snippet of my code:
$errors = array();
if (empty($subject)) {
$errors[] = $lang['Empty Subject'];
}
if (empty($category)) {
$errors[] = $lang['Empty Category'];
}
if (strlen($ath) > 255) {
$errors[] = $lang['Too Long'];
}
if (!validate_string($str)) {
$errors[] = $lang['Invalid String'];
}
and the list goes on...
And for displaying:
if (!empty($errors)) {
foreach ($errors as $error)
$page['errors'] = array();
$page['errors'][] = '<li class="red"><span>'.$error.'</span></li>';
}
I know I could set a variable for every error but that does not seem like a smart idea like, this would work but it seems so stupid:
if (empty($subject)) {
$error_subject = $lang['Empty Subject'];
}
if (empty($category)) {
$error_category = $lang['Empty Category'];
}
and then beside every field:
Subject:
<input type="text" name="subject"><?php echo isset($error_subject) ? '<span style="color: red">'.$error_subject.'</span>' : '';
Category:
<input type="text" name="category"><?php echo isset($error_category) ? '<span style="color: red">'.$error_category.'</span>' : '';
However that does only show 1 error at a time anyways.
How do pros do this?
create the inputs dynamically
$inputs=array('subject','category'); //etc
foreach ($inputs as $input){
<input type="text" name="{$input}"><?php echo isset(${$error_.$input} ? '<span style="color: red">'.${$error_.$input}.'</span>' : '';
}

Categories