IF equals something then show output in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Ok I'm not a PHP editor by default I can do a bit but I am struggling witht his simple variable. I want to display something if the value isnt set to null. The output is run by a function i think, its a catgory for a listing so it would say something like:
WHATS ON: Music, Dance, Trance
Now if there isnt a category set it just says 'WHATS ON:' where as I dont want anything to show if there isnt a category set. Heres the output to display the 'WHATS ON' and category string:
$featured_event .= "<p class=\"complementaryInfo\"><b style=\"color:#74c1df\">WHATS ON:</b>".system_itemRelatedCategories($event->getNumber("id"), "event", true)."</p>";
and heres my variable attempt:
if (system_itemRelatedCategories($event->getNumber("id"), "event", true) == "") {
$featured_event .= "<p class=\"complementaryInfo\"><b style=\"color:#74c1df\">WHATS ON:</b>".system_itemRelatedCategories($event->getNumber("id"), "event", true)."</p>";
}
If you need any more info please ask, I hope this is enough to give you an idea of my problem.
Thanks

Your theory is almost correct. The only issue is that you are telling it to show "What's on" ONLY if there is nothing on.
Change your == to != and it should work fine. Or just drop the == "" entirely since any non-empty string will be truthy.
Personally, I'd further optimise it to:
if( $events = system_itemRelatedCategories($event->getNumber("id"),"event",true))
$featured_event .= "<p class=\"...\"><b...>WHATS ON:</b> ".$events."</p>";
This avoids calling the same function twice.

Use empty() function - if variable is not set or is empty then it returns true
if (!empty(system_itemRelatedCategories($event->getNumber("id"), "event", true)))

Related

PHP Code Session Not Working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My PHP code dosnt seem to be working and I dont Know Why when Ever I Use this code it will Make The whole webpage appear white.I think the problem is Here Somewhereecho Hello, (.$_SESSION['username'], ENT_QUOTES, 'UTF-8');Thanks In Advance
session_start();
if(!isset($_SESSION['user']) && empty($_SESSION['user'])) {
echo '<b>Log In</b>';
}
else {
echo Hello, (.$_SESSION['username'], ENT_QUOTES, 'UTF-8');
echo '</br></b>';
echo '<b>Log Out</b>';
}
When your page goes white, it usually means you have a fatal error in your code and need to check your logs, or turn on error_reporting.
In this case you're missing quotes, have the concatenation a bit messed up, and appear to be missing a function call (probably htmlspecialchars).
Also, you're checking $_SESSION['user'] a few lines before in your code, are you sure you don't mean to echo that here instead of $_SESSION['username']?
I think you want to change that line to:
echo "Hello, " . htmlspecialchars($_SESSION['user'], ENT_QUOTES, 'UTF-8');

PHP Variable Displaying - Not sure why? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Ok got the code below working but it keeps displaying the q value at the top of each page. what do I need to change to stop this happening. I can see the echo value is that what the problem is if so what should I change it too in order to prevent value displaying? Many thanks.
// capture referral url
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'google.' ) )
{
parse_str( $referringPage['query'], $queryVars );
echo $queryVars['q']; // This is the search term used
}
// general form data insert
$sql="INSERT INTO refer_kws (kwid, keyword, kwdate)
VALUES('','".$queryVars['q']."',now())";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "";
mysql_close($con)
remove this line:
echo $queryVars['q']; // This is the search term used
or turn it off by adding double slash at the beginning of the line like this:
// echo $queryVars['q']; // This is the search term used
First confirm that, do you want to display value or not. It seems that you have some misunderstanding regarding value insertion. you do not require to display value if you want to insert them in a database. You can remove whole line or make use of PHP comments 1. add // for one line comment 2. add /* at the starting of line and add */ at the end of line for multi line comments.

Dynamically replacing # and # like Twitter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my Laravel-based site, I need to find # and # within text content and replace it with a URL, such as a URL pointing at a user's Twitter page. How can I:
reliably find these strings within text portions of the HTML
replace found instances with a URL
The code for it is vast. You will have to use ajax here, in the textarea/textbox you will have to use "onkeyup" event, every key pressed have to be compared with "#" or "#" then the next character right after "#" has to be searched in the database.
So lets saw the user has typed "#A" till now and the user aims to type "#Ankur" Then as soon as "A" is typed the ajax script will start searching for users in the database and it is retrieved with the name, url and you just have to echo it on the screen.
THis is what you are looking for.. https://stackoverflow.com/a/4277114/829533
$strTweet = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1#\2', $strTweet);
And https://stackoverflow.com/a/4766219/829533
$input = preg_replace('/(?<=^|\s)#([a-z0-9_]+)/i', '#$1', $input);
Using regex it's rather simple. I would make one function that takes a prefix and replacement, like so.
function tweetReplaceObjects($input, $prefix, $replacement) {
return preg_replace("/$prefix([A-Za-z0-9_-])/", $replacment, $input);
}
An example usage would be something like this.
$text = 'hey look, it\'s #stackoverflow over there';
// expected output:
// hey look, it's stackoverflow over there
echo tweetReplaceObjects($text, '#', '$1');

explain me a preg_match if clause please [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
if(preg_match("/^[\w_.]+$/",stripslashes($_GET['key']))) {
$key = $wpdb->escape(stripslashes($_GET['key']));
}
assuming the key value is = be4e53680e6518cca701ec091258642f0740fe3d
can someone please explain me the if condition ? I`m confused on what exactly it checks for
ok thanks guys for the clarification on that.
now i`m posting one more line of code that ties with this one. if u can help me understand it.
if(preg_match("/^[\w_.]+$/",stripslashes($_GET['key']))) {
$key = $wpdb->escape(stripslashes($_GET['key']));
} else {
if(preg_match("/^[\w_.]+$/",$name)) {
$wpdb->query("some query;");
}
exit(0);
}
assuming $_GET['key'] = be4e53680e6518cca701ec091258642f0740fe3d
$name = TomJones
what i got so far is:
If $_GET['key'] is numeric then $key = stripslashes (get_key)
but when does the else kiks in ?
it looks for strings containing alphanumeric characters, underscores and dots in the key param from request, underscore is ommitable because \w handles it

Make a New Variable From String In PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this script in PHP like this:
echo "The results is $res";
And now, I want make a new variable to save that script. What can I do?
I do this because the value of $res is always changing depending on the input entered from user.
If I've understood you correctly, you want to save the script into a variable (let's call it $script), so that when $res="foo", the result of echo $script; is "The result is foo" and when $res="bar", echo $script; returns "The result is bar".
I don't think you can do this using a normal variable, but you certainly could do it as a function:
//The function
function get_res($res){
return "The result is $res";
}
//The function call
$res = "foo";
print get_res($res);
$res = "bar";
print get_res($res);
This would output "The result is foo" the first time the function is called, and "The result is bar" the second time.
$res = 'some resource';
$echo = 'The result is ' . $res;
echo $echo;
Is that what you want? If not, please clarify / simplify your question.

Categories