PHP Variable Displaying - Not sure why? [closed] - php

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.

Related

PHP Reading the URL [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
How do I make the following url to be read:
localhost/index.php?errormsg=wrongpassword
so I want it to set the variable $errormsg to 'wrongpassword' how do I do that? I am pretty sure there is a way using $_GET or $_POST?
You want to use $_GET['errormsg'].
Something like this should do:
if isset($_GET['errormsg'])
{
echo $_GET['errormsg'];
}
$errormsg = $_GET['errormsg']
This should do the trick for you
$_GET['errormsg'] is the answer of your question.
print_r($_GET)
to see all variables in the URL. in this case you want
$_GET['errormsg']
You were as close as ever. A single google would've yielded the answer to you :)
http://php.net/manual/en/reserved.variables.get.php
<?php
// use htmlspecialchars() to clean up url-encoded characters
$error_message = htmlspecialchars($_GET["errormsg"]);
echo 'Error: ' . $error_message;
?>
prints: "Error: error_here", if url = ...?errormsg=error_here

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');

PHP: Match corresponding file names [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
This one may be a bit of a doozy.
I have 2 sets of files in separate folders:
SET 1:
celeb1.png
celeb2.png
celeb3.png
celeb4.png
celeb5.png
etc
The user selects an image (radio) and I then set this as a get variable on the url:
Now, I want to match another file.
In another folder, I have a set like this:
SET 2:
celeb1-24_59_250_300.png
celeb2-35_67_200_250.png
celeb3-54_87_300_400.png
celeb4-88_98_250_350.png
celeb5-87_43_300_400.png
etc
I want to use the GET variable (i.e.) celeb1.png, and then select the full filename of the corresponding file, based on the first 5 characters.
In other words if $_GET['celebimg'] is 'celeb1.png', I want to set a second variable (let's say $overlay) to string 'celeb1-24_59_250_300.png'
Any idea on how to achieve that?
I could obviously do a switch, but that means I would have to update the code every time a new celeb image is uploaded.
Is there any dynamic way to achieve this?
Thanks
JG
You can try something like this. Since we haven't seen what you have tried so far. This is just an illustration. However the code has been tested.
<?php
$file='celeb3.png'; // Here is where you will get the param $_GET['set1']
$set2=array(
'celeb1-24_59_250_300.png',
'celeb2-35_67_200_250.png',
'celeb3-54_87_300_400.png',
'celeb4-88_98_250_350.png',
'celeb5-87_43_300_400.png'
);
$set1 = explode('.',$file);
//echo $set1[0];//celeb3
foreach($set2 as $val)
{
if($set1[0]==substr($val,0,strlen($set1[0])))
{
echo $val;//celeb3-54_87_300_400.png
break;
}
}
OUTPUT : celeb3-54_87_300_400.png
Use glob to search a folder
//Get the GET variable
$cimg = $_GET['celebimg'];
//split it so we can just get the name without extention
$nameparts = explode(".",$cimg);
//glob will try to find matches to the string passed
$matches = glob("/dir/path/{$nameparts[0]}-*.png");
$matches will be an array of matched files
you could use other file functions like scandir or readdir etc but those do not filter out the files so you would have to do it.

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

IF equals something then show output 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
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)))

Categories