PHP SPLIT
Posted by chaabant on Jan 4, 2009
hi..can anyone please solve my problem?
i have text input and a submit button.
The user will insert sentence/question/any text in the text input.Then, when he/she press the submit
button, the input will be keep in the database(mysql). After that, i want to split the input(let say a sentence) into words(array).
Here is an example:
Input: Differentiate between array and arraylist.
After splitting:
=>Differentiate
=>between
=>array
=>and
=>arraylist
=>.
I wonder how mysql handle the input after splitting it?is it in a field or each index of array(each word) in the difference field?because after splitting it, i want to match the input(which is now in form of words not a sentence) with a library i made in mysql.
Let say, in my library there are 3 fields:weight1,weight2,weight3.The word ‘what’ was in field weight1 in my
library.
So, the question is: how can i split the input into words and then match it with my library so that i can state the input is in which weight(weight1/weight2/weight3).
Please help me.
Thank you.
Hummm i dont know if i get your question ….
here is what i can answer :
When you have the user submit the form , the data will be as is in the database .
so if the user submited :
“Differentiate between array and arraylist”
it will be as is in the database , except if you did something using php on it .
like using the split function and even using the split function you are the only one to decide what you want to do with it .
because all depend on how your database is made (how many fields you have).
give us more information if you want more help .
Thanks .
PHP TUTORIALS ADMIN
sorry for any misunderstanding.my english is not so good.therefore, it’s hard for me to ask/explain the problem.
Here are the flows:
1)submit form,insert the input(sentence) into the database (table name:question, 2 fields:num_question and question)
2)split the input(sentence) into words/array and insert into database(table name:token, i don’t know how many field to create.but for now, i create 9 fields for the inserting the array).
3)search if any words from the input that have been splitted into array match anywords in the database (table name:level, 3 fields:weight1,weight2,weight3).
e.g, weight1 have:what,how,etc.. weight2:describe,differentiate and so on.
4)decide and state the weight for the input submitted.
e.g: Differentiate between array and arraylist. So, it should be in weight2.
Sample of my code:
<?php
include (‘config.php’);
if (isset($_POST['btnSubmit'])){
if($question)
{
$question = $_POST['txtquestion'];
$con = mysql_connect($host,$user,$pword);
mysql_select_db($dbase);
$queryT = “select * from question where question = ‘$question’”;
$resultT = mysql_query($queryT);
$query = “INSERT INTO `question` ( `num_question` , `question`)
VALUES (”, ‘$question’)”;//insert the input into database
$result = mysql_query($query);
if($result)
{
echo “Data successfully entered.“;
echo “”;
echo “”;
echo “PREVIEW: “;
echo “Data: $question”;
echo “”;
echo “”;
echo strtolower($question);//make the input in lowercase
$question=explode(” “, $question);//split the input which is sentence into words or array form
print_r($question);
$queryS = “INSERT INTO `token` (`num_question`,`arr1`,`arr2`,`arr3`,`arr4`,`arr5`,`arr6`,`arr7`,`arr8`,`arr9`)
VALUES (”,’$question’,'$question’,'$question’,'$question’,'$question’,'$question’,'$question’,'$question’,'$question’)”;
$resultS = mysql_query($queryS);//insert the input which has been splitted before to the database
$queryC=”SELECT * FROM `level` WHERE MATCH ($question) AGAINST `database`”;//compare the splitted input to match anyword from the database.
$resultC = mysql_query($queryC);//if found anywords that match with words in the database
if($resultC){
echo” The question is in $resultC”;//the weight of the input is decided.
}
}
}//if $question
}//if isset btnSubmit
?>
For now,i can do until splitting the input(sentence).But the problem is i cannot insert the array word by word into database(table token).If that so, how can i match the input words with the words in the database and decide what weight it is.
Hopefully you can understand and help me.
Thank you..