Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a form script 1

Status
Not open for further replies.

Shukusei

Programmer
Sep 6, 2004
43
0
0
CA
Continuing from my last post(Dynamic PHP), I decided to continue using PHP but use the form-submit way of getting the translation done.

So I changed my script a little, and put it in its own page to display the results and made another page to call it.
I try the page out, it looks like it's going to work, then when I check the source it only shows <html><head>
I have no idea what I did wrong. :S

Here's the translation code:
Code:
<html>
<head>
<?php

$origString = $_POST['toTrans'];
$placeHolder = 0;
$currentChar = "";
$nextChar = "";
$transString = "";
$checkString = "";
$doublesList = array("ph", "gh", "th", "sh", "ch", "rd", "qu", "ng", "ck");

do {
	$currentChar = $origString{$placeHolder};
	$nextChar = $origString{$placeHolder + 1};
	$checkString = $currentChar + nextChar;
	if (in_array($checkString, $doubleList)) {
		switch ($checkString) {
			case "ph":
				$transString += "ng";
				break;
			case "gh":
				$transString += "ng";
				break;
			case "th":
				$transString += "ch";
				break;
			case "sh":
				$transString += "th";
				break;
			case "ch":
				$transString += "sh";
				break;
			case "rd":
				$transString += "ph";
				break;
			case "qu":
				$transString += "ts";
				break;
			case "ng":
				$transString += "rd";
				break;
			case "ck":
				$transString += "k";
				break;
			$placeHolder += 2;
		}
	}
	else {
		switch ($currentChar) {
			case "a":
				$transString += "i";
				break;
			case "b":
				$transString += "w";
				break;
			case "c":
				$transString += "d";
				break;
			case "d":
				$transString += "p";
				break;
			case "e":
				$transString += "a";
				break;
			case "f":
				$transString += "j";
				break;
			case "g":
				$transString += "l";
				break;
			case "h":
				$transString += "c";
				break;
			case "i":
				$transString += "u";
				break;
			case "j":
				$transString += "f";
				break;
			case "k":
				$transString += "g";
				break;
			case "l":
				$transString += "r";
				break;
			case "m":
				$transString += "t";
				break;
			case "n":
				$transString += "s";
				break;
			case "o":
				$transString += "e";
				break;
			case "p":
				$transString += "b";
				break;
			case "q":
				$transString += "1";
				break;
			case "r":
				$transString += "h";
				break;
			case "s":
				$transString += "m";
				break;
			case "t":
				$transString += "n";
				break;
			case "u":
				$transString += "y";
				break;
			case "v":
				$transString += "z";
				break;
			case "w":
				$transString += "k";
				break;
			case "x":
				$transString += "v";
				break;
			case "y":
				$transString += "o";
				break;
			case "z":
				$transString += "x";
				break;
			$placeHolder += 1;
		}
	}
} while ($placeHolder <= strlen($origString));
?>
</head>
<body>
<p>
</p>
</body>
</html>

And here's the calling page:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="dotrans.php">
  <textarea name="toTrans"></textarea>
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
 
the += notation is a mathematical assignment operator. i think you want to use ".="

also where are you actually outputting to the screen?
 
I'll be using echo $transString in the <p></p> tagpair.
I'll try changing the += to .= to see if it works, thanks for the pointer.
 
i have had a closer look at your code. it will time out every time you run it.

before posting this kind of thing again, read sleipnir214's FAQ on debugging and click the link in his signature to read about asking good questions. there needs to be a bit more self-help.

i have the following pointers:

1. you are using curly brackets for the first two lines of the do loop. why? i assume you mean to use an array notation which is normally $array[key].
2. you are treating $origstring as if it were an array. however it is a string. if you want to treat it like an array you first have to translate it into an array. i would have thought you would be better off using the substr() function
3. you have included the counter increment inside the switch statement but outside any case directive. you must either include it in every case or do so after the switch.
4. you refer to $doubleList in the if statement. it should be $doublesList.
5. you cannot concatenate strings with the + operator (eg $checkString). you need to use the "." operator
6. the reference to nextChar should have a string identifier in front of it.
7. you did not provide for a default on your single char translations. did you intend this?

since i've done the work to fix it in writing the above, here is some self-processing code
Code:
<html>
<head>
<?
if (isset($_POST['toTrans']))
{
?>

<?php

$origString = $_POST['toTrans'];
$placeHolder = 0;
$currentChar = "";
$nextChar = "";
$transString = "";
$checkString = "";
$doublesList = array("ph", "gh", "th", "sh", "ch", "rd", "qu", "ng", "ck");

do {
    $currentChar = substr($origString,$placeHolder,1);
    $nextChar = substr($origString,$placeHolder + 1,1);
    $checkString = $currentChar . $nextChar;
	echo $checkString ."<br>";
    if (in_array($checkString, $doublesList)) {
        switch ($checkString) {
            case "ph":
                $transString .= "ng";
                break;
            case "gh":
                $transString .= "ng";
                break;
            case "th":
                $transString .= "ch";
                break;
            case "sh":
                $transString .= "th";
                break;
            case "ch":
                $transString .= "sh";
                break;
            case "rd":
                $transString .= "ph";
                break;
            case "qu":
                $transString .= "ts";
                break;
            case "ng":
                $transString .= "rd";
                break;
            case "ck":
                $transString .= "k";
                break;

        }
        $placeHolder = $placeHolder + 2;
    }
    else {
        switch ($currentChar) {
            case "a":
                $transString .= "i";
                break;
            case "b":
                $transString .= "w";
                break;
            case "c":
                $transString .= "d";
                break;
            case "d":
                $transString .= "p";
                break;
            case "e":
                $transString .= "a";
                break;
            case "f":
                $transString .= "j";
                break;
            case "g":
                $transString .= "l";
                break;
            case "h":
                $transString .= "c";
                break;
            case "i":
                $transString .= "u";
                break;
            case "j":
                $transString .= "f";
                break;
            case "k":
                $transString .= "g";
                break;
            case "l":
                $transString .= "r";
                break;
            case "m":
                $transString .= "t";
                break;
            case "n":
                $transString .= "s";
                break;
            case "o":
                $transString .= "e";
                break;
            case "p":
                $transString .= "b";
                break;
            case "q":
                $transString .= "1";
                break;
            case "r":
                $transString .= "h";
                break;
            case "s":
                $transString .= "m";
                break;
            case "t":
                $transString .= "n";
                break;
            case "u":
                $transString .= "y";
                break;
            case "v":
                $transString .= "z";
                break;
            case "w":
                $transString .= "k";
                break;
            case "x":
                $transString .= "v";
                break;
            case "y":
                $transString .= "o";
                break;
            case "z":
                $transString .= "x";
                break;
			default:
				$transString .= $currentChar;
				break;

        }
		$placeHolder ++;
    }
} while ($placeHolder < strlen($origString));
}?>
</head>
<body>
<p>
<? echo "$transString"; ?>
</p>
<form name="form1" method="post" action="#">
  <textarea name="toTrans"></textarea>
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
 
Thanks alot!
Yeah, I kind of expected to have made a significant amount of silly errors, I'm new to PHP.
To answer the questions you asked;
1) I got that from the PHP.net site, actually. The code showed you can used the curley brace notation in place of the full code, maybe I read it wrong
7) I should of, but completely overlooked it.

And again, thank you for being so helpful(and it works perfectly fine too, thanks!)
 
Justin

there are times when you can use the curly brackets. but i don't think it works the way you wanted. to address an array element you have to use the square brackets.

rgds
Justin (!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top