0

I started to learn PHP. I have written a class and I defined an array in that.

<?php

/**
 *
 */
class page {

    public $content= "Coming soon";
    public $title="Saeb Mollayi";
    public $style ="<link rel=\"stylesheet\" type=\"text/css\" href=\"./style/style.css\">";
    public $b = array(
        "خانه"       => 'homepage.php',
        "ارتباط"     => 'contact.php',
        "خدمات"      => 'services.php',
        "نقشه سایت"  =>'sitemap.php',
    );


    function displaymenu()
    {
        echo "<table>"."\r\n";
        echo  " <tr>";
        $width=(100/count($b));

        while (list($name,$url)= each ($b))
        {
            $this -> displaybuttons ($width,$name,$url, $this -> urlbool($url));
        }
        echo "</tr>";
        echo "</table>";
    }

    function ncontent($newcontent)
    {
        $this-> content = $newcontent ;
    }

    function ntitle($newtitle)
    {
        $this -> title = $newtitle ;
    }

    function nbuttons($newbuttons)
    {
        $this -> b = $newbuttons ;
    }

    function display()
    {
        echo "<head>";
        echo "\r\n";

        $this -> displaytitle();
        $this -> style ;

        echo "</head>"."\r\n"."<body>"."\r\n";

        $this -> displayheader();
        $this -> displaymenu($this -> b);

        echo $this -> content ;

        $this -> displayfooter() ;
        echo "</body>"."\r\n";
    }

    function displaytitle()
    {
        echo "<title>";
        echo $this -> title ;
        echo "</title>";
    }

    function displayheader()
    {
        echo '
        <table  id="header">
            <tr id="header">
                <td id="lheader"><img src="./img/logo1.png"></td>
                <td id="cheader"> Welocme. Welcome! </td>
                <rd id="rheader"><img src="./img/logo1.png"></td>
            </tr>
        </table>
        ';
    }

    function urlbool($url)
    {
        if (strpos($_SERVER['SCRIPT_NAME'],$url)==false)
            return false;
        else return true;
    }

    function displaybuttons($width,$name,$url,$active=false)
    {
        if (!active) {
            echo "<td \" style =\"width=$width% ;\">
                <a href ='$url'>
                <img src=\".\img\top.png \" alt='$name' border='0'></a>
                <a href='$url' ><span class='menu'>$name</span></a>
                </td>";
        } else {
            echo "
                <td style='width=$width%'>
                    <img src='./img/right.png'>
                    <span class='menu'>$name</span>
                </td>";
        }
    }

    function displayfooter()
    {
        echo "&copy footer";
    }
}
?>

This is my index.php file:

<html>
<?php
    require "./class/page0.inc";
    $cc = array(
        'خانه'      => 'homepage.php',
        'ارتباط'    => 'contact.php',
        'خدمات'     => 'services.php',
        'نقشه سایت' =>'sitemap.php',
    );

    $ppage = new page();
    $ppage-> nbuttons(array(
        'خانه' => 'homepage.php',
        'ارتباط'=> 'contact.php',
        'خدمات' => 'services.php',
        'نقشه سایت' =>'sitemap.php',
    ));
    $ppage -> ncontent('this is content'."<br>");
    $ppage -> display();
?>
</html>

But when I run it I see these errors:

Notice: Undefined variable: b in /opt/lampp/htdocs/all/test3/class/page0.inc on line 14

Warning: Division by zero in /opt/lampp/htdocs/all/test3/class/page0.inc on line 14

Warning: Variable passed to each() is not an array or object in /opt/lampp/htdocs/all/test3/class/page0.inc on line 15

Where i did do something wrong? What is my fault?

5
  • 1
    possible to clean up and indent some for readabilities sake? Commented Feb 11, 2016 at 19:27
  • You are invoking $this -> displaymenu($this -> b); with a parameter - $this -> b, but your function declaration does not have a param -> function displaymenu(){...}. You need to change it to function displaymenu( $b ){...} Commented Feb 11, 2016 at 19:33
  • 1
    "What is my fault?" - If this happens the first time... not knowing the rules (for accessing member variables) - failing to read the documentation or a book. If this happens again: lazyness. Commented Feb 11, 2016 at 20:10
  • @KarolyHorvath made my day. Commented Feb 11, 2016 at 20:32
  • :D im working in my english and php Commented Feb 12, 2016 at 15:33

2 Answers 2

2

Text in errors in readable by humans, they means:

  1. Variable $b does not exist in your function on line 14 in page.inc file.
  2. Because $b does not exists, PHP function "count" will return 0 and you have division by zero.
  3. You are passing variable $b to a function "each", $b does not exists and it is not an array or object.

If you have a variable $b inside class, use $this->b instead to access to it.

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

Comments

1

I don't know if it's because in the copy/paste something went wrong, but editing your post I found out that there are a lot of syntax errors.

First of all those spaces in the objects function calls, where you have unnecessary spaces.

The arrow should not have any space like $this -> displaymenu($this -> b);, it has to be $this->displaymenu($this->b);.

In addition $b must be referenced using $this->b, that's why you get that error about dividing by zero.

2 Comments

tnx . i must use $this->b every where i have $b in function. tnx
You're welcome! If this was your solution it would be kind if you select it as your answer.

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.