multi-array search query with delimited field - php

I have a array which store some country names
im doing a search module
and i wanna to find the db records whether contain the country names or not
but since db design problem , those record can own mutil counties and store the countries by delimiter "|"
below is a example
input: array("Cyprus","Austria") // note that the max input will be 300 country
db record country column: Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina|Bulgaria|Croatia|Cyprus|Czech Republic|Denmark|Estonia|European Union|Faroe Islands
So anyone can give a solution base on php for seaching?

First of all your DB design is not good. You should not be putting multiple delimited country names in one filed. You might wanna change it.
If you really want to work with the existing design, you can make use of explode and in_array functions of PHP as:
$db_rec = 'Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina';
$arr = explode('|',$db_rec);
$key = 'Albania';
if(in_array($key,$arr)) {
echo "$key found";
}

Related

Stored ENUM is displayed as a string

I have an ENUM stored in PHPMYADMIN which allows the numbers 1-10.
I'm trying to find out how that number can be converted to a string which the user can see, an example is;
1=London
2=Spain
3=France
4=Germany
etc...
The obvious way would be to do an if statement for each something like
if ENUM == 1 then STRING == "London"
if ENUM == 2 then STRING == "Spain"
but I was wondering if there was a similar way of doing this or if I just need to do 10 if statements. I've tried to look online but no helpful tutorials.
Thanks (Sorry i've had to submit the question as code, stackoverflow wouldnt allow me to post it otherwise for some reason)
Here is an efficient/clean/professional way of doing it:
$enum = 1; // The value fetched from the database
$cities = array(
'1'=>'London',
'2'=>'Spain',
'3'=>'France',
'4'=>'Germany'
); // Array of cities
// Make sure there is a city with the given key
if(isset($cities[$enum])){
echo $cities[$enum];
}
But it is also advisable to store the cities in another database table.

Search with split text in DB and get re-hitting value

I'd like to get the most matched data from DB by split array in search query.
Imagine my query is: "How to buy a motorbike" & in DB I've enlisted some data like
buy a helicopter
buy a motorbike
buy a car
So, after split the query, it'll search into DB as "how", "to","buy", "motorbike" respectfully. Using these sub-strings, the query only generate "buy a helicopter" string in output. But I want to get buy a motorbike data. I generated a method but it only encode data when any of these sub-strings get matched with same data in DB.
Here's my code that fetch only first matched data but my desired data is in no. 2.
$str=$_POST['search'];
$str=preg_split("/[\s]+/", $str);
foreach ($str as $search ) {
$sql = "SELECT answer FROM query_tbl WHERE q1 like '%".$search."%' ";
$record = mysqli_query($link, $sql);
$rows=mysqli_fetch_assoc($record);
if ($rows == true) {
echo json_encode(array('ans'=>$rows['answer']));
}
}
So, which algorithm should i follow and where to use it to achieve my goal?
I think it would be best to do a full-text search as described here.
By default or with the IN NATURAL LANGUAGE MODE modifier, the MATCH() function performs a natural language search for a string against a text collection. A collection is a set of one or more columns included in a FULLTEXT index. The search string is given as the argument to AGAINST(). For each row in the table, MATCH() returns a relevance value; that is, a similarity measure between the search string and the text in that row in the columns named in the MATCH() list.

How to do search function with one textbox and search for values in many columns of the mysql db using php codeigniter

I want to create a search function in a way that users can write all the keywords on a textbox
(just like Google search) ex :- Cars in Japan, Used cars in Japan. In this second example we can identify three keywords which belong to different columns in Item table in my DB. ex :- "Used" keyword from condition column, "cars" keyword from category column and "Japan" keyword from location column. Currently I'm using two drop down lists to select location and category. And check boxes to select condition. So I want to take these all keywords with one textbox. please help..!!!
You can capture the contents of the search bar into a php var ($inputRaw for example).
$inputRaw = $_POST[yourSearchBox]; // capture the raw user search input
From there you can clean it up a bit:
$InputRawArr = $searchResultsArr = array();
// process raw input
// trim any extra spaces outside the string
// then remove any double spaces inbetween the search terms
$inputRaw = str_replace(' ', ' ', trim($inputRaw));
// separate each search term into an array
$inputRawArr = explode(' ', $inputRaw);
$inputRawArr = array_filter($inputRawArr);
So now if a user has entered 'used cars in Japan', you will end up with:
$inputRawArr =
[0] = 'used',
[1] = 'cars',
[2] = 'in',
[3] = 'Japan'
From here you should be able to create some logic to search the $inputRawArr for the strings relevant to your desired result. Checkout Php's in_array function for this part
So if 'used' in array $inputRawArr, then set that part of the query true. Same with 'car'.
Do determine if a location ('Japan') is selected, I would cycle the $inputRawArr contents against a list of the possible locations. If one or more cells matches one of the locations in the list, then store those $inputRawArr cells into a PHP generated WHERE clause for location.

php sql:can contents of a word document being split and then insert to database in single row each

sorry for disturbing. I have a table on database call KEYWORDS..contains KEY column..I have a bunch of keywords list in 1 file of docx format called keywords.docx.. each keywords in the file is separated by comma..the keywords will be like this:
environment, country pollution, animal habitat problem, water, energy..
So up there, they are 5 keywords. The idea is I will upload my keywords.docx to the page and then click save button.. in the database server side, the keywords will be automatically insert to the KEY column separately. That means they will have 5 rows of keywords in KEY column under KEYWORDS table. Is it possible in php and mysql..???
Thank you very much for your help and opinion... :)
You can use explode method in PHP somthing like this:-
<?php $data="ONE TWO THREE FOUR FIVE";
$splittedstring=explode(" ",$data,-2);
foreach ($splittedstring as $key => $value) {
echo "splittedstring[".$key."] = ".$value."<br>"; }
?>
its output will be like this:-
splittedstring[0] = ONE
splittedstring[1] = TWO
splittedstring[2] = THREE
See more at:
http://braincybersolutions.com/php-tutorial/php-explode-split-a-string-by-string-into-array/#sthash.tnUaGi9w.dpuf
After this you can store your values in your database.

