2

I have index.php file and it contains:

1.class eachObject

class eachObject
{
    function outPut()
    {
        echo '<td>&nbsp;</td>';
    }
}

2.class wall

class wall extends eachObject
{
    function outPut()
    {
        echo '<td class="wall">&nbsp;</td>';
    }
}

3.class blank

class blank extends eachObject
{
    function outPut()
    {
        echo '<td class="blank">&nbsp;</td>';
    }
}

I got instances from them:

$wall    = new wall();
$blank   = new blank();

and I have an array called room including wall and blank:

$room     = array();    
$room[0] = array($wall, $wall, $blank, $wall, $blank);

and then I will use it in a table to show walls and blanked areas:

<html> 
<body>
<?php
    echo '<table>';
    foreach ($room as $row) {
        echo '<tr>';
        foreach ($row as $tool) {
            $tool->outPut();
        }
        echo '</tr>';
    }
    echo '</table>';
?>
</body>
</html>

The question is: how can I change this array in another php file called test.php to have:

$room[0] = array($wall, $wall, $wall, $wall, $blank);

As you can see, the third value changed from $blank to $wall.

and then when I refresh the div which contains the table, I will have a wall instead of blank area.

4
  • 1
    What do you mean by another PHP file? Commented Sep 29, 2017 at 14:31
  • @LajosArpad thanks for your question. I mean that I have all my classes and array and instances in index.php file and I want to change a value (or different values) in my array in test.php file. Commented Sep 29, 2017 at 14:35
  • Are you including requiring the other file in index.php, that is, do you have something like require_once("test.php")? Commented Sep 29, 2017 at 14:40
  • @LajosArpad, yes, I have! Commented Sep 29, 2017 at 14:56

3 Answers 3

2

Since test.php is included/required to index.php, $rooms can be modified there using

$rooms[0][3] = $wall;

but note that this is only possible if both $rooms and $wall are reachable. If you declare them as globals before you include/require test.php and after you include/require index.php you do some changes for $room[0], then it should work and you should have a new value. From your description it seems that you either included/required test.php before $rooms and/or $wall is defined, or the variables are out of context, for instance, inside a function. You will need to make sure that the variables are reachable in the other file and when the other file starts to use them, they are already declared. However, you might want to rethink the way your code is structured and use some ideas like MVC. If this answer is not enough for you to solve your problem, then you will need to add more details about your code.

Sign up to request clarification or add additional context in comments.

Comments

0

Create a functional construct (class with methods, function, ...) that first deletes your old test.php file, then creates a new one, adding line by line the content you wanted to add. (See the Php filesystem functions for such an application)

That is a way that works, but it is not a way I would recommend. (But I do not know your project, I might not see the whole image) Better would be a way using a serialised array. So you serialise your new array, save that string to your file and each time you want to use your array, just deserialise the string in your file. And last method: use a database, if possible.

Comments

-1

you can change the value per $room[0][3] = $wall;

2 Comments

Huh? Was the OPs question? I don't think so...read question carefully
@tom, I have mentioned in another php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.