i am generating random links using array but unable to add some value as per array random selection.
this file is language file which has values for different language
if($c2[2]=='en' || $c2[3]=='en'){$htmllang='en';$alt='Download';$text1='Wallpapers';$text2='HOLLYWOOD';}
elseif($c2[2]=='af' || $c2[3]=='af'){$htmllang='af';$alt='Aflaai';$text1='Wallpapers';$text2='HOLLYWOOD';}
else($c2[2]=='am' || $c2[3]=='am'){$htmllang='am';$alt='አውርድ';$text1='የግድግዳ';$text2='የሆሊዉድ';}
else($c2[2]=='ar' || $c2[3]=='ar'){$htmllang='ar';$alt='تحميل';$text1='خلفيات';$text2='هوليوود';}
here is code which is generating random links
$extpo = array("af","sq","am","ar");
$random_keys=array_rand($extpo,3);
shuffle($extpo);
$randlink=''.$extpo[0].'';
$city2 is number .No issue here
it generates link like this
http://somesitesss.com/some/am/566556
i want it to include $alt values before no. like this
http://somesitesss.com/some/am/አውርድ/566556
any help will be great
I'm not totally clear what you want so I maybe wrong)
Here's the code I think it should be:
$extpo = array("af","sq","am","ar");
// I don't think that you need $random_keys if you `shuffle` array
//$random_keys = array_rand($extpo,3);
shuffle($extpo);
$lang_key = $extpo[0];
// Okey, now `$lang_key` holds your lang code:
// Now I use your block of `if`s but I compare `$lang_key`
// As I don't know what is `$c2`
if ($lang_key == 'en'){
$htmllang='en';$alt='Download';$text1='Wallpapers';$text2='HOLLYWOOD';
} elseif ($lang_key == 'af') {
$htmllang='af';$alt='Aflaai';$text1='Wallpapers';$text2='HOLLYWOOD';
} elseif ($lang_key == 'am') {
$htmllang='am';$alt='አውርድ';$text1='የግድግዳ';$text2='የሆሊዉድ';
} elseif ($lang_key =='ar') {
$htmllang='ar';$alt='تحميل';$text1='خلفيات';$text2='هوليوود';
}
// now your `$alt` has a proper value according to `$lang_key`
// get a link:
$randlink=''.$lang_key.'';
Related
I think my question is some stupid because the code work, I only want know if I can do it better hehe, I want get a text from a URL usin GET, but I want check if it is empty, check if the text have numbers, and if the text is not a option of two options required condition_text_1 and condition_text_2, so I need do it with conditionals (IF), I will show my code:
if (isset($_GET['TEXT'])&&!empty($_GET['TEXT'])) {
if (!preg_match("/^[A-Za-z]+$/", $_GET['TEXT'])) {
$url = "DEFAULT_TEXT";
}else{
if ($_GET['TEXT'] !== 'condition_text_1' && $_GET['TEXT'] !== 'condition_text_2') {
$url = "DEFAULT_TEXT";
}else{
$url = $_GET['TEXT'];
}
}
}else{
$url = "DEFAULT_TEXT";
}
I dont know if its explained correct, but Im not talk english and Im new on php so it is a explosive post. Advanced thanks!!
So I inherited an old site from another developer, and I'm not really a programmer so I'm having some trouble. I've put the code into a Fiddle: https://jsfiddle.net/s6coraf5/
Basically there are different categories of real estate properties and when you click on different pages it's supposed to filter them and only display the ones specific to whatever page you're on. The problem is that no matter what page you're on, it's just displaying everything. I've narrowed down some specific code but can't figure out why it isn't applying it right.
In the php there's:
$select_title = "Unknown";
if ($select_type == "all") { $select_title = "All Listings"; }
if ($select_type == "office") { $select_title = "Office"; }
if ($select_type == "industrial") { $select_title = "Industrial"; }
if ($select_type == "retail") { $select_title = "Retail"; }
if ($select_type == "shoppingcenter") { $select_title = "Shopping Center"; }
if ($select_type == "land") { $select_title = "Land"; }
if ($select_type == "agricultural") { $select_title = "Ranch / Farm"; }
if ($select_type == "investment") { $select_title = "Investment"; }
if ($select_type == "lodging") { $select_title = "Lodging"; }
if ($select_type == "sportsentertainment") { $select_title = "Sports / Entertainment"; }
In the HTML there are various places where those $select_type's are supposed to be applied:
a href="properties.php?select_type=<?php echo $select_type;?>&select_city=<?php echo $select_city;?>&priceForm=<?= $lowPrice;?>,<?= $highPrice; ?>&sqft=<?= $lowSize;?>,<?= $highSize; ?>&sort_type=city, asking_price desc"><font size=4><b>Location,</b></a>
it's only applying the "all" one though on every page. Again, the fiddle has the full php and html which is probably more helpful. I realize it's ugly and bad but maybe someone can see something obvious that I can't.
Thanks in advance for any help anyone can provide.
Based on the PHP code in the fiddle (Which really shouldn't be there since the fiddle is for Javascript), it seems like the problem is that you never use the select_type given in the URL.
Take a look at this line. This is the first time $select_type is used.
if (!isset($select_type)) $select_type = "all";
Thus, $select_type will always be all.
Instead you should either change it to:
if (!isset($select_type)) $select_type = $_GET['select_type'];
Or just add this line before it:
$select_type = $_GET['select_type'];
Your ssql query in your jsfiddle seems like it might be the culprit. I'll put it here to make it easier:
select properties.property_id,selected_subtypes.property_type,properties.listing_type,properties.city,properties.asking_price,memberships.name,properties.membership_id,properties.building_size,memberships.website,properties.sold_price
from selected_subtypes,properties,memberships where (selected_subtypes.property_id = properties.property_id)
and (properties.membership_id = memberships.membership_id)
and (memberships.status = 'Active') and (properties.sold_information = ' ' or properties.sold_information = 'Undisclosed')
and ((selected_subtypes.property_category ='".$select_type."' or '".$select_type."'='all')
or (selected_subtypes.property_type ='".$select_type."'))
and (properties.city = '".$select_city."' or '".$select_city."'='all')
and (properties.asking_price BETWEEN ".$lowPrice." and ".$highPrice.")
and (properties.building_size BETWEEN ".$lowSize." and ".$highSize.")
".$date_sql."
order by ".$sort_type
The query appears to be, in each line, be selecting $select_type OR 'all
This boolean approach will always bring back either of those, so it would bring back "all" every time.
If you want to bring back only the selected type, you'd need to eliminate the "all" within the OR in these rows.
The easiest way to handle this would be to set the value $select_type to be equal to "all" if that is what is selected, or else, the specific type. One way he way I do "all" queries is to set the value to be "1=1" which will always be true.
In other words, modify the query like so (for each of the selected types) to show this (I changed the OR to AND in this case)
AND selected_subtypes.property_type ='".$select_type."'
and then in the php modify the code to be something like this:
if (!isset($select_type)) {
$select_type = "1=1"
}
else {
$select_type = $_GET['select_type'];
}
Another thing to be aware of
This particular code is somewhat vulnerable to SQL injection, so you might want to modify the way that you query the database. I strongly suggest you look into prepared statements, either using mysqli or PDO
I have an option for the users to write which pages they don't want to see.
Simplifying, is a text input where they will write the pages names, such "contact", "bio" etc.
While they tip "contact, bio" I have to create a conditional to hide these pages, so the content would be displayed like this on the code:
if (('contact') || ('bio')) {
hide content
}
Typing just one page would be easy but with more pages, I don't know how to replace the comma to the OR conditional. How can I do it?
Try to use below code:
$skip_pages = explode(",","contact, bio");
$current_page = "contact";
if(in_array($current_page,$skip_pages)){
//redirect page to ....
}
//continue with page
You should first take user's input in one variable
Lets say you got it in $string
$string = "contact,bio,home";
$token = strtok($string, ",");
$i=0; // To know how manny keywords are there.
while ($token != false)
{
echo "$token<br>";
$user_ip[i++] = strtok(",");
}
and then you need to parse all pages name by , and store it in array.
lets say you stored it in $user_ip
Then you need to compare it like
if ( ($user_ip[0] === "contact") || ($user_ip[0] === "bio") {
}
and check above if for all members of $user_ip.
I am creating a web application that takes in a user input (Scientific Paper DOI) and queries a database to display a graph. I've been trying to limit the connections made to the database since its on a remote server (private DMZ with web server) by checking the user input if it matches a correct DOI.. if it doesn't then no connection to the database will be made, I hope this will help speed up the application if there are many users at once making queries.
Pseudo: All paper DOIs start with "10.1103/" because they are all physics papers. This part I have implemented correctly using substr. Next I want to check every character in the input to make sure it only consists of only these characters:
Letter
Number
"/"
"."
Example DOIs:
10.1103/RevModPhys.9.1
10.1103/RevModPhys.76.1015
10.1103/PhysRevLett.95.208304
Here is my code:
function checkDOI($doi) {
if (substr($doi, 0, 8) != "10.1103/") {
echo "Invalid DOI";
return false;
}
for ($n = 0; $n < strlen($doi)+1; $n++) {
if ( !ctype_alnum($doi[n]) && $doi[n] != "." && $doi[n] != "/") {
echo "Invalid DOI";
return false;
}
}
echo "Valid DOI";
return true;
}
if(isset($_POST['submit'])) {
$doi_input = $_POST['doi_input'];
checkDOI($doi_input);
}
I am working with PHP and javascript for the very first time, the pseudo is fairly simple but for some reason, there is something wrong with the 2nd if statement. Not sure if I can really do that. The Echos are just for tests.
Do you think doing this check for every input will slow down the application significantly? Is it worth it to limit the amount of connections to mysql?
The bottom of the code will be modified once I have this working to only query the database if checked returns true.
Thanks for the help!
to check every character in the input to make sure it only consists of only these characters
I suggest you to use preg_match.Try this:
$value="10.1103/RevModPhys.9.1";
if(preg_match("/^[a-zA-Z0-9\/.]+$/", $value)){
echo "match found";
}else{
echo "no match found";
}
Check here: Demo
For more info: preg_match
Your error is $doi[n], it is not an array and should it be the index is invalid.
So use a function like
$chars_doi = str_split($doi);
Before the loop to get an array of characters then use in your loop
$chars_doi[$n]
So you should have something like:
$chars_doi = str_split($doi);
$size = sizeof($chars_doi) - 1;
for ($n = 0; $n < $size; $n++) {
if (!ctype_alnum($chars_doi[$n]) && $chars_doi[$n] != "." && $chars_doi[$n] != "/") {
echo "Invalid DOI";
return false;
}
}
Little tip, avoid use functions such as strlen / sizeof as a loop argument or it will get called at each iteration. It is better for performance to store the value in a variable beforehand.
I would just do:
if (! preg_match ('#^10\.1103/[\p{L}\p{N}.-_]+$#', $doi)) {
... // bad
return;
}
// good
Reference:
http://www.php.net/manual/en/reference.pcre.pattern.syntax.php
http://php.net/manual/en/regexp.reference.unicode.php
I am in the outlining phase of constructing a Routing system for a PHP Framework I am building.
I will need to use mod rewrite, for pretty urls. I got that part covered.
But say I want to make a page with the url like:
www.domain.com/news/10(News-id)/
and I want this dynamic variable ( This news id ) to have a name when rewriting.
What I want to achieve is;
Frameworks routes to news controller, and passes 10 as argument as:
$args = array ( 'news_id' => 10 )
You can use the $_SERVER super-global to inspect the requested URI. In your example, $_SERVER['REQUEST_URI'] will be set to something like:
/news/10/
You can then get the requested news-id from that string.
Update
// Use substr to ignore first forward slash
$request = explode('/', substr($_SERVER['REQUEST_URI'], 1));
$count = count($request);
// At this point, $request[0] should == 'news'
if($count > 1 && intval($request[1])) {
// The second part of the request is an integer that is not 0
} else {
if( $count == 1) {
// This is a request for '/news'
// The second part of the request is either not an integer or is 0
} else if($request[1] == 'latest') {
// Show latest news
} else if($request[1] == 'oldest') {
// Show oldest news
} else if($request[1] == 'most-read') {
// Show most read news
}
}
See the manual entry for $_SERVER