Hello,

I am writing a simple forum and I keep getting the error 'Undefined Variable'. But this makes no sense has I have assinged a a string to the variable in question . Its part of a while loop and has a .(period) before the equals. If I remove that the problem resolves its self but I need the period in order to concantonate the data read from sql.

the line is `$topic .= "<table width='100%' style='border-collapse: collapse;'>";

and the code the snippet is taken from is as follows

    if (isset($_SESSION['uid'])) {
        $logged = " | <a href='create_topic.php?cid=".$cid."'>Click Here To Create A Topic</a>";
    } else {
        $logged = " | Please log in to create topics in this forum.";
    }
    // Query that checks to see if the category specified in the $cid variable actually exists in the database
    $sql = "SELECT id FROM category WHERE id='".$cid."' LIMIT 1";
    // Execute the SELECT query
    $res = mysql_query($sql) or die(mysql_error());
    // Check if the category exists
    if (mysql_num_rows($res) == 1) {
        // Select the topic that are associated with this category id and order by the topic_reply_date
        $sql2 = "SELECT * FROM topic WHERE category_id='".$cid."' ORDER BY topic_reply_date DESC";
        // Execute the SELECT query
        $res2 = mysql_query($sql2) or die(mysql_error());
        // Check to see if there are topic in the category
        if (mysql_num_rows($res2) > 0) {
            // Appending table data to the $topic variable for output on the page
            $topic .= "<table width='100%' style='border-collapse: collapse;'>";
            $topic .= "<tr><td colspan='4'><a href='index.php'>Return To Forum Index</a>".$logged."<hr /></td></tr>";
            $topic .= "<t.r style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Last User</td><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
            $topic .= "<tr><td colspan='4'><hr /></td><tr>";
            ;
            // Fetching topic data from the database
            while ($row = mysql_fetch_assoc($res2)) {
                // Assign local variables from the database data
                $tid = $row['id'];
                $title = $row['topic_title'];
                $views = $row['topic_views'];
                $date = $row['topic_date'];
                $creator = $row['topic_creator'];

            }
            $topic .= "</table>";
            // Displaying the $topic variable on the page
            echo $topic;
        }

You're trying to concatenate to a variable that was never truly given an initial value. Somewhere before the first concatenation, add this:

$topic = '';

Yea I had already tried that. The undefined variable issue was resolved but the information still wasnt reading and I know the If statemment returns true because I tested that. I also added a if (!isset($topic)) {$topic = ''; } thinking that maybe the blank value was resetting it. I have deduced is definitly something with '.=' it doesnt like so I am now on php.net trying to figure it out.

Problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.