-4

I've been working on a project for school where I have you search for a string, and it wil query a database and return all of the words which are in that string of letters. For that, I have to use strtoupper(), which works fine if you have a string of all uppercase or lowercase letters. If you enter AAB or aab into the search, everything will work fine, and it will return the two anagrams, ABA and BAA. However, if you type in aAB, it will return nothing. So it is getting the post data from the input, named alpha, and then it is alphabetizing the word, so if you typed in ABA, it would return AAB, and then making it uppercase.

<title>Scrabble</title>
<?php
require 'connect.inc.php';
if (isset($_POST['al'])){
    $al=$_POST['al'];
    $al=alpha($al);
    $al=trim(strtoupper($al));
    $query="SELECT * from Words WHERE alpha='$al'";
    if ($query_run = mysql_query($query)){

        while ($query_row = mysql_fetch_assoc($query_run)){
            $alpha = $query_row['alpha'];
            $ana = $query_row['word'];

            echo "<strong>$ana</strong> $alpha<br>";

        }
    }


}
function alpha($word){
    $array=array();
    for($x=0;$x<strlen($word);$x++){
        $char=substr($word,$x,1);
        $array[$x]=$char;


    }
    sort($array);
    $alpha=implode('',$array);
    return $alpha;

}
?>
<form action='scrabble.php' method='POST'>
Enter text to anagram. Please use either all uppercase or all lowercase<input type='text'                   name='al'>
<input type='submit'>
</form>

he link is here http://newdev.shodor.org/~amalani/newdev/scrabble.php Thanks

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64

1 Answers1

1

Multiple issues:

PHP's sort function returns a boolean for whether it was successful or not. In your code you have:

$array=sort($array);

When it should just be:

sort($array);

You have a syntax error near the top:

$alphagram=trim(strtoupper($al);

Should probably be:

$alphagram = trim(strtoupper($alphagram));

I also noticed that on your website, you use:

name='al'

And then in the script try to access the POST variable alpha. You should be using:

$_POST['al']
Supericy
  • 5,866
  • 1
  • 21
  • 25