Replace comma with OR conditional (PHP) - php

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.

Related

Is correct use this count of IF?

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!!

generating random php link with if else prefix

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

How can I check the post slug?

Scenario: As you know, StackOverflow checks the title in the question. I mean when you open this URL:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function
automatically it will be replaced with this:
http://stackoverflow.com/questions/38839016/should-i-store-the-result-of-an-function-into-an-array
That replacement is because of being incomplete the first URL.
Ok well, I'm trying to make such a system by PHP. Here is my attempt:
// getting this from the URL
$id = '38839016';
// fetching this from database based on that id (38839016)
$real_title = $result['title'];
//=> Should I store the result of an function into an array
$real_title = str_replace("-"," ",strtolower($real_title));
//=> should-i-store-the-result-of-an-function-into-an-array
// getting whole uri
$current_uri = $_SERVER["REQUEST_URI"];
//=> /questions/38839016/should-i-store-the-result-of-an-function
What I want to do: I need to compare $real_title with title-part of the $current_uri. How can I determine "title-part"? It is everything which is after $id.'/' until / or ? or $ (end of string). How can I do that comparison?
And then:
if ( preg_match("//", $current_uri) ) {
// all fine. continue loading the page
continue;
} else {
// replace title-part of the $current_uri with $real_title
preg_replace("//", $real_title, $current_uri);
// redirect to this post with correct slug
header('Location: '.$_SERVER["HTTP_HOST"].);
}
briefly, I want to complete these:
if ( preg_match("//", $current_uri) ) {
preg_replace("//", $real_title, $current_uri);
Ok, in simple words, there is a good url and a requested url
if the requested url not equal the good url
redirect the visitor to the good one
<?
$id = '38839016';
$good_url = '/questions/'.$id.'/'.str_replace("-"," ",strtolower($result['title']));
preg_match( '/\/[0-9]+\/([a-z0-9-]+)\/.*/', $_SERVER[REQUEST_URI], $matches);
if ($matches[1] != str_replace("-"," ",strtolower($result['title'])))
header('Location: '.$good_url);

Efficient way to make a (PHP) redirection functionality for multiple (128) database values

Currently I am working on a webpage where certain data from a database is being fetched and displayed to the user. This information is about certain project information and the lead partners of those projects. I want to implement a functionality where the user can click on a lead partner (link) and the link will redirect this user to another page (where the database information of all the organisations is on) BUT with a predefined search for only the organisations that are equal to the clicked lead partner link.
I found a solution on how to do this but the problem is that the solution (which i describe further below) isn't that good/efficient for multiple (128) organisations.. So the question is, do you know a better/more efficient solution to achieve this. To answer this question you probably also need some background information:
BACKGROUND INFORMATION
In the outputted database information (which is done in a table) there are several information columns/titles such as:
Project name
Organisations involved
Leader partner
Project website
And so on...
The fetching of the data is being done with a simple query where certain database columns have a specific identifier. For example, the project website columns is obviously a link, so in the query it is being done as follows: $SomeQueryVar = ("SELECT project_website as LINK FROM xxxx WHERE project_website ('$some_website') "); -- Just a short example to clarify things.
For the displaying, the data is being 'catched' like so:
if(count($SomeQueryVar)>0){
for($i=0;$i<count($SomeQueryVar);$i++){
echo "<tr>";
foreach($SomeQueryVar[$i] as $key=>$value){
echo "<td>";
$b=unserialize($value);
if($key =='LINK' && $value != NULL){
$first = true;
array_filter($b);
foreach($b as $y){
echo ''."Project website".'';
$first = false;
if(!$first) break;
}
} else {
echo $value;
}
echo "</td>";
}
echo "</tr>";
}
}
As you can see in the above code, certain database columns need other displaying as the rest. For example, links must be clickable instead of just being plain text. These 'exceptions' are being catched with the if $key ==, for the data that just needs regular displaying (plain text) there is the last else inserted that just echo's the $value.
MY FOUND SOLUTION
So regarding the question, i found out that i can create redirection links using the ?SomePage on the projects page and using this 'added link value' on the organisations page to compare it. If the link is equal, then do the specific query. But it is probably easier to paste the code here:
The 'CATCHING' part
To catch a specific lead partner is also used an identifier in my query called ORG (which stands for organisation). So here is the code where i catch the organisations table:
if($key =='ORG' && $value != NULL){
$needle = array('Brainport','Development','BRAINPORT',
'brainport','DEVELOPMENT','development');
$needle2 = array('Casa','CASA', 'casa');
if (strpos_arr($value,$needle) !== false) {
echo '<a href="http://portal.e-ucare.eu/database/organisations/?a=brainport" >'.$value.'</a>';
}
if (strpos_arr($value,$needle2) !== false) {
echo '<a href="http://portal.e-ucare.eu/database/organisations/?a=casa" >'.$value.'</a>';
}
}
In the above code i just created it for 2 organisations (brainport and casa). For this solution i used a function called strpos_arr which searches for the needle in the haystack;) So in the code above i set the needles to the names in the database where it has to create a link for. So if for example a company with the word Brainport exists in the database, this 'catcher' will see this and display the organisation and make the name clickable.
The strpos_arr function for the $needle is as follows:
function strpos_arr($value, $needle) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $what) {
if(($pos = strpos($value, $what))!==false) return $pos;
}
return false;
}
In the redirection page the code will also catch certain links to make queries for that link -- so for the brainport link this is http://portal.e-ucare.eu/database/organisations/?a=brainport -- in the second page code this link is catched like so:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'portal.e-ucare.eu/database/organisations/?a=brainport')
{
$link_word = "Brainport";
$tmp = $wpdb->get_results("
SELECT
name_of_company__organization, company_location,
company_website as LINK, organisation_type,
organisation_active_in_, organisation_scope,
organisation_files as DOC
FROM
wp_participants_database
WHERE
name_of_company__organization REGEXP ('$link_word')
ORDER BY
name_of_company__organization ASC
");
}
With this solution i can do what i want, BUT like i said in the beginning, i need this for not just 2 organisations but 128!! This would mean i have to copy the catching code blocks on both pages 128 times!! This is obviously not very efficient..
So is there any more efficient way to achieve this? Sorry if it is a bit unclear but i found it quite hard to easily wright this down;)
Anyway, thank you in advance!
The firts part you can rewrite to
if($key =='ORG' && $value != NULL){
$needles = array(
'brainport'=>array('Brainport','Development','BRAINPORT','brainport','DEVELOPMENT','development'),
'casa'=>array('Casa','CASA', 'casa')
);
foreach($needles AS $nkey => $needle){
if(strpos_arr($value,$needle) !== false) {
echo "<a href='http://portal.e-ucare.eu/database/organisations/?a={$nkey}' >{$value}</a>";
}
}
}
For the second part make an array like
[EDIT]
$link_words = array(
'portal.e-ucare.eu/database/organisations/?a=brainport'=>'Brainport',
'portal.e-ucare.eu/database/organisations/?a=casa'=>'Casa',
);
then you can use
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(!empty($link_words[$host])){
$link_word = $link_words[$host];
$tmp = $wpdb->get_results("
SELECT
name_of_company__organization, company_location, company_website as LINK, organisation_type, organisation_active_in_, organisation_scope, organisation_files as DOC
FROM
wp_participants_database
WHERE
name_of_company__organization REGEXP ('$link_word')
ORDER BY
name_of_company__organization ASC
");
}

str_replace() function and multiple variables?

This is going to be pretty hard to explain, so I'll try to make it as much of a chronological story as possible and end with the question, so that anyone who needs a relatively in-depth idea of what I'm talking about has one :).
I have a theme template with four text containers named primary_headline, primary_subline, secondary_headline and secondary_subtext. The content for each container comes from echo($container_name).
Each container has it's own variable, $container_name, which posts user created content to from a form, contents which sometimes contains the variables $city_name and $ref_name as raw data (i.e. "Hello, I'm from $city_name"), as $city_name and $ref_name has assigned values, for arguments sake let's just say $city_name= Dallas and $ref_name = Facebook.
Originally, I believed that I only wanted the user to be able to use $city_name and $ref_name within the secondary_subtext container, and I was also going to make the option to use $city_name OR $ref_name the choice of the user in the first place (they'd have to select one, or the other, or none - but couldn't select both.
However, I now want to allow $city_name AND $ref_name across all four container variables ($secondary_subtext for example).
So, my question is, how do I go about doing that in the easiest possible fashion?
Here's my old code for good measure:
if($geo_text == "enable") {
$geo_init = include("inc/geo_text.php");
$secondary_headline = str_replace('$city_name', $city_name, $_POST['secondary_headline']); // $_POST data from user
} else($ref_text == "enable") {
$ref_init = include("inc/ref_text.php");
$secondary_headline = str_replace('$ref_name', $ref_name, $_POST['secondary_headline']); // $_POST data from user
} else { $secondary_headline = $_POST['secondary_headline']; }
Any comments/answers etc will be very greatly appreciated :)!!
$placeholders = array('city_name','ref_name');
$include_mapper('geo_text' => 'inc/geo_text.php','ref_text'=>'inc/ref_text.php');
$containers = array('first_headline','scondary_headline');
$found = 0;
foreach ($include_mapper as $map => $include){
if ($$map == "enable"){
include ($include_mapper[$map]);
foreach ($container as $container){
foreach ($placeholders as $placeholder){
$$container = str_replace('$'.$placeholder, $$placeholder, $_POST[$container];
}
}
$found = 1;
break;
}
}
if (!$found){
foreach ($container as $container){
$$container = $_POST[$container];
}
}

Categories