Numbers and calculations in php/sql

ok im new on the scene with php and sql, i read the basics and was able to produce a DB driven shoutbox.
This is my website http://www.teamdelta.byethost12.com/
What i want to do next im not sure what i should be using java? php? sql? i have no idea...
but i assume php and sql if sql can do what i think it can..
What i want to do next is to A extract data from data at certain positions for example
data in the format "DAB:CD:EF:GH" eg "D03:57:16:23" to be turned into AB, DF, CE. Eg "3","76","51".
then i want to store these Numbers (Not VARCHARS) in the database.
that is part 1.
the sceond part that i want to make sure is possible before i put to much effort in is to do calculations on all the entries in the database with respect to a 3rd peice of data and display all the entries in a decending ordered list ordered by the output of the calculation..
i think calculations can be added to querys...but as i said im new and the tutorial i read only coved reading values out of the db..
and just if it helps to clarify what im trying to do even though this is not part of the question... here is what im trying to do
* set up a database and entry system that records the player name, base name, location,econ, and comment and stored this as a entry in the database..(i have done this)
*on entry i wish to extract numeric values(AB,DF,CE) from a text string(location) (dont know how)
*then i wish to display the list(i have done this)
*a new column should be added on display containing the resuly of a calculation involving the numberic values from each entry with a global value that is entered on page(dont know how)
*then the list should be sorted in decending order of the output of the calculation.
*lastly i wish to be able to remove items from the list with a click.
thats all :) ,, seeking part advice and guidence
here is the page its php but i renamed it as text so its readble.
cant past is as code as it has escape chars in it
http://www.teamdelta.byethost12.com/trade/postroute3.txt
currently trying to use
$values = array();
$string = $location;
$values[0]= $string[1].$string[2];
$values[1]= $string[5].$string[8];
$values[2]= $string[4].$string[7];
$x = intval($values[1], 10);
$y = intval($values[2], 10);
$g = intval($values[0], 10);
print_r($x);
echo(' : '); print_r($y); echo(' : ');
print_r($g);
ok.. for the first part.
$values = array();
$values = split(":", 'DAB:CD:EF:GH');
$int_values = array();
foreach($values as $v) {
$int_values[] = intval($v, 10);
}
// values in base 10
print_r($int_values);

Categories