PHP unitialized string offset: 0
<p>Hellow fellow programmers.</p>

<p>I decided to give PHP a try, and then bought lynda.com's essential training tutorial.
The problem is, that i get this error:</p>

<blockquote>
  <p>( ! ) Notice: Uninitialized string offset: 0 in
  C:\wamp\www\widget_corp\includes\functions.php on line 147.</p>
</blockquote>

<p>when trying to compare two values.</p>

<p>Can anyone help me ? :)</p>

<p>error in navigation function:</p>

<pre><code>if($page["id"] == $selectedPage['id']){
</code></pre>

<p><strong>Class content.php below:</strong></p>

<pre><code>&lt;?php require_once("includes/connection.php"); ?&gt;
&lt;?php require_once("includes/functions.php");?&gt;
&lt;?php findSelectedPage(); ?&gt;
&lt;?php include("includes/header.php");?&gt;

&lt;table id="structure"&gt;
      &lt;tr&gt;
        &lt;td id="navigation"&gt;
            &lt;?php echo navigation($selSubject, $selectedPage);?&gt;
            &lt;br/&gt;
            &lt;a href="new_subject.php"&gt;+ Add a new subject&lt;/a&gt;
        &lt;/td&gt;
        &lt;td id="page"&gt;
            &lt;?php echo checkSubjOrPage();?&gt;
            &lt;br/&gt;
            &lt;div id="footer"&gt;Copyright 2007, Widget Corp&lt;/div&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
   &lt;/table&gt;
&lt;?php require("includes/footer.php"); ?&gt;
</code></pre>

<p><strong>class function.php below:</strong></p>

<pre><code>&lt;?php
    //This file is the place to store all basic functions.

    //NB!
    //function to prevent problems with submitting values, that contains
    //chars such as: "", '' etc., into the database.
    function mysql_prep($value){
        $magic_quotes_active = get_magic_quotes_gpc;

        //i.e. php&gt;= v4.3.0
        $new_enough_php = function_exists("mysql_real_escape_string");

        if($new_enough_php){
            //undo any magic quotes effects so mysql_real_escape_string can do the work
            if($magic_quotes_active){
                $value = stripslashes($value);
            }
            $value = mysql_real_escape_string($value);
        } else { // before PHP v4.3.0
            //if magic quotes aren't already on then add slashed manually
            if(!$magic_quotes_active){
                $value = addslashes($value);
            }
        }

        return $value;
    }

    function confirm_query($result_set){
        if(!$result_set){
            die("Database connection failed: " . mysql_error());
        }
    }

    function getAllSubjects(){
        global $connection;
        $query = "SELECT *
                    FROM subjects
                    ORDER BY position ASC";

        $subject_set = mysql_query($query, $connection);
        confirm_query($subject_set);

        return $subject_set;
    }

    function getPagesForSubject($subject_id){
        global $connection;
        $query = "SELECT *
                    FROM pages
                    WHERE subject_id = {$subject_id}
                    ORDER BY position ASC";

        $page_set = mysql_query($query, $connection);
        confirm_query($page_set);

        return $page_set;
    }



    function get_subject_by_id($subject_id){
        global $connection;
        $query = "SELECT * ";
        $query .= "FROM subjects ";
        $query .= "WHERE id=" . $subject_id . " ";
        $query .= "LIMIT 1";

        $result_set = mysql_query($query, $connection);
        confirm_query($result_set);

        //REMEMBER: 
        //if no rows are returned, fetch_array will return false.
        if($subject = mysql_fetch_array($result_set)){
            return $subject;
        } else {
            return NULL;
        }
    }

    function get_page_by_id($page_id){
        global $connection;
        $query = "SELECT * ";
        $query .= "FROM pages ";
        $query .= "WHERE id=" . $page_id . " ";
        $query .= "LIMIT 1";

        $result_set = mysql_query($query, $connection);
        confirm_query($result_set);

        //REMEMBER: 
        //if no rows are returned, fetch_array will return false.
        if($page = mysql_fetch_array($result_set)){
            return $page;
        } else {
            return NULL;
        }
    }

    function checkSubjOrPage(){
        global $selSubject;
        global $selectedPage;

        if(!is_null($selSubject)){
            return "&lt;h2&gt;" . $selSubject['menu_name'] . "&lt;/h2&gt;";
        } else if(!is_null($selectedPage)){
            return "&lt;h2&gt;" . $selectedPage['menu_name'] . "&lt;/h2&gt;" . "&lt;div&gt;" . $selectedPage['content'] . "&lt;/div&gt;";
        } else {
            return "&lt;h2&gt;" . "Select a subject or page to edit!" . "&lt;/h2&gt;";
        }
    }

    function findSelectedPage(){
        global $selSubject;
        global $selectedPage;

        if(isset($_GET['subj'])){
            $selSubject = get_subject_by_id($_GET['subj']);
            $selectedPage = "";
        } else if(isset($_GET['page'])){
            $selSubject = NULL;
            $selectedPage = get_page_by_id($_GET['page']);
        } else {
            $selectedPage = NULL;
            $selSubject = NULL;
        }
    }

    function navigation($selSubject, $selectedPage){
        $output =  "&lt;ul class=\"subjects\"&gt;";
        //3. Perform our database query
        $subject_set = getAllSubjects();

        while($subject = mysql_fetch_array($subject_set)){
            $output .= "&lt;li";
            if($subject["id"] == $selSubject['id']){
                $output .= " class=\"selected\"";
            }  
                $output .= "&gt;&lt;a href=\"content.php?subj=" . urlencode($subject["id"]) .
                "\"&gt;{$subject["menu_name"]}&lt;/a&gt;&lt;/li&gt;";

                $page_set = getPagesForSubject($subject["id"]);

                $output .= "&lt;ul class=\"pages\"&gt;";   
                while($page = mysql_fetch_array($page_set)){
                    $output .= "&lt;li";
                    if($page["id"] == $selectedPage['id']){
                        $output .= " class=\"selected\"";
                }
                    $output .= "&gt;&lt;a href=\"content.php?page=" . urlencode($page{"id"}) .
                    "\"&gt;{$page["menu_name"]}&lt;/a&gt;&lt;/li&gt;";
                }
                $output .= "&lt;/ul&gt;";
            }

                $output .= "&lt;/ul&gt;";

                return $output;
    }
    function getPositions(){
        $subject_set = getAllSubjects();
        $subject_counts = mysql_num_rows($subject_set);
        $output = "&lt;select name=\"position\"&gt;";

        //$subject_counts + 1 b/c we are adding a subject.
        for($count = 1; $count &lt;= $subject_counts +1; $count++){
            $output .= "&lt;option value=\"{$count}\"&gt;{$count}&lt;/option&gt;";
        }

        return $output . " &lt;/select&gt;";
    }

?&gt;
</code></pre>
