Jump to content

Recommended Posts

Hello all sorry newbie to PHP scripting here and still learning. I have an issue with a discussion board script I am working on and have been wracking my brain around why for 2 days now.

Code is as follows

 

<?php
if (!file_exists("messages.txt") || filesize("messages.txt") == 0)
echo "<p>There are no messages posted.</p>";
else {
$MessageArray = file("messages.txt");
for ($i=0; $i<count($MessageArray); ++$i) {
	$CurMessage = explode("~", $MessageArray[$i]);
	$KeyMessageArray[$CurMessage[0]] = "~" .  $CurMessage[1] . "~" . $CurMessage[2];
}
$Count = 1;
foreach($KeyMessageArray as $Message)  {
	$CurMessage = explode("~", $Message);
	echo "<tr>";
	echo "<td><strong>" . $Count++ . "</strong>.</td>";
	echo "<td><strong>Topic</strong>: " . stripslashes(key($KeyMessageArray)) . "<br />";
	echo "<strong>Name</strong>: " . stripslashes($CurMessage[1]) . "<br />";
	echo "<strong>Message</strong>: " . stripslashes($CurMessage[2]);
	echo "</td></tr>";
	next($KeyMessageArray);

}
}
?>

 

messages.txt file is filled with test data as follows:

 

Topic 1~Name 1~Message 1

Topic 2~Name 2~Message 2

Topic 3~Name 3~Message 3

Topic 4~Name 4~Message 4

 

the ouput is as follows:

 

Discussion

1. Topic: Topic 2

Name: Name 1

Message: Message 1

2. Topic: Topic 3

Name: Name 2

Message: Message 2

3. Topic: Topic 4

Name: Name 3

Message: Message 3

 

As you can see from the output for some reason the topic from the following message is being displayed with the previous post. It is like the 0 variable of the array in the first pass is being ignored for some reason.

 

Any help with this would be greatly appreciated.

 

<?php
<SNIPPED>
else {
$MessageArray = file("messages.txt");
for ($i=0; $i<count($MessageArray); ++$i) {
	$CurMessage = explode("~", $MessageArray[$i]);
	$KeyMessageArray[$CurMessage[0]] = "~" .  $CurMessage[1] . "~" . $CurMessage[2];
}
</END SNIPPED>
?>

 

messages.txt file is filled with test data as follows:

 

Topic 1~Name 1~Message 1

Topic 2~Name 2~Message 2

the ouput is as follows:

 

Discussion

1. Topic: Topic 2

Name: Name 1

As you can see from the output for some reason the topic from the following message is being displayed with the previous post. It is like the 0 variable of the array in the first pass is being ignored for some reason.

 

At first glance --- your code says

for ($i=0; $i<count($MessageArray); ++$i) {
             $CurMessage = explode("~", $MessageArray[$i]);

If the fornext loop works the same as C / Java etc  (spot the php newbie answering here gulp )

Then i is equal to 1 on the first pass through the messages.

change the code to

 

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

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

 

and it will probably work.

 

++i  means increment i then do the loop - first loop i=1

i++ means do the loop then increment i  - first loop i=0

 

Shane

For the record finally figured out what works..

 

<?php
if (!file_exists("messages.txt") || filesize("messages.txt") == 0)
echo "<p>There are no messages posted.</p>";
else {
$MessageArray = file("messages.txt");
for ($i=0; $i<count($MessageArray); $i++) {
	$CurMessage = explode("~", $MessageArray[$i]);
	$KeyMessageArray[$CurMessage[0]] = $CurMessage[0] . "~" .  $CurMessage[1] . "~" . $CurMessage[2];
}
$Count = 1;
foreach($KeyMessageArray as $Message)  {
	$CurMessage = explode("~", $Message);
	echo "<tr>";
	echo "<td><strong>" . $Count++ . "</strong>.</td>";
	echo "<td><strong>Topic</strong>: " . stripslashes($CurMessage[0]) . "<br />";
	echo "<strong>Name</strong>: " . stripslashes($CurMessage[1]) . "<br />";
	echo "<strong>Message</strong>: " . stripslashes($CurMessage[2]);
	echo "</td></tr>";
	next($KeyMessageArray);
}
}
?>

 

Changes:

 

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

 

and

 

echo "<td><strong>Topic</strong>: " . stripslashes($CurMessage[0]) . "<br />";

 

 

Still do not understand why the other way doesn't work if someone else can explain it please do. but for not this problem is solved tks.

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.