Can anybody help me on how I can stop an heading displaying if there are no search results?
The app generates palettes based on flickr images, at the minute I have a few else statements for validation that if there are no results from the flickr API etc, then a message will appear, as shown:
// No results from Flickr
} else {
echo "<div class=\"errormsg\"> Uh oh... no results for that Hex. Why not try a keyword?</div>";
}
} else {
// Error with the Flickr API
echo "<div class=\"errormsg\"> Oh boy... there was an error with the API.</div>";
}
} else {
// No hex code entered
echo "<div class=\"errormsg\"> Hey, it looks like you forgot to enter a hex code - Try again!</div>";
}
If there are results it will display a heading (#welcomeDiv) stating
Displaying palettes for...
However, I only want this to display if there are results, I have tried:
else{
?>
<style type="text/css">#welcomeDiv{
display:none;
}</style>
<?php
}
welcomeDiv that I want hidden
<?php if (isset($_POST['submit'])) { ?>
<h2 id="welcomeDiv">Displaying palettes for <span class="hex">"<?php echo $_POST['hex'] ?>"</span></h2>
<div id="results">
<?php include 'assets/php/generate.php'; ?>
</div><!--/results-->
<?php } ?>
But this doesn't apply. Does anybody know why?
Here's a link to the project.
This edit could work.
// No results from Flickr
} else {
?>
<style type="text/css">#welcomeDiv{ display:none;}</style>
<?php
echo "<div class=\"errormsg\"> Uh oh... no results for that Hex. Why not try a keyword?</div>";
}
} else {
// Error with the Flickr API
echo "<div class=\"errormsg\"> Oh boy... there was an error with the API.</div>";
}
} else {
// No hex code entered
echo "<div class=\"errormsg\"> Hey, it looks like you forgot to enter a hex code - Try again!</div>";
}
I see you also have jquery referred in the link. You can use javascript to hide the heading.
// No results from Flickr
} else {
?>
<script>$('#welcomeDiv').hide();</script>
<?php
echo "<div class=\"errormsg\"> Uh oh... no results for that Hex. Why not try a keyword?</div>";
}
} else {
// Error with the Flickr API
echo "<div class=\"errormsg\"> Oh boy... there was an error with the API.</div>";
}
} else {
// No hex code entered
echo "<div class=\"errormsg\"> Hey, it looks like you forgot to enter a hex code - Try again!</div>";
}
Basically:
if ($has_results) {
echo ...
} else {
// do nothing
}
put an if() test on the relevant section, and DON'T echo something when you don't want it to be echoed.
Related
I would like to show different header images on a wordpress site using php. Although I found numerous subjects with the title of my question, none of them clearly explain how this is done.
The first one I tried just failed to work. Here is the code I added to functions.php. I know the file is executed since the text I added is displayed.
//12/28/2020 - START
define("HOME", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/nyc-e1608682430748.png");
define("AWARD", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/trophy-e1608681301876.jpg");
define("ERP", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/IntuitiveERP-e1608681626647.png");
define("EVENT", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/Calendar-scaled-e1608681638194.jpg");
function change_the_header($url_for_image) {
if (is_home())
$url_for_image = HOME;
else if (is_single())
$url_for_image = ERP;
else
$url_for_image = AWARD;
return $url_for_image;
}
add_filter('theme_mod_header_image', 'change_the_header');
//12/28/2020 - END
This one doesn't explain where the code goes nor how the function is_page works. I tried to fill in the blanks, but it failed in the end.
//12/28/2020 - START
if( is_page('About') ) echo HOME;
elseif( is_page('Events') ) echo EVENT;
elseif( is_page('Awards') ) echo AWARD;
else echo '-NONE-';
//12/28/2020 - END
With one answer by someone who knows nothing of Wordpress and no mention of which file is being edited, I didn't even attempt this.
With code that actually ran, I had the most hope here. However, there is no explanation given on how to determine what page you are on. There is no is_about function nor is there any information on what is required of one so I can create it. And once I have it, there is nothing to indicate where it goes. Also passing a parameter to get_header does nothing.
//12/28/2020 - START
//12/28/2020 - OLD
//get_header();
//12/28/2020 - NEW
if (is_home()) {
echo '* HOME *';
get_header();
//} else if (is_about()) {
// echo '# away #';
// get_header('About');
} else {
echo '# away #';
get_header('About');
}
wp_head();
//12/28/2020 - END
I figured it out: The id can be found in the body tag or in your browser when using wordpress to edit pages, it will be the post id.
in the following link the page id is 62:
johnm5.sgedu.site/wp-admin/post.php?post=62&action=edit
Here's my code:
//JPM - 12/28/2020 - START
define("ABOUT_ID", 6);
define("AWARD_ID", 64);
define("ERP_ID", 58);
define("EVENT_ID", 62);
define("ABOUT", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/nyc-e1608682430748.png");//68
define("AWARD", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/trophy-e1608681301876.jpg");//64
define("ERP", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/IntuitiveERP-e1608681626647.png");//58
define("EVENT", "http://johnm5.sgedu.site/wp-content/uploads/2020/12/Calendar-scaled-e1608681638194.jpg");//62
function change_the_header($url_for_image) {
if (is_page(ABOUT_ID)) {
$url_for_image = ABOUT;
} else if (is_page(AWARD_ID)) {
$url_for_image = AWARD;
} else if (is_page(ERP_ID)) {
$url_for_image = ERP;
} else if (is_page(EVENT_ID)) {
$url_for_image = EVENT;
}
else {
echo "<script>alert('- NONE -');</script>";
}
return $url_for_image;
}
add_filter('theme_mod_header_image', 'change_the_header');
I want to make a call of this php logic inside a html div but when passing it as a function the logic breaks since it does not send an error message in case of entering the pass wrong and its confirmation at the time of performing the password change.
<?php
require 'funcs/conexion.php';
require 'funcs/funcs.php';
$user_id = $mysqli->real_escape_string($_POST['user_id']);
$token = $mysqli->real_escape_string($_POST['token']);
$password = $mysqli->real_escape_string($_POST['password']);
$con_password = $mysqli->real_escape_string($_POST['con_password']);
if(validaPassword($password, $con_password))
{
$pass_hash = hashPassword($password);
if(cambiaPassword($pass_hash, $user_id, $token))
{
echo "Contraseña Modificada <br> <a href='index_alumnos.php' >Iniciar Sesion</a>";
} else {
echo "Error al modificar contraseña";
}
} else {
echo "Las contraseñas no coinciden <br> <a href='index_alumnos.php' >contacta a Academia</a>";
}
?>
If the echo happens before your actual div is drawn, the echo goes... right where it happens. Which isn't within your div.
One way of getting around this would be to put your error message into a variable and then deliver this variable into your div (whether it be through a return value, if it's a call, or some other means.)
Here's a simple example to illustrate this:
<?php
if(1 === 2) {
//great, 1 is 2
} else {
//oh no, an error
$someErrorLine = '1 is not 2';
} ?>
<h1>Hi</h1>
<div><?= $someErrorLine ?></div>
You could also check if the variable exists, something like if(isset($someErrorLine)) {} and echo the div with it, or put the div within your variable.
I am using already this code:
<?php
// [...]
ELSE IF(isset($_GET['msg'])){
echo "<div id='noticePanel' class='panel panel-success'><div class='panel-heading'>
<h2 class='panel-title'>Good job!</h2></div>
<div class='panel-body'><h4>".addslashes(strip_tags(trim(#$_GET[msg])))."</h4></div></div>";
}
//[...]
?>
Which is called from other pages if some operation goes well:
if ($result){
header("Location:index.php?msg=DB updated succesfully");
exit;
}
BUT as you may imagine, it is not clean. Working, but not the best.
Can anybody tell me if I should move it to a POST request or if this code is "widely" used to exchange status messages between pages?
And, in case of the POST, can someone write down some code or redirect me to a page where this is explained well? I searched already of course, I found THIS from mozilla, THIS with jquery and in general searching "send post php header" on google.
But I just can't understand how to do it.
Thanks a lot guys!
Because you prefer to keep your code pure
i just suggest you to do not send the message in GET/POST
just send the status like
index.php?status=success
then check the status in result file
if(isset($_GET['status']) AND $_GET['status'] == success){
echo "bla bla";
}elseif(isset($_GET['status']) AND $_GET['status'] == fail){
echo "bla bla";
}else{
//do something
}
EDIT
If you have many messages, then you can create array
$message = [
'success' => 'success message here',
'fail' => 'failmessage here',
'notFound' => 'not found message'
];
$status = ( isset($_GET['status']) AND
isset($message[$_GET['status']]) )?$_GET['status']:'notFound';
echo $message[$status];
I think i'm going to go with this: PHP. How to pass variable and auto redirect to another PHP file
<?php
session_start();
$id = $user_profile['id'];
$_SESSION['id'] = $id;
header('Location: checkIfExsists.php');
?>
And on the checkIfExsists.php page, retrieve the variable so:
<?php
session_start();
$id = $_SESSION['id'];
?>
New code
session_start()
// [...]
ELSE IF(isset($_GET['msg'])){
echo "<div id='noticePanel' class='panel panel-success'><div class='panel-heading'>
<h2 class='panel-title'>Good job!</h2></div>
<div class='panel-body'><h4>".addslashes(strip_tags(trim(#$_SESSION['msg'])))."</h4></div></div>";
}
And..
session_start();
if ($result){
$_SESSION['msg'] = "DB updated!";
header("Location:index.php?msg");
exit;
}
What's wrong with this preg_match() usage? I want to check steam lobby link and if it's matching then write to database. If not, just echo the error. I am doing this through ajax. Is it better to do this with ajax or $_SERVER["REQUEST_METHOD"] == "POST"?
<?php
require("../includes/config.php");
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^((steam?:)+(/joinlobby\/730\/)+([0-9]{17,25}\/.?)+([0-9]{17,25})/$)%i", $lobby)) {
echo "Lobby link isn't formatted correctly.";
}
else {
$rank = "Golden";
$mic = "No";
try {
$stmt=$db->prepare("INSERT INTO created_lobby (lobby_link, current_rank, have_mic) VALUES (:lobby_link, '$rank', '$mic')");
$stmt->execute(array(
':input_link' => $_POST['lobbyLink']
));
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
?>
My Problem:
When I execute this code, it will give me false.
Thank you for help.
This works:
$lobby = "steam://joinlobby/730/109775243427128868/76561198254260308";
if (!preg_match("%^(steam?:)+(//joinlobby/730/)+([0-9]{17,25}/.?)+([0-9]{17,25}$)%i", $lobby)) {
echo "Lobby link isn't formatted correctly.";
}
I changed /joinlobby to //joinlobby, and remove the / at the end. I also removed the unnecessary () around everything.
I suspect you also shouldn't have (...)+ around steam?: and //joinlobby/730/. They'll cause repeated uses of those prefixes to be accepted as correct, e.g. steam:steam:...
I got an ajax submitting form source code, and learning it right, I found the return errors are all in a big square. I want to separate errors to where they belong to.
then I can simply add something like following code
<?php if($_session('errorarray'): ?>
<span class="errorclass"><?php echo $errorarray['phone']; ?></span>
<?php endif; ?>
here is validation php code, (I have 5 items need to be checked)
$error = array();
if(!check('name'))
$error[]='too short!';
else if(validate_name($_POST['name']))
$error[]='letters please!';
..........
if(checkphone($_POST['phone'])){
$error[]="Please enter a valid phone number";
}
here is the code return to $_Session
if(count($error))
{
if($_POST['ajax'])
{
echo '-1';
}
else if($_SERVER['HTTP_REFERER'])
{
**$_SESSION['errorarray'] = array ($error);** // possible?
$_SESSION['post']=$_POST;
header('Location: '.$_SERVER['HTTP_REFERER']);
}
exit;
}
Sorry I am very new to php. Hope my expression is not too hard to understand.
Many thanks in advance.
You can just set $_SESSION['errorarray'] = $error; Then in your view, you can do something like <span class="errorclass"><?php foreach ($_SESSION['errorarray'] as $err) { echo $err; } ?></span>
Sorry, your question is a bit unclear, if you provide more details we could probably give you a better answer.