Array - echo and php

Hi Everyone,

I try to create a very simple labyrinth game with php. I set up PHP array for the Cardinal directions (North, East, South and West). Here’s an example :

$nord=array(0=>-1, 1=>-1, 2=>-1, 3=>-1, 4=>-1,);

the same for enigmas
$enigmes=array(
0=>"…",
1=>"",
);

Here’s the code, the player enters the room $piece (room), click on the direction to reach the next room and so on.
$piece = 0;
if(isset($_GET[“numpiece”])) {
$piece = $_GET[“numpiece”];
}
if($sud[$piece] !== -1) {
echo ‘SUD’;

translation piece=room ; sud = south ; numpiece=room number.

My problem is, and I’ve tried to resolve it but in vain… I don’t know where to embed my $enigmes (enigmas) within the code above. I need to write a very short text to inform the player he has reached the room x (you reach the room # . This text must appear above the doors.

echo “Vous êtes dans la pièce … $piece[$piece]”;

Thank you so much for your help.
Cheers

Well, in one part of your post, you mentioned using " $piece = " and in another, " $piece[$piece] " …
Therefore, you are using the variable name $piece as a variable and also as an array. Not sure what you are
trying to do there.

I did a lot of game theory in my younger days. One problem I always ran into is the layout of the rooms in the
game. With a little thought in the designing of the game, I found moving around in a game was much simpler.

The two ways to move the player thru a game is either use a two-dimensional room layout using X and Y
coordinates to keep track of the current room or keep a list of each room with where the user may move to.
The first is much easier. A simple two-dimensional array ($room[row][col]…) can make moving from room
to a new room with ease! You can move SUD (South) just by adding 1 to the row index. You will need to
set limits and check for them. (So that you do not go off the end of your world.)

Not sure if this helps or confuses you. I just find that flat arrays for rooms make programming adventure
games much harder to control…

Thank you for your answer.

I understand… but you know, I begin with php… This game is an exercice (mooc).
We have to create a variable $nord (north) etc. and $enigmes (enigmas) using “array”. Then display the value of these variables for the current room : variable $piece.

Examples given are $north = array(0=>, etc…

So, this is for a school project not a real live game project. Either way, it should be simple to create.
But, you will need to know all the possible ways your player can move. And, all the directions. Here are some
comments that might help you…

If you have a variable array of the names of the rooms or descriptions of the room similar to
$room_names=array(“room1”, “room2”, “r3”); you would just use the current room and display the name of
it. Therefore, if the current room is $piece, you can use the names of the rooms like this:
echo $room_names[$piece]; But, for a room to show four room descriptions for a player to enter, you would
need to create a two-dimensional array with the current room’s info and where they can go. One simple way to
do this would be to create a list of the rooms with the places they can go. Then use the same display to show
them on the page. Something like this would work:
$room_exits = array();
$room_exits[1][1] = 0; // Room#1 North = no exit…
$room_exits[1][2] = 2; // Room#1 Right = room #2
$room_exits[1][3] = 3; // etc…
$room_exits[1][4] = 0; // etc…
$room_exits[2][1] = 33; // Room#2 North = Room #33
As you see, you have to design how rooms may move thru exits and where they can go. In this example, I
made a two-dimensional array with the room number in the first dimension, the direction coded as 1,2,3,4 and
then the exit value of the room you can go to by exiting in that direction. So, moving from room #1 using the
$room_exits array and using 2 as moving East or Right, the player would go to room #2. To create this array
and fill it up, you need to know all of your rooms and all of the exits that can be taken from that room. Some
of the exits would not go anywhere and those would be marked zero. Again as in my last post, you need to
really design this ahead and think out the possiblities. And, then to display, for example, the doorway for the
room#1 that goes East (Right) to room #2 it would be displayed like this:
echo $room_names[ $room_exists[$piece][2] ]; Shows the name of the room to the East of current room!

Not sure if this helps you or makes it more complicated. If you get further along in your project show us some
of your code and we can help you fix it. (Please put the code inside the PHP tags when posting it…)

Thank you for your answer and your precious help !
Yes it helps… and it’s much more easier.
It took me hours but I finally got done with the code .

Here’s the code :
[embed=425,349] $piece = 0;
if(isset($_GET[“numpiece”])){
$piece = $_GET[“numpiece”];
}

// Affichage de la piece en cours
echo '<p>Vous êtes dans la pièce '.$piece.'</p>' ;

echo ‘

’.$indice[$piece].’

’;
if($nord[$piece] != -1){
	echo '<a href="index.php?numpiece=',$nord[$piece],'">NORD<img src="door.png" width="50"></a>';
}[/embed]

My problem now is to center door(s) and link(s). Enigmas and text are already done. I use an external CSS file.

Do you mean center a picture of a door on the page? It is best to do that with CSS.
You could do it with <img…> , but, it is much better to use the CSS system as it is designed
to do that.

Let’s imagine that you have four doors. Place them into four

tags. Then, use CSS to set that
on
the page exactly where you want it to show up. You can set a DIV to be in a locked in position on the page.
It is quite simple. Loosely something like this: Note that you can set the locked it position of the TOP of the DIV and LEFT of the DIV to any position you want it to be at. It can be a pixel value or a percentage value. In my example, it says at the top of the page as I used top=0 and a little less than one half of the across value. So, this would center that DIV in the middle top of the screen. For lower middle, use bottom: 0; left: 45%; For right middle use top: 45%; right: 0; etc... You would use four of these DIV's with different settings for each of the door position's...

Hope that helps…

Sorry, I forgot to mention that I’m using an external CSS.

I try to center doors and links below each door . I know that php allows html tags. To do so, we close php ?>, write html tags then re-open php. Am I right ?

So I’ve tried this, but it does not work !

[embed=425,349]<?php
if($nord[$piece] != -1){ ?>

<?php echo 'NORD'; } ?>[/embed]

To open/close PHP as you say, you just do it…

Remember PHP is handled on the server. It’s output is placed into the page where it is and then the combined
HTML is sent to the browser. Something like this in HTML:

Hello, <?PHP echo "some text..."; ?>, thank you…

Would send this HTML to the browser: Hello, some text…, thank you…
This means that your code you posted can be done either with stopping and starting the PHP or just making it
into an echo line. Here is one way it could be done…
[php]

<?PHP if($nord[$piece] != -1){ echo '
'; echo 'NORD'; } ?>

[/php]
NOTE: Please notice the quotes and double-quotes. You must close pairs of them in order. You can not do
it this way: ’ "… ’ " It must be this way: ’ “…” ’ This means it is tricky sometimes to sort that issue out!
Also, to pass a variable such as $nord[$piece] to a webpage using ?numpiece= , you do not need quotes for
the value if it is a number. You only need quotes if the value contains spaces…
Lastly, note the use of a period to “concatenate” parts of the string together with data.

Hope this helps…

[embed=425,349]echo ‘

’;[/embed] you’re right. I must be very tired.

I’ve tried the echo but still doesn’t work. May be I can try

only by closing and reopening ?> and <?php.

I tried something new :
[embed=425,349]<?php
$piece = 0;
if (isset($_GET[“numpiece”])) {
$piece = $_GET[“numpiece”];
}
?>

<?php echo "

{$indice[$piece]}

"; ?>
<?php if ($nord[$piece] != -1) { echo "
NORD"; } ?>[/embed]

No result.
Here’s the full code in attachments as you can see, it’s a simple and basic game.


Corrections.pdf (17.5 KB)

[php]if($nord[$piece] != -1){
echo ‘NORD’;
}[/php]

have you tried

[php]if($nord[$piece] != -1){
echo ‘NORD’;
}[/php]

I know , is supposedly saves space when php interrupts it, but in the past has caused me problems, so I just stick with the period (.). This probably isn’t the problem, but it would hurt to try?

I know , is supposedly saves space when php interrupts it, but in the past has caused me problems, so I just stick with the period (.). This probably isn’t the problem, but it would hurt to try?

I’ve already tried…
Never mind, i will try something help.
thank you anyway !

Well, danae, I changed all of your commas to periods and your code works on my server.
It allows me to move east to west and back again.

I think it works as you want it to. (But, not with those commas!)

Indeed ErnieAlex, it works for me too - both with commas and periods.
My problem is with CSS and php. I try to center doors and directions (below my doors). So here’s my code :

[embed=425,349]f($nord[$piece] != -1){
echo '<div id=“element1” '. $nord[$piece] . ‘">’;
echo ‘NORD’;
}[/embed]
I want my doors and directions aligned horizontally with links under.
To separate the directions from my doors do I need to exclude <img src=“door.png”… from the echo ’ <a href="…"… ?

Well, if you think of the page as a canvas and you place four

's on the page, they can be placed anywhere
on the page. But, normally, they are placed one after the other down the page. To fix this, you need to use the
“position” argument and tell the DIV that it is to be positioned in a fixed location on the page. Once you do that,
it will stay on the page where you want it.

Also, anything that you place in one of the DIV’s will be grouped together. So, the link will stay with the image if
you group them together in one DIV. I explained this earlier in the thread… If you want to just place them under
the rest of your display, you can place them into a table. The table would be 3x3 cells where you would use the
four in the middle of each side. Or, you can place on them on the page at the edges of all of the page like this:
[php]
if($nord[$piece] != -1){
echo ‘

’;
echo ‘NORD’;
echo ‘
’;
}
[/php]
You will need to do this for each direction. Test this by trying just this one room and moving south (sud) to
see it displayed. I used three echo’s to make it easier for you to read, but, could be just one…

Hope that helps…

I tried yesterday : doors and links were positioned on top.

Now everything is fine, it’s almost done. I use an external css file, it’s much better, I think.
My problem is the position of my doors and links. As you know, I want my links placed under the doors. But as they are all grouped together in one DIV, I need to place my png out of the div : your notes are so helpful, really. Table ? I do not how to manage this [embed=425,349]echo ’

'; [/embed] ? tr, td ?

Links :[embed=425,349]if($nord[$piece] != -1){
echo “<div id = 'echo2’NORD”;
}[/embed]. They are vertically centered not horizontally !
I tried the same thing with my image echo ’ <img src=“door.png” … it doesn’t work, I have a white page.

A table will be the best way, I think but it means I have to redo my the work !

I try to create a table :

[embed=425,349][embed=425,349]if($nord[$piece] != -1){
// Open the table
echo “

”;
echo “”;
echo “”;
echo ‘’;
echo “”;
// Close the table
echo “
NORD<img src=“door.png”’ . $nord[$piece] . ‘" alt=“door”>
”;
}[/embed]
[/embed]
I did this for each direction. Doors and links still not horizontally aligned. Links now are centered but above each door…
May be I need to create a [embed=425,349]
[/embed] ?

Change this section to the following… Note that you do not want to use

as it is a separate cell in a table.
You normally use the (table header) for long cols of data. For your use, just make it normal text and if you
want special formatting for it (such as colored backgrounds or bold text), then use CSS to make it pretty…

[php]
echo “

”;
echo “”;
echo “<img src='door.png alt=“door”>
”;
echo “NORD”;
echo “”;
echo “”;
[/php]
With this code, you have a table row. In that row, you have one cell square. In that cell, you have both the
image and the text for that one door. This example places the door at the top, uses
to move below the
picture and places the text under the door. You would reverse this to make the text above the door. For the
left-right door’s you would remove the
and place the text next to the image. I feel it is easier using the
DIV version I gave you before, but, the table is good, too. Either will work for you…

Why in the world are you echo’ing HTML?

[php]<?php if($nord[$piece] != -1): ?>

NORD door
<?php endif; ?>[/php]

I’ve tried this code : I’m getting a 500 server error message or a white page with safari !!

Is this actually supposed to be minus one?

!= [size=14pt]-[/size]1

Sponsor our Newsletter | Privacy Policy | Terms of Service