I need your help. When user enter plural word or adjective to find singular word in my native language: czech. For example: when user enters motorový, it should return motor (now return motorov) without ový, same when i enter motorce (return motorc). It seems like 2letter and 3letter check doesn't work for me, 1letter check works. U can try my code on webiste . Thank you for you effort, time and good advices.
My Code:
<!DOCTYPE html>
<html lang="cs">
<meta charset="utf-8" />
<head>
<title>Vyhledavac</title>
</head>
<body>
<form method="get" action="index.php">
<input type="text" name="s_word" value="<?php $_GET['s_word'] ?>" /> <input type="submit" value="Hledat" />
</form>
<hr />
<?php
if(isset($_GET['s_word'])){
$s = $_GET['s_word'];
$s_word = $s;
$l_word = substr($s_word,-3);
if($l_word == "ovi" or $l_word == "ovy" or $_l_word == "ový" ){
$s_word = rtrim($s_word,$l_word);
}
$l_word = substr($s_word,-2);
if($l_word == "ce" or $l_word == "ku" or $l_word == "em"){
$s_word = rtrim($s_word,$l_word);
}
$l_word = substr($s_word,-1);
if($l_word == 'y' or $l_word == 'i' or $l_word == 'u' or $l_word == 'e' or $l_word == 'ů' or $l_word == 'ý'){
$s_word = rtrim($s_word,$l_word);
}
$query = "SELECT * FROM search_test WHERE title LIKE '%$s_word%' OR message LIKE '%$s_word%'";
echo "$query";
mysql_connect("localhost","user","passwd") or die('spatne pripojeni');
mysql_select_db("search") or die('nelze najit databazi');
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'");
$query = mysql_query($query) or die('spatny dotaz');
$numrows = mysql_num_rows($query);
if($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$message = $row['message'];
$website = $row['website'];
echo "<h2><a href='$website'>$title</a></h2><br /><p>$message</p>";
}
}else{
echo "Žádné výsledky pro dotaz: ".$s_word."<br />";
}
}
?>
</body>
</html>
Shouldn't this:
$l_word = $s_word[strlen($s_word)-3];
Be something like:
$l_word = substr($s_word,-3);
The way you currently have it, it will only give you 1 character into $l_word and not the amount you think you're subtracting. Which means $l_word == "ovi" will never match.
For example:
<?php
$s_word = 'motorce';
$l_word = $s_word[strlen($s_word)-3];
echo $l_word;
Will return:
r
Live DEMO and what you actually want is:
<?php
$s_word = 'motorce';
$l_word = substr($s_word,-3);
echo $l_word;
Which returns:
rce
Live DEMO.
It gives you only one character:
$l_word = $s_word[strlen($s_word)-3];
Try with substr().
$l_word = substr($s_word, -3);
Not a fully working solution but the road i would have taken (assuming a UTF-8 encoded file):
$word = $_GET["s_word"];
$removelist = array("ový","ce");
foreach($removelist as $entry) {
if ((pos = mb_stripos($word,$entry)) !== FALSE) {
$word = mb_substr($word,$pos);
break;
}
}
Now $word should be trimmed and ready for searching, otherwise you should add more to the list.
Related
Here is my code. It is a simple html form with two inputs. All data is saved into xml file using DOMDocument a then all data from XML file is inserted into the table below the form. I added two buttons edit and delete (x). Now I can edit and delete any user/player from the table. And here is my problem. I need to remove query string from URL after deleting or editing. I want to add a new user/player into the table after deleting. When I delete manually query string from URL everything works fine again. But I want to delete query string automatically after deleting some user. Sorry for my English hoping you understand me. Thanks in advance!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$xml = new DOMDocument("1.0", "utf-8");
if (is_file('database.xml')) {
$xml->load("database.xml");
$db = $xml->getElementsByTagName('db')->item(0);
} else {
$db = $xml->createElement("db");
$xml->appendChild($db);
$db = $xml->getElementsByTagName('db')->item(0);
}
$newPlayer = $xml->createElement("player");
foreach ($_POST as $key => $value) {
$playerStuff = $xml->createElement($key, $value);
$newPlayer->appendChild($playerStuff);
}
$db->appendChild($newPlayer);
$xml->save("database.xml");
}
$name = $number = "";
if (isset($_GET['action']) && $_GET['action'] == 'edit') {
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
$player = $xml->getElementsByTagName('player')->item($_GET['id']);;
$name = $player->getElementsByTagName('name')->item(0)->nodeValue;
$number = $player->getElementsByTagName('number')->item(0)->nodeValue;
}
if (isset($_GET['action'])) {
switch ($_GET['action']){
case 'edit':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$nameEl = $player->getElementsByTagName('name')->item(0);
$numberEl = $player->getElementsByTagName('number')->item(0);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$newName = $xml->createElement("name", $_POST["name"]);
$nameEl->parentNode->replaceChild($newName, $nameEl);
$newNumber = $xml->createElement("number", $_POST["number"]);
$numberEl->parentNode->replaceChild($newNumber, $numberEl);
$db = $xml->getElementsByTagName('db')->item(0);
$lastElement = $db->lastChild;
$lastElement->parentNode->removeChild($lastElement);
}
$xml->save("database.xml");
break;
case 'delete':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
//$xml = $dokument->getElementsByTagName('xml')->item(0);
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$player->parentNode->removeChild($player);
$xml->save("database.xml");
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>PLAYERS</title>
<link rel="stylesheet" href="stylesss.css">
</head>
<body>
<form action="" method="POST">
<input type="text" name="name" value="<?php echo $name ?>">
<input type="text" name="number" value="<?php echo $number ?>">
<input type="submit" value="INSERT">
</form>
<?php
$xml = new DOMDocument("1.0", "utf-8");
if (is_file('database.xml')) {
$xml->load('database.xml');
$players = $xml->getElementsByTagName("player");
echo "<table>";
echo "<tr><th>" . "Player Name" . "</th><th>" . "Number". "</th></tr>";
foreach($players as $key => $player){
echo "<tr>";
foreach($player->childNodes as $data) {
echo "<td>". $data->nodeValue ."</td>";
}
echo "<td>
<a href='?id={$key}&action=edit' class='buttons edit'>EDIT</a>
<a href='?id={$key}&action=delete' class='buttons delete'>✖</a>
</td>";
echo "</tr>";
}
}
echo "</table>";
?>
</body>
</html>
I would say to redirect header("Location: your-page.php"); The page you redirect to can be the same page but without the query string. It means that if someone refreshes you will not repeat the same action. So, something like this. You have to use the header function before anything is written to the page, it will not work if it is used after HTML or after something is print or echo to the page.
if (isset($_GET['action'])) {
switch ($_GET['action']){
case 'edit':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$nameEl = $player->getElementsByTagName('name')->item(0);
$numberEl = $player->getElementsByTagName('number')->item(0);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$newName = $xml->createElement("name", $_POST["name"]);
$nameEl->parentNode->replaceChild($newName, $nameEl);
$newNumber = $xml->createElement("number", $_POST["number"]);
$numberEl->parentNode->replaceChild($newNumber, $numberEl);
$db = $xml->getElementsByTagName('db')->item(0);
$lastElement = $db->lastChild;
$lastElement->parentNode->removeChild($lastElement);
}
$xml->save("database.xml");
break;
case 'delete':
$xml = new DOMDocument("1.0", "utf-8");
$xml->load("database.xml");
//$xml = $dokument->getElementsByTagName('xml')->item(0);
$player = $xml->getElementsByTagName('player')->item($_GET['id']);
$player->parentNode->removeChild($player);
$xml->save("database.xml");
break;
}
header("Location: your-page.php");
}
I'm making something similar a captha, the not working part is the IF, under if(isset($_POST['submit'])), that always returns false. I think.
Tried a lot ways with no luck...
Anyway, I have followed this solution https://stackoverflow.com/a/21504949/4167976 without success.
Here is my test php and html:
<?php
session_start();
$char = "abcdefghijklmnopqrstuvwxyz1234567890";
$code = $char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)];
$_SESSION["testcode"] = $code;
echo $_SESSION["testcode"]."<br>"; // echo here only for testing
if(isset($_POST['submit'])) {
$code1 = mb_substr($_POST['fullcode'], 0, 5);
$code2 = mb_substr($_POST['fullcode'], -6);
if ($code2 == $_SESSION["testcode"])
{echo "The code is correct!";}
else
{echo "Wrong code!";}
// unset($_SESSION['testcode']); // ???
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="fullcode">
<input type="Submit" name="submit" value="Submit!">
</form>
</body>
</html>
Please, tell me what and where I'm wrong... Thanks! :)
EDIT:
<?php
session_start();
if (!isset($_SESSION["testcode"])) {
$char = "abcdefghijklmnopqrstuvwxyz1234567890";
$code = $char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)];
$_SESSION["testcode"] = $code;
}
if(isset($_POST['submit'])) {
$code1 = mb_substr($_POST['fullcode'], 0, 5);
$code2 = mb_substr($_POST['fullcode'], -6);
if ($code2 === $_SESSION["testcode"])
{echo "The code is correct!<br>";}
else
{echo "Wrong code!<br>";}
unset($_SESSION['testcode']);
$char = "abcdefghijklmnopqrstuvwxyz1234567890";
$code = $char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)];
$_SESSION["testcode"] = $code;
}
?>
Finally, get a new code IF the condition is false!
<?php
session_start();
// first request.
// if not set session 'testcode' and set it, else do nothing.
// prevent session be covered.
if (!isset($_SESSION["testcode"])) {
reFreshCode();
}
if(isset($_POST['submit'])) {
$code1 = mb_substr($_POST['fullcode'], 0, 5);
$code2 = mb_substr($_POST['fullcode'], -6);
if ($code2 == $_SESSION["testcode"])
echo "The code is correct!";
else {
// get a new code IF the condition is false!
echo "Wrong code!";
echo “new code:”.reFreshCode();
}
}
function reFreshCode() {
$char = "abcdefghijklmnopqrstuvwxyz1234567890";
$code = $char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)].$char[rand(0,35)];
return $_SESSION["testcode"] = $code;
}
I have a single page php application to generate an 8 digit number, user inputs a value and page generates another value, both values are inserted into mysql db and displaying the generated value in an input box at a single button click. Insertion is happening, but generated value is not displaying in the inputbox at the first time, I also tried session its not helping too.
here is my php code,
<?php
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<div>
<?php
if(isset($_POST['luckyButton']) && $_POST['myLuckyNumber'] != ""){
//sleep(20);
$myLuckyNum = $_POST['myLuckyNumber'];
$resultmyLuckyNum = mysql_query("SELECT * FROM lucky where luckyNum='$myLuckyNum'") or die(mysql_error());
if (mysql_num_rows($resultmyLuckyNum) > 0) {
while ($rowmyLuckyNum = mysql_fetch_array($resultmyLuckyNum)) {
$generatedNumber = $rowmyLuckyNum["generatedNum"];
}
}else{
$gen = getGen();
$resultGenNum = mysql_query("INSERT INTO lucky (slno, luckyNum, generatedNum) VALUES ( NULL, '$myLuckyNum', '$gen')");
if (mysql_num_rows($resultGenNum) > 0) {
$generatedNumber = $gen;
}
}
}
?>
<form action="" method="post">
<input type="text"class="inputs" name="myLuckyNumber" onClick="this.select();" placeholder="Enter You Lucky Number" value="<?php echo $myLuckyNum; ?>" />
<input type="submit" class="css_button" name="luckyButton" value="Check"/>
</form>
</div>
<br><br><br>
<?php
if($generatedNumber != ""){
echo '<input type="text" name="myLuckyNumber" onClick="this.select();" id="generatedNumber" readonly="true" value="' . $generatedNumber . '" />';
}
echo '<p id="errText"> ' . $err . ' </p>';
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
function getGen(){
$gen1 = random_string(8);
$result = mysql_query("SELECT * FROM lucky where generatedNum='$gen1'") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
if($row["generatedNum"] == $gen1){
getGen();
}
}
}else{
//echo $gen1;
return($gen1);
}
}
?>
</body>
</html>
my table,.
CREATE TABLE `lucky` (
`slno` int(11) NOT NULL,
`luckyNum` text NOT NULL,
`generatedNum` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
The problem with your code is that mysql_num_rows will return either 0 or 1, use it like
if($resultGenNum === 1){
$generatedNumber = $gen;
}
You are using mysql_num_rows in a wrong way. When you run insert query, it does not return rows, it returns true or false (Boolean). When it will return true, you will get 1 (as $resultGenNum).
use this instead:
if ($resultGenNum === 1){
$generatedNumber = $gen;
}
Instead of using if ( $generatedNumber != "" ), perhaps you should try something like if ( isset( $generatedNumber ) ).
If $generatedNumber doesn't exist (which it doesn't if nothing was posted to the script), then the comparison will fail (in fact, you're probably getting a PHP error).
Of course, that leads us to the next problem - if nothing is posted to the script, you don't generate a number. So of course, if you don't generate a number, there is no number to display. So you should structure your code in such a way that a random number is always generated - regardless of whether a post variable was received or not.
I'm aware there are many similar questions on this forum, but it's the 2nd day that I'm going through the answers and nothing seems to work.
It's my first week of PHP learning, so please try to answer in a simple way :) So:
I'm creating a conjugator and so far it's going well, but only if I don't use special characters. As Polish verbs are all about special characters, I'm stuck.
This code works (the conjugation is visible on the screen after pressing "submit"):
Page 1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Conjugator</title>
</head>
<body>
<form action="new.php" accept-charset="UTF-8" method="post" >
Conjugate: <input type="text" name="verb"><br>
<input type="submit">
</form>
</body>
</html>
Page 2:
<?php
header('Content-Type: text/html; charset="UTF-8"');
$verb = $_POST['verb'];
$last2 = substr ($verb, -2);
$last3 = substr($verb, -3);
$last4 = substr($verb, -4);
$root2 = str_replace($last2, "", $verb);
$root3 = str_replace($last3, "", $verb);
$root4 = str_replace($last4, "", $verb);
$nic = array("nie", "nisz", "ni", "nimy", "nicie", "nia" );
$gnic = array("gnije", "gnijesz", "gnije", "gnijemy", "gnijecie", "gnija");
$ac = array("am", "asz", "a", "amy", "acie", "aja");
if ($last3 == "nic" && $last4 != "gnic") {
foreach ($nic as $one) {
echo "<li>$root3$one</li>";
}
}
elseif ($last4 == "gnic") {
foreach ($gnic as $one) {
echo "<li>$root4$one</li>";
}
}
elseif ($last2 == "ac") {
foreach ($ac as $one) {
echo "<li>$root2$one</li>";
}
}
?>
But if I write for example:
$nic = array("nię", "nisz", "ni", "nimy", "nicie", "nią" );
and then:
if ($last3 == "nić" && $last4 != "gnic")
no result shows up.
For checking, try with verbs like "pienić" or "lśnić" (they don't work) or write them without the special characters ("pienic", "lsnic") - the first code will work.
Help will be deeply appreciated!!
You probably can't use substr() with UTF-8. It thinks in terms of bytes, not characters. Take a look at mb_substr().
i wrote this code.
in this code we user guess a number then program checked the number with the number that i gite to it and print appropriates message.
when i start program it shows welcome that is true but when i entered number in it always it shows little in output but it isn't true.
$val = '42';
$gues ;
if(!isset($gues))
$mess = "welcome<br>";
elseif($gues > $val)
$mess = "bigr<br>";
elseif($gues < $val)
$mess = "little<br>";
else
$mess = "win<br>";
$gues = #(int) $gues;
?>
<html>
<head><title>bazi riazi</title></head>
<body>
<h1>
<?php print $mess;
?>
</h1>
<form method="POST">
type you gues here:<input type="text" name='gues'>
</form>
</body>
</html>
How your var $gues get value from form? Did you use $_POST['gues'] first?
Script always print little because probably variable $gues was not initialized any value (default is zero, I suppose...).
This code should work (untested, though):
$val = '42';
$gues = int($_POST['gues']);
if (!isset($gues))
{
$mess = "welcome<br>";
}
elseif($gues > $val)
{
$mess = "bigr<br>";
}
elseif($gues < $val)
{
$mess = "little<br>";
}
else
{
$mess = "win<br>";
}