Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: "Inventory" for HTML RPG  (Read 43587 times)

0 Members and 1 Guest are viewing this topic.

Dilbert

    Topic Starter
  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    Re: "Inventory" for HTML RPG
    « Reply #15 on: March 22, 2006, 04:26:57 PM »
    Instructions followed. Output is still:

    <html>
     <body>
     <?php
       // If php is running correctly, this will display "Hello world!"
       echo '<p>Hello world!</p>';
     ?>
     </body>
     </html>

    Here's what I did:

    Downloaded the .zip, installed it.

    Modified the .ini file to point to Apache.

    Set up Apache to go to C:/PHP.

    [edit]Riiiiiiiiight. I seem to have accidentally put both recommended and the other INI file in the PHP folder and left them there. I deleted the non-recommended one, and now output is as normal. At least, no HTML is unparsed. Now all that's left is to learn how to set up variables that go outside of individual pages. If I learn how to set up multipage variables, I'll be all set. It'll make it easy to add even more new features (if I think of any. Rarely do I attempt such ambitious improvements at once.)[/edit]

    [edit]OK, I found the 'include' function. That carries over variables... but also the whole page. I just want the variables... any way to do this?[/edit]
    « Last Edit: March 22, 2006, 11:21:30 PM by Timothy_Bennett »
    "The geek shall inherit the Earth."

    Rob Pomeroy



      Prodigy

    • Systems Architect
    • Thanked: 124
      • Me
    • Experience: Expert
    • OS: Other
    Re: "Inventory" for HTML RPG
    « Reply #16 on: March 22, 2006, 11:12:15 PM »
    Read about Sessions.
    Only able to visit the forums sporadically, sorry.

    Geek & Dummy - honest news, reviews and howtos

    Dilbert

      Topic Starter
    • Moderator


    • Egghead

    • Welcome to ComputerHope!
    • Thanked: 44
      Re: "Inventory" for HTML RPG
      « Reply #17 on: March 22, 2006, 11:59:17 PM »
      Well, I'm trying, anyway. I have two pages, test1.php and test2.php. After being completely baffled by your link, I Googled "PHP Sessions" and learned... not enough, apparently. There's so many different methods people have on the web that I am a little perplexed. I tried a few things, and I'm stuck now.

      I have the following code right now:

      Code: [Select]
      <!-- test1.php -->
      <html>
      <head>
      <title>Test page for PHP</title>
      </head>
      <body>
      <?php
      session_start
      ("healthmeter");
      $_SESSION["health"] = 100;
      echo 
      $health;
      ?>



      <p><a href="test2.php">Take off 25% health</a></p>
      </body>
      </html>

      Code: [Select]
      <!-- test2.php -->
      <html>
      <head>
      <title>Test page for PHP</title>
      </head>
      <body>
      <?php
      session_start
      ()
      $_SESSION["health"] -= 25;
      echo 
      $health
      ?>


      </body>
      </html>

      You can probably already see issues. My output is:

      Quote
      Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\test1.php:7) in C:\Program Files\Apache Group\Apache2\htdocs\test1.php on line 8

      Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\test1.php:7) in C:\Program Files\Apache Group\Apache2\htdocs\test1.php on line 8


      Take off 25% health

      Quote
      Parse error: syntax error, unexpected T_VARIABLE in C:\Program Files\Apache Group\Apache2\htdocs\test2.php on line 9

      OK, I know what a syntax error on test2.php means. I goofed, but I don't know where. Also, I have no idea what those errors are on test1.php. What am I doing wrong in my code?
      "The geek shall inherit the Earth."

      Rob Pomeroy



        Prodigy

      • Systems Architect
      • Thanked: 124
        • Me
      • Experience: Expert
      • OS: Other
      Re: "Inventory" for HTML RPG
      « Reply #18 on: March 23, 2006, 03:29:52 AM »
      Okay, the first session problem is because you have already sent some output to the browser.  The session_start stuff must be at the beginning of your script; i.e. before the <html> tag.  The problems in the second script arise from you forgetting to use semicolons.  You can get away with that in javascript, but not in php.
      Only able to visit the forums sporadically, sorry.

      Geek & Dummy - honest news, reviews and howtos

      Dilbert

        Topic Starter
      • Moderator


      • Egghead

      • Welcome to ComputerHope!
      • Thanked: 44
        Re: "Inventory" for HTML RPG
        « Reply #19 on: March 23, 2006, 08:15:03 AM »
        OK, I moved the entire PHP code to the top because it made no sense to see session_start outside of PHP tags, and then ehen I moved the opening PHP tag it included things like <title> and <head>. If that's wrong, let me know:

        Code: [Select]
        <!-- test1.php -->
        <?php
        session_start
        ("healthmeter");
        $_SESSION["health"] = 100;
        echo 
        $health?>

        <html>
        <head>
        <title>Test page for PHP</title>
        </head>
        <body>

        <p><a href="test2.php">Take off 25% health</a></p>
        </body>
        </html>

        Code: [Select]
        <!-- test2.php -->
        <?php
        session_start
        ();
        $_SESSION["health"] -= 25;
        echo 
        $health?>

        <html>
        <head>
        <title>Test page for PHP</title>
        </head>
        <body>


        </body>
        </html>

        OUTPUT:

        Quote
        Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\test1.php:2) in C:\Program Files\Apache Group\Apache2\htdocs\test1.php on line 3

        Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\test1.php:2) in C:\Program Files\Apache Group\Apache2\htdocs\test1.php on line 3

        Quote
        Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\test2.php:2) in C:\Program Files\Apache Group\Apache2\htdocs\test2.php on line 3

        Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\Apache Group\Apache2\htdocs\test2.php:2) in C:\Program Files\Apache Group\Apache2\htdocs\test2.php on line 3

        This has to be a case of "imaboneheaditis" but it just makes little to no sense to me.
        "The geek shall inherit the Earth."

        Rob Pomeroy



          Prodigy

        • Systems Architect
        • Thanked: 124
          • Me
        • Experience: Expert
        • OS: Other
        Re: "Inventory" for HTML RPG
        « Reply #20 on: March 23, 2006, 01:04:13 PM »
        Try this:

        <?php
        session_start("healthmeter");
        $_SESSION["health"] = 100;
        ?>
        <html>
        <head>
        <title>Test page for PHP</title>
        </head>
        <body>

        <p><a href="test2.php">Take off 25% health[/url]</p>
        <?php
        echo $health;
        ?>
        </body>
        </html>

        and this:
        <?php
        session_start();
        $_SESSION["health"] -= 25;?>
        <html>
        <head>
        <title>Test page for PHP</title>
        </head>
        <body>

        <?php
        echo $health;
        ?>
        </body>
        </html>


        In other words, ditch those comments at the start of the files, which are in fact causing output (and headers) to be sent to the browser.  ANd put the echo command within php tags in the correct place in the html.
        Only able to visit the forums sporadically, sorry.

        Geek & Dummy - honest news, reviews and howtos

        Dilbert

          Topic Starter
        • Moderator


        • Egghead

        • Welcome to ComputerHope!
        • Thanked: 44
          Re: "Inventory" for HTML RPG
          « Reply #21 on: March 23, 2006, 04:38:50 PM »
          OK, I C&P'ed the code, deleting the extra lines caused by Copy and Paste ( >:() and now I don't get any error messages, but the echo command doesn't seem to be working. It doesn't show the variable. Now, I tried several methods of showing it. First, I tried enclosing it in quotes. The phrase $health appeared, but when I took out the quotes there was nothing. So, thinking it was possibly an HTML problem, I enclosed the echo PHP in <p> tags, so it read:

          <p><?php  
           echo $health;
           ?> </p>

          It stayed blank. I think it's working, I just can't see the results on the page.
          "The geek shall inherit the Earth."

          Rob Pomeroy



            Prodigy

          • Systems Architect
          • Thanked: 124
            • Me
          • Experience: Expert
          • OS: Other
          Re: "Inventory" for HTML RPG
          « Reply #22 on: March 24, 2006, 12:12:45 AM »
          No sorry, of course it doesn't - I didn't pay close enough attention to your code.  The variable is $_SESSION["health"], not $health.
          Only able to visit the forums sporadically, sorry.

          Geek & Dummy - honest news, reviews and howtos

          Dilbert

            Topic Starter
          • Moderator


          • Egghead

          • Welcome to ComputerHope!
          • Thanked: 44
            Re: "Inventory" for HTML RPG
            « Reply #23 on: March 24, 2006, 12:23:07 AM »
            You're on a roll. That's two major features I am now capable of implementing. Needless to say, it works perfectly.

            Hmm... that's one of those things both of us should have caught, right? But who's made the bigger mistake: The guy that misreads a section of code or the guy that implements changes he has a nagging feeling that he just knows don't work right, then think up every conceivable solution except the one that works?

            Whatever. It works fine now. I'd put it in my game but I'm sleepy and really should be getting to bed. (It's 11:23 PM in my time zone)
            "The geek shall inherit the Earth."

            Rob Pomeroy



              Prodigy

            • Systems Architect
            • Thanked: 124
              • Me
            • Experience: Expert
            • OS: Other
            Re: "Inventory" for HTML RPG
            « Reply #24 on: March 24, 2006, 02:36:12 AM »
            Quote
            Hmm... that's one of those things both of us should have caught, right?  But who's made the bigger mistake: The guy that misreads a section of code or the guy that implements changes he has a nagging feeling that he just knows don't work right, then think up every conceivable solution except the one that works?
            Cheeky monkey!  In my defence, I was only debugging your specific errors, not checking your code for correct functioning. :P

            There's one thing that you can do to improve readability - create a reference to the session variable, thus:

            $health = &$_SESSION['health'];


            Changes then to $health will change the session variable, so you can just use $health from that point on within the script.
            Only able to visit the forums sporadically, sorry.

            Geek & Dummy - honest news, reviews and howtos

            Dilbert

              Topic Starter
            • Moderator


            • Egghead

            • Welcome to ComputerHope!
            • Thanked: 44
              Re: "Inventory" for HTML RPG
              « Reply #25 on: March 24, 2006, 10:06:32 AM »
              Quote
              Quote
              Hmm... that's one of those things both of us should have caught, right?  But who's made the bigger mistake: The guy that misreads a section of code or the guy that implements changes he has a nagging feeling that he just knows don't work right, then think up every conceivable solution except the one that works?
              Cheeky monkey!  In my defence, I was only debugging your specific errors, not checking your code for correct functioning. :P

              There's one thing that you can do to improve readability - create a reference to the session variable, thus:

              $health = &$_SESSION['health'];


              Changes then to $health will change the session variable, so you can just use $health from that point on within the script.

              Actually, I was trying to show how I goofed, not you. Sorry if I mistyped, it was late, I was about to fall asleep on my desk. (It would have been a disaster to wake up with keyboard-face) Please don't take what I said the wrong way. I mean no insult. :D

              [edit]BTW, I looked up the w3school for SQL, and it seems useful. My last database was in Access, but this looks fairly simple. Do you think it would be a good idea to use PHP for health and SQL for inventory? The idea's looknig better and better.[/edit]

              [edit]Wait. If I used a database, then if two people were playing the game then they'd be modifying each other's inventory tables... or would they?[/edit]
              « Last Edit: March 24, 2006, 01:18:25 PM by Timothy_Bennett »
              "The geek shall inherit the Earth."

              Rob Pomeroy



                Prodigy

              • Systems Architect
              • Thanked: 124
                • Me
              • Experience: Expert
              • OS: Other
              Re: "Inventory" for HTML RPG
              « Reply #26 on: March 24, 2006, 02:44:05 PM »
              Don't worry - not offended!

              Man, if you could learn relational databases then you would be well on the way to a significant improvement on your design.  If you go down the MySQL route, then I can provide support, but be warned: relational database design is an entirely new discipline to learn, and is not the same as programming.  I've never looked for tutorials, but you might find something useful out there.  If so, try and find something that provides MySQL examples.

              SQL is not simpler than Access.  I think it is probably easier to get up and running with Access databases, because of all the hand-holding tools.  In fact when I started doing some serious programming, I used Access's query builders to help me construct SQL commands.  So it has its uses.  But SQL is faster by several orders of magnitude (in my non-rigourous impression...) and much more robust, and perfectly suited to large databases and multi-user environments.

              In your final comment you refer to two users overwriting each others inventories.  No.  You have a table of users, and a table of inventories.  You use the user's ID to link to the corresponding entries in the inventory table.

              If you install MySQL, also get hold of phpMySQLAdmin; excellent for adminstering MySQL databases in a php environment.

              Addendum: if you get the MySQL ODBC connector, you can actually use Access as a frontend to MySQL tables.  i.e. get the power and speed on MySQL queries on the tables that are stored on your MySQL server, coupled with the ease of interface design of Access.  A veteran DB programmer told me many years ago that you should always keep your data separate from your interface.  He was right.
              « Last Edit: March 24, 2006, 02:51:36 PM by robpomeroy »
              Only able to visit the forums sporadically, sorry.

              Geek & Dummy - honest news, reviews and howtos

              Dilbert

                Topic Starter
              • Moderator


              • Egghead

              • Welcome to ComputerHope!
              • Thanked: 44
                Re: "Inventory" for HTML RPG
                « Reply #27 on: March 24, 2006, 03:26:46 PM »
                I have MySQL installed. I suppose the first thing to do is get Apache hooked up to it, right? What file should I edit, and how? I have tuts for learning MySQL but I know I need to get Apache to "see" mySQL.

                « Last Edit: March 24, 2006, 04:16:32 PM by Timothy_Bennett »
                "The geek shall inherit the Earth."

                Rob Pomeroy



                  Prodigy

                • Systems Architect
                • Thanked: 124
                  • Me
                • Experience: Expert
                • OS: Other
                Re: "Inventory" for HTML RPG
                « Reply #28 on: March 24, 2006, 04:15:01 PM »
                No - it's your PHP installation that needs to see MySQL, not Apache.  Not too difficult.  If MySQL is running on your server, there isn't a lot else to do.  Get phpMyAdmin (sorry mis-spelled it before) up and running and that'll make stuff easier for you.
                Only able to visit the forums sporadically, sorry.

                Geek & Dummy - honest news, reviews and howtos

                Dilbert

                  Topic Starter
                • Moderator


                • Egghead

                • Welcome to ComputerHope!
                • Thanked: 44
                  Re: "Inventory" for HTML RPG
                  « Reply #29 on: March 24, 2006, 04:26:12 PM »
                  I can't find a place to d/l  phpMySQLAdmin. I swear, I've looked everywhere for it, and the only link I got - sourceforge - gave me a 500 error.

                  Oh, it's phpMyAdmin. One sec... got it. I'm setting it up... I did everything it told me to do. Now when I use MySQL it makes databases on my server. :)

                  [edit]OK, I have a small mySQL database. I couldn't figure out how to use that thing that mixes it with Access, but whatever. I like the prompt for reasons that baffle every sane person on Earth.

                  anyway, my inventory right now is thus: I have fields name, description, onHand, inInventory, and importance. I have two items entered and can add more. How can I make a web page see this? My biggest problem never has been figuring out how to do things (though that is hard sometimes); my main problem is linking it all up.[/edit]
                  « Last Edit: March 24, 2006, 06:39:48 PM by Timothy_Bennett »
                  "The geek shall inherit the Earth."