Bahamut666 Posted June 21, 2011 Share Posted June 21, 2011 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. Link to comment https://forums.phpfreaks.com/topic/239947-array-data-not-being-displayed-correctly/ Share on other sites More sharing options...
vbconz Posted June 21, 2011 Share Posted June 21, 2011 <?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 Link to comment https://forums.phpfreaks.com/topic/239947-array-data-not-being-displayed-correctly/#findComment-1232586 Share on other sites More sharing options...
Bahamut666 Posted June 21, 2011 Author Share Posted June 21, 2011 It was a good thought but I see the same results when placing the incriment after the $i. Link to comment https://forums.phpfreaks.com/topic/239947-array-data-not-being-displayed-correctly/#findComment-1232589 Share on other sites More sharing options...
Bahamut666 Posted June 21, 2011 Author Share Posted June 21, 2011 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. Link to comment https://forums.phpfreaks.com/topic/239947-array-data-not-being-displayed-correctly/#findComment-1232601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.