Jump to content

Array data not being displayed correctly


Bahamut666

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.