Jump to content

*** DELETE PLEASE *** (Solved)


jaimenacach

Recommended Posts

Hello,

 

First of all, I just want to say this is my FIRST time ever posting on a PHP Forum.

I've been studying PHP on my own as well as through an online class using a Book called "PHP Programming with MySQL" by DON GOSELLIN. This problem is from Chapter 7.

In the file called "ViewDiscussion.php"

 

SHORT STORY ABOUT WHAT I'M DOING

I'm working on an example for how to create a Discussion forum (like the one I'm using here). The input of the user (Topi, name and message) are saved in a TEXT FILE into the array called "$MessageArray". Then I create a NEW ARRA in which The TOPIC for each message is used as part of the KEY to my ASSOCIATIVE ARRAY in the array called "$KeyMessageArray". It's defined like this:

 

$KeyMessageArray[$CurMessage[0]] = $CurMessage[1] . "~" . $CurMessage[2];

 

I then have a FOREACH loop look that iterates through the ELEMENTS of the $KeyMessageArray to print (and show) the DISCUSSION FORUM messages.

 

THE PROBLEM

The Content of each message is Correctly printed (each row, beginning with the first) has the correct user's NAME and MESSAGE, but the TOPIC for each line actually LISTS the WRONG Topic Name (Row 1, of message 1, contains the TOPIC name of message 2; Message 2, has TOPIC name of message 3). REALLY WEIRDDD!!

 

EXAMPLE OF HOW LIST LOOKS LIKE WHEN PRINTED (visit link below)

http://mkdigitaldirect.com/php/ch07/ViewDiscussion.php

 

CONTENT OF TEXT FILE called "messages.txt" has the following contents:

Topic 1~User #1~Content of Message 1

Topic 2~User #2~Content of Message 2

Topic 3~User #3~Content of Message 3

Topic 4~User #4~Content of Message 4

 

 

CODE LOOKS LIKE THIS: THE BOLD SECTION shows the Foreach loop

$MessageArray = file("messages.txt");

//$MessageArray = stripslashes($MessageArray);

for ($i = 0; $i < count($MessageArray); ++$i) {

$CurMessage = explode("~", $MessageArray[$i]);

// The following creates a new array in which it uses the TOPIC of the entry as the KEY for the array, then it has the rest of the items.

$KeyMessageArray[$CurMessage[0]] = $CurMessage[1] . "~" . $CurMessage[2];

}

//print_r($KeyMessageArray); // Prints the contents of the array

$Count = 1;

reset($KeyMessageArray);

foreach ($KeyMessageArray as $Message) {

$CurMessage = explode("~", $Message);

echo "<tr>";

echo "<td><strong>" . $Count++. "</strong>.</td>";

echo "<td><strong>Topic</strong>: " . key($KeyMessageArray) .  "<br />";

echo "<strong>Current Array element</strong>: " . current($KeyMessageArray) . "<br />";

echo "<strong>Name</strong>: " . $CurMessage[0] . "<br />";

echo "<strong>Message</strong>: " . $CurMessage[1];

echo "</td></tr>";

next($KeyMessageArray);

}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.