1

I am trying to create a PHP function which will allow me to read selected lines from a text file using a JSON string. The JSON string consists of two numbers separated by a comma. The first number is the line I want to seek, and the second is the number of lines to read going downward.

For example, I have this sentence in a separate text file:

A diving spiral attack in which Pikachu head butts downward while 
being surrounded by electricity.

(If you're wondering, this is part of my upcoming fan site, still under construction).

The sentence starts at line 6, and uses up 2 lines. So in my JSON file, I would write "6,2". The explode() command is ran using the comma as the delimiter, allowing me to store these number into two individual variables, $start and $length.

    $result = explode(",", $sample); // In JSON string, $sample is $i["desc"]
    $start = $result[0];
    $length = $result[1];

With the variables now in place, the following lines will then be called to perform the actual line-by-line extraction.

    $file = new SplFileObject("pikachu/pikachu.txt");

    for ($i = $start; $i <= ($start+$length); $i++) {
        $file->seek($i-1);
        echo "$file";
    }

I did a test of my script using a direct call instead of through JSON parsing. That worked. But when I tried to use the JSON string, I ended up with both a Runtime and a Fatal Error.

How do I fix the scripts so that I can use the JSON string as markers for seeking lines from an external text to print while avoiding the Runtime and Fatal Errors?

(EDIT: The problem became too difficult for me to fix, so I've decided to ditch the line-by-line seek feature for now. I will revisit this on a later date. I will leave the code below for archival purposes.)

character.php

    <?php

    $name = $_GET['name'];
    // Step 1: Get the name from the previous page and store it.

    $file = file_get_contents("characters/$name/$name.json");
    // Step 2: Use the name to find a JSON file for the character (We'll do the moves list later).

    $json = json_decode($file, true);
    // Step 3: Decode the JSON file

    $character = $json["character"][0]["name"];
    $group = $json["character"][0]["group"];
    $series = $json["character"][0]["series"];
    $story = $json["character"][0]["story"];
    $snapback = $json["character"][0]["snapback"];
    $interrupt = $json["character"][0]["interrupt"];
    $hiddenpow = $json["character"][0]["hiddenpow"];
    $disrupt = $json["character"][0]["disrupt"];
    $assist1 = $json["character"][0]["assist1"];
    $assist2 = $json["character"][0]["assist2"];
    $assist3 = $json["character"][0]["assist3"];
    $counter = $json["character"][0]["counter"];
    $bros = $json["character"][0]["bros"];
    $party = $json["character"][0]["party"];
    $ground = $json["character"][0]["ground"];
    $jump = $json["character"][0]["jump"];
    $superjump = $json["character"][0]["superjump"];
    $launcher = $json["character"][0]["launcher"];
    $altlaunch = $json["character"][0]["altlaunch"];
    $acfinisher = $json["character"][0]["acfinisher"];
    // Step 4: Retrieve variables and store them for use

    echo "<HTML>\n";
    echo "<HEAD>\n";
    echo "<TITLE>Marvel & Capcom vs. Pokemon: $character</TITLE>\n\n";
    echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"http://fonts.googleapis.com/css?family=Roboto\">\n";
    echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"http://fonts.googleapis.com/css?family=Open+Sans\">\n";
    echo "<LINK rel=\"stylesheet\" type=\"text/css\" href=\"media/style.css\">\n";
    echo "<SCRIPT type=\"text/javascript\" src=\"media/tabs.js\"></SCRIPT>\n";
    echo "<SCRIPT type=\"text/javascript\">\n";
    echo "function toggle_visibility(id) {\n";
    echo "    var movename = id.nextSibling.nextSibling;\n";
    echo "    if(movename.style.display == \"block\")\n";
    echo "        movename.style.display = \"none\";\n";
    echo "    else\n";
    echo "        movename.style.display = \"block\";\n";
    echo "}\n";
    echo "</SCRIPT>";
    echo "</HEAD>\n\n";
    echo "<BODY onload=\"init()\">\n\n";
    // Step 5: The HTML stuff begins

    echo "<TABLE id=\"Character\">\n";
    echo " <TR>\n";
    echo "  <TD class=\"Name\">" . strtoupper($character) . "</TD>\n";
    echo "  <TD class=\"Image\" rowspan=\"4\"><IMG src=\"characters/$name/$name.jpg\"></TD></TR>\n";
    echo " <TR>\n";
    echo "  <TD class=\"Series\">Series: $series</TD></TR>\n";
    echo " <TR>\n";
    echo "  <TD class=\"Story\">$story</TD></TR>\n";
    // Step 6: Write the variables to the file

    echo " <TR>\n";
    echo "  <TD style=\"vertical-align: top\">\n";
    echo "  <TABLE id=\"$group\">\n";

    function moverw ($move) {
        $subfollow = "FOLLOW>>";
        $follow = "FOLLOW>";
        if (strpos($move,$subfollow) !== false)
            $move .= " (followup)";
        else if (strpos($move,$follow) !== false)
            $move .= " (followup)";
        $move = str_replace("FOLLOW>>", "<IMG class=\"Followup\" src=\"images/follow.png\" style=\"text-indent: 5px\"> ", $move);
        $move = str_replace("FOLLOW>", "<IMG class=\"Followup\" src=\"images/follow.png\"> ", $move);
        return ($move);
    }

    function descrw ($desc,$name) {
        $result = explode(",", $desc);
        $start = $result[0];
        $length = $result[1];

        $file = new SplFileObject("characters/$name/$name.txt");

        $result = "";
        for ($i = $start; $i <= ($start+$length); $i++) {
            $file->seek($i-1);
            $result .= $file;
        }
        return($result);
    }

    function commrw ($command) {
        $command = str_replace("AM,", "<IMG title=\"Assist Mode\" src=\"images/assist.gif\"> ", $command);
        $command = str_replace("SM,", "<IMG title=\"Single Mode\" src=\"images/single.gif\"> ", $command);
        $command = str_replace("BRAWL,", "<IMG title=\"Super Smash Brawl\" src=\"images/brawl.png\"> ", $command);
        $command = str_replace("uf,", "<IMG class=\"Arrow\" src=\"images/uf.png\">", $command);
        $command = str_replace("df,", "<IMG class=\"Arrow\" src=\"images/df.png\">", $command);
        $command = str_replace("ub,", "<IMG class=\"Arrow\" src=\"images/ub.png\">", $command);
        $command = str_replace("db,", "<IMG class=\"Arrow\" src=\"images/db.png\">", $command);
        $command = str_replace("u,", "<IMG class=\"Arrow\" src=\"images/u.png\">", $command);
        $command = str_replace("f,", "<IMG class=\"Arrow\" src=\"images/f.png\">", $command);
        $command = str_replace("d,", "<IMG class=\"Arrow\" src=\"images/d.png\">", $command);
        $command = str_replace("b,", "<IMG class=\"Arrow\" src=\"images/b.png\">", $command);
        $command = str_replace("+", "<IMG class=\"Plus\" src=\"images/plus.png\">", $command);
        $command = str_replace("AP", "<IMG class=\"Button\" src=\"images/punch.png\">", $command);
        $command = str_replace("AK", "<IMG class=\"Button\" src=\"images/kick.png\">", $command);
        $command = str_replace("A1", "<IMG class=\"Button\" title=\"Assist 1\" src=\"images/assist1.png\">", $command);
        $command = str_replace("A2", "<IMG class=\"Button\" title=\"Assist 2\" src=\"images/assist2.png\">", $command);
        $command = str_replace("AB", "<IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\">", $command);
        $command = str_replace("LP", "<IMG class=\"Button\" title=\"Jab\" src=\"images/jab.png\">", $command);
        $command = str_replace("LK", "<IMG class=\"Button\" title=\"Short\" src=\"images/short.png\">", $command);
        $command = str_replace("MP", "<IMG class=\"Button\" title=\"Strong\" src=\"images/strong.png\">", $command);
        $command = str_replace("MK", "<IMG class=\"Button\" title=\"Forward\" src=\"images/forward.png\">", $command);
        $command = str_replace("HP", "<IMG class=\"Button\" title=\"Fierce\" src=\"images/fierce.png\">", $command);
        $command = str_replace("HK", "<IMG class=\"Button\" title=\"Roundhouse\" src=\"images/roundhouse.png\">", $command);
        $command = str_replace("FOLLOW>>", "<IMG class=\"Followup\" src=\"images/follow.png\" style=\"text-indent: 5px\"> ", $command);
        $command = str_replace("FOLLOW>", "<IMG class=\"Followup\" src=\"images/follow.png\"> ", $command);
        $command = str_replace("degrees", "&deg;", $command);
        return ($command);
    }

    function typerw ($type) {
        $type = str_replace("BUG.", "<IMG src=\"images/bug.png\">", $type);
        $type = str_replace("CYBER.", "<IMG src=\"images/cyber.png\">", $type);
        $type = str_replace("DARK.", "<IMG src=\"images/dark.png\">", $type);
        $type = str_replace("DRAGON.", "<IMG src=\"images/dragon.png\">", $type);
        $type = str_replace("ELECTRIC.", "<IMG src=\"images/electric.png\">", $type);
        $type = str_replace("FAIRY.", "<IMG src=\"images/fairy.png\">", $type);
        $type = str_replace("FIGHTING.", "<IMG src=\"images/fighting.png\">", $type);
        $type = str_replace("FIRE.", "<IMG src=\"images/fire.png\">", $type);
        $type = str_replace("FLYING.", "<IMG src=\"images/flying.png\">", $type);
        $type = str_replace("GHOST.", "<IMG src=\"images/ghost.png\">", $type);
        $type = str_replace("GRASS.", "<IMG src=\"images/grass.png\">", $type);
        $type = str_replace("GROUND.", "<IMG src=\"images/ground.png\">", $type);
        $type = str_replace("ICE.", "<IMG src=\"images/ice.png\">", $type);
        $type = str_replace("LIGHT.", "<IMG src=\"images/light.png\">", $type);
        $type = str_replace("NORMAL.", "<IMG src=\"images/normal.png\">", $type);
        $type = str_replace("PSYCHIC.", "<IMG src=\"images/psychic.png\">", $type);
        $type = str_replace("STEEL.", "<IMG src=\"images/steel.png\">", $type);
        $type = str_replace("WATER.", "<IMG src=\"images/water.png\">", $type);
        return ($type);
    }

    function noterw ($note) {
        $note = str_replace(".AM.", "<IMG title=\"Assist Mode\" src=\"images/assist.gif\"> ", $note);
        $note = str_replace(".SM.", "<IMG title=\"Single Mode\" src=\"images/single.gif\"> ", $note);
        $note = str_replace(".HP.", "<IMG class=\"HiddenPower\" title=\"Hidden Power\"src=\"images/hiddenpower.png\">", $note);
        return ($note);
    }

    foreach ($json["character"][0]["moves"] as $i) {

        if ($i["flag"] == "normal") {
            $move = strtoupper($i["move"]);
            $desc = $i["desc"];
            $command = $i["comm"];
            $type = $i["type"];
            $note = $i["note"];
            echo "   <TR>\n";
            echo "    <TD class=\"MoveName Normal\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
            echo "    <DIV class=\"Details\">" . descrw($desc,$name) . "</DIV></TD>\n";
            echo "    <TD class=\"Command\">" . commrw($command) . "</TD>\n";
            echo "    <TD class=\"Type\">" . typerw($type) . "</TD>\n";
            echo "    <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
        }
        if ($i["flag"] == "special") {
            $move = strtoupper($i["move"]);
            $command = $i["comm"];
            $type = $i["type"];
            $note = $i["note"];
            echo "   <TR>\n";
            echo "    <TD class=\"MoveName Special\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
            echo "    <DIV class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
            echo "    <TD class=\"Command\">" . commrw($command) . "</TD>\n";
            echo "    <TD class=\"Type\">" . typerw($type) . "</TD>\n";
            echo "    <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
        }
        if ($i["flag"] == "hyper") {
            $move = strtoupper($i["move"]);
            $command = $i["comm"];
            $type = $i["type"];
            $note = $i["note"];
            echo "   <TR>\n";
            echo "    <TD class=\"MoveName Hyper\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
            echo "    <DIV class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
            echo "    <TD class=\"Command\">" . commrw($command) . "</TD>\n";
            echo "    <TD class=\"Type\">" . typerw($type) . "</TD>\n";
            echo "    <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
        }
        if ($i["flag"] == "final") {
            $move = strtoupper($i["move"]);
            $command = $i["comm"];
            $type = $i["type"];
            $note = $i["note"];
            echo "   <TR>\n";
            echo "    <TD class=\"MoveName Final\"><A href=\"javascript:;\" onclick=\"toggle_visibility(this);\">" . moverw($move) . "</A>\n";
            echo "    <DIV id=\"Final\" class=\"Details\">" . $i["desc"] . "</DIV></TD>\n";
            echo "    <TD class=\"Command\">" . commrw($command) . "</TD>\n";
            echo "    <TD class=\"Type\">" . typerw($type) . "</TD>\n";
            echo "    <TD class=\"Note\">" . noterw($note) . "</TD></TR>\n";
        }
    }
    // Step 7: Write the moves. When Command and Type are parsed, execute respective rewriters. Saves me from typing lots, hun ;)

    echo "  </TABLE></TD></TR>\n";
    echo " <TR>\n";
    echo "  <TD colspan=\"2\">\n";
    echo "  <UL id=\"tabs\">\n";
    echo "  <LI><A href=\"#Single\" onclick=\"showTab()\">SINGLE MODE</A></LI>\n";
    echo "  <LI><A href=\"#Assist\" onclick=\"showTab()\">ASSIST MODE</A></LI>\n";
    echo "  <LI><A href=\"#Combo\" onclick=\"showTab()\">COMBO PROFILE</A></LI>\n";
    echo "  <LI><A href=\"#Attack\" onclick=\"showTab()\">ATTACK PROFILE</A></LI></UL>\n";
    echo "  <DIV class=\"tabContent\" id=\"Single\">\n";
    echo "  <TABLE id=\"Single\" border=1>\n";
    echo "   <TR>\n";
    echo "    <TH>Snapback:</TH>\n";
    echo "    <TD>$snapback</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Interrupt:</TH>\n";
    echo "    <TD>$interrupt</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Hidden Power:</TH>\n";
    echo "    <TD>$hiddenpow</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH class=\"Header Rumble\"colspan=\"2\">RUMBLE CHART</TH></TR>\n";
    echo "   <TR>\n";
    echo "    <TD colspan=\"2\"></TH></TR></TABLE></DIV>\n";
    echo "  <DIV class=\"tabContent\" id=\"Assist\">\n";
    echo "  <TABLE id=\"Assist\" border=1>\n";
    echo "   <TR>\n";
    echo "    <TH>Disrupt:</TH>\n";
    echo "    <TD>$disrupt</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH colspan=2>Assist Attacks</TH></TR>\n";
    echo "   <TR>\n";
    echo "    <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"></TH>\n";
    echo "    <TD>$assist1</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"><IMG class=\"Plus\" src=\"images/plus.png\"><IMG class=\"Button\" title=\"Fierce\" src=\"images/fierce.png\"></TH>\n";
    echo "    <TD>$assist2</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH><IMG class=\"Button\" title=\"Assist\" src=\"images/assist.png\"><IMG class=\"Plus\" src=\"images/plus.png\"><IMG class=\"Button\" title=\"Roundhouse\" src=\"images/roundhouse.png\"></TH>\n";
    echo "    <TD>$assist3</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Cross-Over Counter:</TH>\n";
    echo "    <TD>$counter</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH colspan=2>Super Smash Melee</TH></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Bros. Attack:</TH>\n";
    echo "    <TD>$bros</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Party Attack:</TH>\n";
    echo "    <TD>$party</TD></TR></TABLE></DIV>\n";
    echo "  <DIV class=\"tabContent\" id=\"Combo\">\n";
    echo "  <TABLE id=\"Combo\" border=1>\n";
    echo "   <TR>\n";
    echo "    <TH colspan=2>???</TH></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Ground:</TH>\n";
    echo "    <TD>$ground</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Jump:</TH>\n";
    echo "    <TD>$jump</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Super Jump:</TH>\n";
    echo "    <TD>$superjump</TD></TR>\n";
    echo "   <TR>\n";
    echo "    <TH colspan=2></TH></TR>\n";
    echo "   <TR>\n";
    echo "    <TH>Launcher:</TH>\n";
    echo "    <TD>$launcher</TD></TR>\n";

    if ($altlaunch != null) {
        echo "   <TR>\n";
        echo "    <TH>Alternate Launch:</TH>\n";
        echo "    <TD>$altlaunch</TD></TR>\n";
    }

    echo "   <TR>\n";
    echo "    <TH>AC Finishers:</TH>\n";
    echo "    <TD>$acfinisher</TD></TR></TABLE></DIV>\n";
    echo "  <DIV class=\"tabContent\" id=\"Attack\">\n";
    echo "  $test 4\n";
    echo "  </DIV>\n";
    echo "  </TD></TR></TABLE></TD></TR></TABLE>\n\n";
    echo "</BODY></HTML>";

    ?>

$name.json, where $name is the name of the character (using pikachu as demo):

    {"character":[
        {
            "name":"Pikachu",
            "group":"Pokemon",
            "series":"Pokemon",
            "story":"To-do...",
            "moves":[
                {
                    "flag":"normal",
                    "move":"Discharge",
                    "desc":"3,2",
                    "comm":"Press HP (crouch) (air)",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"normal",
                    "move":"Electric Screw",
                    "desc":"6,2",
                    "comm":"Tilt d,+HP [air]",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"normal",
                    "move":"Tackle",
                    "desc":"9,1",
                    "comm":"Tap f, , f,",
                    "type":"NORMAL.",
                    "note":"Notes"
                },
                {
                    "flag":"special",
                    "move":"Quick Attack",
                    "desc":"11,2",
                    "comm":"Tap any direction and Press AP (air)",
                    "type":"ELECTRIC.FIGHTING.",
                    "note":"Notes"
                },
                {
                    "flag":"special",
                    "move":"Thunder Jolt",
                    "desc":"14",
                    "comm":"Motion d,df,f,+AP",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"special",
                    "move":"Electro Ball",
                    "desc":"16,2",
                    "comm":"Motion d,df,f,+AP [air]",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"special",
                    "move":"Thunder",
                    "desc":"19,2",
                    "comm":"Motion f,d,df,+AP (air)",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
               {
                    "flag":"special",
                    "move":"Electric Chair",
                    "desc":"22,2",
                    "comm":"Motion f,df,d,db,b,+AP",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"hyper",
                    "move":"Thunder Shock",
                    "desc":"25,2",
                    "comm":"Motion d,df,f,+APAP (air)",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"hyper",
                    "move":"Thunder Bolt",
                    "desc":"28,1",
                    "comm":"Motion d,df,f,+AKAK (air)",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                },
                {
                    "flag":"hyper",
                    "move":"Skull Bash",
                    "desc":"30,2",
                    "comm":"Motion d,db,b,+APAP (air)",
                    "type":"ELECTRIC.FIGHTING.",
                    "note":"Notes"
                },
                {
                    "flag":"final",
                    "move":"Volt Tackle",
                    "desc":"33,2",
                    "comm":"Motion d,df,f,d,df,f,+APAP (air)",
                    "type":"ELECTRIC.",
                    "note":"Notes"
                }],
            "snapback":"Crouching Forward",
            "interrupt":"Thunder Jolt, Thunder",
            "hiddenpow":"Hidden Power Enhancements (Line per line lookup)",
            "disrupt":"Thunder",
            "assist1":"Thunder Jolt",
            "assist2":"Thunder",
            "assist3":"Quick Attack",
            "counter":"Thunder",
            "bros":"Skull Bash",
            "party":"Thunder Bolt",
            "ground":"Punch to Kick",
            "jump":"Punch to Kick",
            "superjump":"Zig Zag",
            "launcher":"Standing Roundhouse",
            "altlaunch":"Crouching Strong",
            "acfinisher":"Discharge, Electric Screw, Quick Attack, Electro Ball, Thunder, Thunder Shock, Thunder Bolt, Skull Bash"
        }
    ]}
Wammy
  • 39
  • 11
  • `6,2` is not valid JSON. A JSON array would be `[6, 2]`. – Barmar Mar 31 '15 at 04:47
  • @Barmar Do I need to put the brackets in the string of that JSON file? Right now, I have it listed as "desc":"6,2". – Wammy Mar 31 '15 at 04:50
  • 1
    It shouldn't matter how you set `$start` and `$length` when you get to the `SplFileObject`. The error message in your failing version indicates that you're trying to open `/.txt` instead of `/pikachu/pikachu.txt` – Barmar Mar 31 '15 at 04:51
  • Never mind my earlier comment, that was before you posted the code. I thought you meant that `6,2` was the contents of the JSON file. – Barmar Mar 31 '15 at 04:52
  • Although it seems like it would be better if your JSON were `"desc": [6, 2]`, then you wouldn't need to use `explode`. – Barmar Mar 31 '15 at 04:53
  • Please post the actual code in the question. The problem you're having is because you use the undefined variable `$name` when you create the `SplFileObject`, and that code isn't in the question. – Barmar Mar 31 '15 at 04:55
  • @Barmar SplFileObject seems to be the likely culprit. No matter what direction I posted, I would always get this or another similar error. In fact, I tried replacing it with file_get_contents, and I still got the same error. I'll update it with that mention. – Wammy Mar 31 '15 at 04:57
  • You need to learn about variable scoping. You either need to pass `$name` as an argument to the `descrw` function, or add `global $name` to the function. – Barmar Mar 31 '15 at 04:58
  • @Barmar I added a link to the Sandbox Test Main Page. You'll see it as testfail.php Source Code. It's a text file. – Wammy Mar 31 '15 at 05:01
  • I've read the code. You need to post it HERE for this to be a properly-written question. – Barmar Mar 31 '15 at 05:06
  • I just tried the failed version again, now I'm not getting an error message. What did you do to it? – Barmar Mar 31 '15 at 05:19
  • Why are you doing all this with flat files? If you have some web service that allows you to store line lookup information for particular `$name` values, why not just store the related content in that same data store rather than having to look up in a flat file somewhere? In other words, why not just have the output of the call to `characters/$name/$name.json` just give you that data you are looking for? – Mike Brant Mar 31 '15 at 20:33
  • @MikeBrant You mean like MySQL? I don't have privileges to access this program. – Wammy Mar 31 '15 at 20:56
  • @Wammy There are a number of possible solutions (including database-based solutions). At a minimum, if you wanted to stick with file-based solution, why not set up files to more directly access your data. In other words, why lookup coordinates in one file to use to look up the actual content in another file? Your data structure seems flawed. – Mike Brant Mar 31 '15 at 21:06
  • @MikeBrant I do not understand what you mean. When I came up with the line-by-line seek feature, it was mainly to avoid having to write excess text files. For example, I don't want to have one text for one move, then another text for a second move. My primary concern was trying to keep the file count down: get as much information as possible with just the fewest files. I plan to have four: the image, the JSON, the text for the move descriptions, and text for extra notes. – Wammy Mar 31 '15 at 21:31
  • @Wammy But somewhere you still need to manage the mapping between the full content and where within that content are the lines for a specific character move description. So you have proliferated a bunch of JSON files for each individual character name. What you really need to do is look at the data you are interested in and think about how you need to lookup that data (i.e. by character name or whatever) and put together a data structure that that efficiently supports your lookup use cases. – Mike Brant Mar 31 '15 at 21:34
  • @MikeBrant I don't know how I would do that. I can tell you that the reason I went to the line-by-line seeking method is for fear of breaking my screen again. A year ago, I was using Notepad++ to edit some lines inside a JSON file. The edit was at the end of the line, and the line itself was very long. Once I got to the end of it, BAM - the screen turned into a mess, and I was forced to restart my computer. Since then, I've placed a limit of the number of characters I can have on the screen to prevent my screen's resolution from being broken again. – Wammy Mar 31 '15 at 21:40

1 Answers1

0

The problem is in this function, and has nothing to do with your use of JSON. It's just a simple variable scope issue.

function descrw ($desc) {
    $result = explode(",", $desc);
    $start = $result[0];
    $length = $result[1];

    $file = new SplFileObject("$name/$name.txt");
    for ($i = $start; $i <= ($start+$length); $i++) {
        $file->seek($i-1);
        echo "$file";
    }
    return(0);
}

The variable $name is not defined, so it's trying to open the file /.txt. You need to change the function so it takes $name as an argument. And since the caller of descrw expects it to return a string so that it can be concatenated, descrw shouldn't do its own echo.

function descrw ($desc, $name) {
    $result = explode(",", $desc);
    $start = $result[0];
    $length = $result[1];

    $file = new SplFileObject("$name/$name.txt");

    $result = '';
    for ($i = $start; $i <= ($start+$length); $i++) {
        $file->seek($i-1);
        $result .= $file;
    }
    return($result);
}

Then change the place where it's called to pass the argument:

echo "    <DIV class=\"Details\">" . descrw($desc, $name) . "</DIV></TD>\n";
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • (Apologies for the late reply, was fixing the code's format) That fix really helped. All that's left for me to do is revise that last part so that it appears inside the white box. If you click on the move name back in the demo, you'll see there's a white DIV. The text unfortunately was posted outside of it. I was able to fix it by revising the return code, but it only gives me one line as seen in the demo. – Wammy Mar 31 '15 at 05:29
  • `descrw` should return the string, not echo it itself, so that the concatenation will work properly. – Barmar Mar 31 '15 at 05:33
  • That did it! I've updated the PHP string for the rest of the other CSS classes (normal, special, Hyper, Final). I know very little PHP (ie: beginner stuff like echo and variable saving), but I've never done advanced stuff like this before. I'm relieved to know that S.O. is a big lifesaver. Thanks, Barmar. – Wammy Mar 31 '15 at 05:43
  • We... have a problem. The code worked on the test, but it doesn't work on the real thing. A new error popped up. I'll post the true code from my MCP root folder instead of the test one, as those will be deleted. – Wammy Mar 31 '15 at 15:30
  • Sounds like the `characters` directory isn't where you think it is. Your code expects it to be a subdirectory of the directory containing the script. – Barmar Mar 31 '15 at 17:32