Hello guys, I having a hard time what this error causes in my script. Please see the following codes: // create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array(""), ); foreach ($endings as $ploral_end => $singulars) { if( substr($neword, - strlen($ploral_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($ploral_end)) . $replace_singular; //check_for_word is your word lookup function if ( check_for_word($tryword) ) { $neword = $tryword; break; } } Code (markup): I can't figure it out because it pointed that error is in the last line of the script. Thank you guys for helping me out.
Try this code: // create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array(""), ); foreach ($endings as $ploral_end => $singulars) { if( substr($neword, - strlen($ploral_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($ploral_end)) . $replace_singular; //check_for_word is your word lookup function if ( check_for_word($tryword) ) { $neword = $tryword; break; } }; Code (markup): Let me know if that helps!
// create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array(""), //<-------take off the last commar from the array ); foreach ($endings as $ploral_end => $singulars) { if( substr($neword, - strlen($ploral_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($ploral_end)) . $replace_singular; //check_for_word is your word lookup function if ( check_for_word($tryword) ) { $neword = $tryword; break; }//if }//for }//if }//for PHP:
Okay the error was gone, thanks php-lover! But why is it my script doesn't work? It doesn't replace queries that end in "es" "ves" and such? Is there something wrong with the construction of the code?
LOL. You got me there.. Thanks for the help guys but my script still doesn't work. I don't know if I wrote a correct function please take a look again. $slashedword = stripslashes($_REQUEST['w']); $neword = preg_replace('%\ $%', '', $slashedword); // create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array("") ); foreach ($endings as $plural_end => $singulars) { if( substr($neword, - strlen($plural_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular; function check_for_word() { $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC'); $foundword = mysql_fetch_array( $matching ); }//function //check_for_word is your word lookup function if ( check_for_word($tryword) ) { $word = $foundword; break; }//if }//for }//if }//for //Find the word again that matched previously $result = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'" ORDER BY ss_type ASC'); .....and so on... Code (markup): This error shows up:
$slashedword = stripslashes($_REQUEST['w']); $neword = preg_replace('%\ $%', '', $slashedword); // create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array("") ); foreach ($endings as $plural_end => $singulars) { if( substr($neword, - strlen($plural_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular; //check_for_word is your word lookup function if ( check_for_word($tryword) ) { $word = $tryword; break; }//if }//for }//if }//for function check_for_word($tryword) { $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC'); if(is_resource($matching)){ $result = (mysql_num_rows($matching) > 0)true:false; return $result; }//if }//function PHP:
There is an error, it says The line is in: $result = (mysql_num_rows($matching) > 0)true:false; Code (markup): What does that mean?
$result = (mysql_num_rows($matching) > 0)true:false; $result = (if number of result row is greater than zero) then return true else return false; Can you post the error line number.
I see. Here's the actual error: The line 81 is the bolded texts: // create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array("") ); foreach ($endings as $plural_end => $singulars) { if( substr($neword, - strlen($plural_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular; //check_for_word is your word lookup function if ( check_for_word($tryword) ) { $word = $tryword; break; }//if }//for }//if }//for function check_for_word($tryword) { $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC'); if(is_resource($matching)){ [B] $result = (mysql_num_rows($matching) > 0)true:false;[/B] return $result; }//if }//function Code (markup): The actual php script is this. I am sorry, I am still learning php. I hope you understand.
//Not a problem. Replace the function with this code function check_for_word($tryword) { $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC'); if(is_resource($matching)){ if(mysql_num_rows($matching) > 0) return true; else return false; }//if }//function PHP:
The previous error was gone, but a new error came up: The bolded text is the line: // create ending array $endings = array( "ves" => array("fe", "f"), "fs" => array ("f"), "os" => array("o"), "oes" => array("o"), "ies" => array("y"), "xes" => array("x", "xe"), "as" => array("a"), "ae" => array("a"), "rs" => array("r"), "es" => array(""), "s" => array("") ); foreach ($endings as $plural_end => $singulars) { if( substr($neword, - strlen($plural_end)) == $plural_end ) { foreach ($singulars as $replace_singular) { $tryword = substr($neword, 0, strlen($neword) - strlen($plural_end)) . $replace_singular; //check_for_word is your word lookup function [B]if ( check_for_word($tryword) ) {[/B] $word = $tryword; break; }//if }//for }//if }//for function check_for_word($tryword) { $matching = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $tryword).'" ORDER BY ss_type ASC'); if(is_resource($matching)){ if(mysql_num_rows($matching) > 0) return true; else return false; }//if }//function Code (markup): PS: I have solved the problem. I put function check_for_word before the if ( check_for_word($tryword) and it seems it is working now but it still has minor problems. When I searched for words that ends in s such as "seasons" it return as to "season" but the rest of the endings in the array seems doesn't replace the query?
After a lot of hours searching for the answer, I found the solution. I will share it to you because this one is new to me. The solution is to take the function always outside a loop. Pretty clever right? Why? Functions declared in a function are global, so calling the container function twice will attempt to create the same function in the global scope. -from E-E.com So lesson for the day: Take out the functions outside a loop.