tags:

views:

59

answers:

3

I have this code and it should print labels with text boxes for people to fill in information. It takes a text like : $tokens = "*noun *noun *verb" and should print to the user a table that has :

Noun: (text box to be filled) Noun: (text box to be filled) verb: (text box to be filled) etc.

but it is not working

echo "<form action=\"storygenerated.php\" method=\"post\">
        <input name=\"fields\" type=\"hidden\" value=\"$tokens\" />
        <input name=\"story\" type=\"hidden\" value=\"$story\" />
        <table>";
for ($i = 0; $i < count($tokenArray); $i++) {
    $fieldWords = split('_',$tokensArray[$i]);
    echo "<tr><td>";
    echo $fieldWords[0];
    for ($j = 1; $j < count($fieldWords); $j++) {
        echo " ".$fieldWords[$j];
    }
    echo ":";
    echo "</td><td><input name=\"$tokensArray[$i]\" type=\"text\" /></td></tr>";
}

which is from this code that is generating the text $tokens

$storyArray = split(' ', $story);
$tokens = ""; // space-delimited list of fields
$tokensArray=array();
for ($i = 0; $i < count($storyArray); $i++) {
    if ($storyArray[$i][0] == '*') {  
        $tokens .= $storyArray[$i] . " ";
        $tokensArray[] = $storyArray[$i];
    }
}
+1  A: 

There is a few things wrong in your code. I'll start with the first segment.

  1. In your for condition, you are using $tokenArray instead of $tokensArray (missing s) in your count().

  2. In your last echo, since you are using a compound variable name ($tokensArray[$i]), you should enclose it in braces as such: {$tokensArray[$i]}

The corrected code for the first part is the following:

echo "<form action=\"storygenerated.php\" method=\"post\">
        <input name=\"fields\" type=\"hidden\" value=\"$tokens\" />
        <input name=\"story\" type=\"hidden\" value=\"$story\" />
        <table>";
for ($i = 0; $i < count($tokensArray); $i++) {
    $fieldWords = split('_',$tokensArray[$i]);
    echo "<tr><td>";
    echo $fieldWords[0];
    for ($j = 1; $j < count($fieldWords); $j++) {
        echo " ".$fieldWords[$j];
    }
    echo ":";
    echo "</td><td><input name=\"{$tokensArray[$i]}\" type=\"text\" /></td></tr>";
}


There is also a few performance improvements that could be done which would increase readability and simplify your code.

  • Since you are using characters to split your string, use explode() instead of split(). split() uses a regular expression to split the string versus explode() which simply uses plain characters.

  • The whole $fieldWords split and loop could be replaced by a one liner:


echo str_replace('_', ' ', $tokensArray[$i]);
  • You are separating your $story string manually when using a regular expression would be so much simpler:


preg_match_all('/\*\w+/m', $story, $tokensArray);
$tokensArray = $tokensArray[0];
$tokens = implode(' ', $tokensArray); // Space delimited list of tokens
Andrew Moore
Thank you very much, i really appreciate your help. I asked another question, i really appreciate the help i am getting in this website :)
c2009l123
A: 

In the line if ($storyArray[$i][0] == '*') {
you check just if the string is a strig with 1 caracter egual to an asterisc so I suppose your $tokensArray[] is always empty.

I hope you'll modify the lines echo $fieldWords[0];
for ($j = 1; $j < count($fieldWords); $j++) {
echo " ".$fieldWords[$j];
}
with

foreach ($fieldWords as $word) { echo " ".$word; }

Fabio Mattei
Wrong, `$storyArray[$i][0]` checks if the first character of `$storyArray[$i]` is a `*`. Strings are arrays.
Andrew Moore
A: 

Two things I was not doing. The first one is that I was dealing with story like an array even though it is not, it is a string. The second is that I was not actually replacing the variables with the value I wanted to be published. I solved the problem with the help of the people who posted some information here. Thank you all.

c2009l123