Dekken Posted November 14, 2008 Share Posted November 14, 2008 Hey! started learning PHP yesterday...so i took on myself small project for my sister News board i guess? some kind of? Iam trying to make a Text file show up in a Table, but i cant seem to make it. <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1255"> <title></title> </head> <?php $fileName = 'file_name.txt'; $fileBody = file_get_contents($fileName); //Date if (preg_match_all('/\[CloseDate](.*?)\[CloseDate]/', $fileBody, $matches)) { $words = $matches[1]; for ($i = 0; $i < count($words); $i++) { echo $words[$i]; } } //Kurs if (preg_match_all('/\[CloseKurs](.*?)\[CloseKurs]/', $fileBody, $matches)) { $words = $matches[1]; for ($i = 0; $i < count($words); $i++) { echo $words[$i]; } } // Subject if (preg_match_all('/\[CloseSubject](.*?)\[CloseSubject]/', $fileBody, $matches)) { $words = $matches[1]; for ($i = 0; $i < count($words); $i++) { echo $words[$i]; } } ?> </html> basically i want it to look like this: but whatever i tried got screwed up and messed up! also is there more efficient way of doing this? Thanks!! Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/ Share on other sites More sharing options...
.josh Posted November 14, 2008 Share Posted November 14, 2008 we'd have to see file_name.txt to figure out if there's a more efficient way of doing that. Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690506 Share on other sites More sharing options...
Dekken Posted November 14, 2008 Author Share Posted November 14, 2008 its not that important...i am just Inserting data into the text file using <textarea>... but here it is [CloseKurs]KURS[CloseKurs] [CloseSubject]SUBJECT[CloseSubject] [CloseDate]DATE[CloseDate] [CloseKurs]KURS[CloseKurs] [CloseSubject]MESSAGE[CloseSubject] [CloseDate]DATE[CloseDate] the CloseDate and CloseSubject etc is the only way i tho of, about how to insert data into tables...identify which line is which :-\ Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690509 Share on other sites More sharing options...
.josh Posted November 15, 2008 Share Posted November 15, 2008 Personally I would use a database but if that's not a route you're willing or able to take, I would look into xml. What you're doing is the same general principle. Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690514 Share on other sites More sharing options...
Dekken Posted November 15, 2008 Author Share Posted November 15, 2008 i looked a bit into it, ill consider it whenever i get some free time but the XML still doesnt help me with the Tables thingy... how could i make the tables show up correctly? =\ Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690532 Share on other sites More sharing options...
.josh Posted November 15, 2008 Share Posted November 15, 2008 You're not going to be able to properly do your tables with 3 separate loops like that. Store each regex result into a separate array (or 1 multidim array) and then do 1 loop. Example: <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1255"> <title></title> </head> <?php $fileName = 'file_name.txt'; $fileBody = file_get_contents($fileName); //Date if (preg_match_all('/\[CloseDate](.*?)\[CloseDate]/', $fileBody, $matches)) { $words['date'] = $matches[1]; } //Kurs if (preg_match_all('/\[CloseKurs](.*?)\[CloseKurs]/', $fileBody, $matches)) { $words['kurs'] = $matches[1]; } // Subject if (preg_match_all('/\[CloseSubject](.*?)\[CloseSubject]/', $fileBody, $matches)) { $words['subject'] = $matches[1]; } echo "<table>"; $total = count($words['date']); for ($x = 0; $x < ($total - 1); $x++) { echo "<tr>"; echo "<td>{$words['date']}</td>"; echo "<td>{$words['subject']}</td>"; echo "<td>{$words['message']}</td>"; echo "</tr>"; } echo "</table>"; ?> </html> Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690541 Share on other sites More sharing options...
Dekken Posted November 15, 2008 Author Share Posted November 15, 2008 Thanks! i got the tables sorted out now the problem i have is how do i make it Read the words Between the Brackets? if i have something like this: [CloseDate] Test. Test 2 Test 3 [CloseDate] It wont show up anything...just an empty table Thats my code: <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1255"> <title></title> </head> <?php $save_file = $_POST['save_file']; $savecontent1 = $_POST['savecontent1']; $savecontent2 = $_POST['savecontent2']; $savecontent3 = $_POST['savecontent3']; $loadcontent = "file_name.txt"; if($save_file) { $savecontent1 = stripslashes('[CloseDate]' . $savecontent1 . '[CloseDate]' . "\n"); $savecontent2 = stripslashes('[CloseKurs]' . $savecontent2 . '[CloseKurs]' . "\n"); $savecontent3 = stripslashes('[CloseSubject]' . $savecontent3 . '[CloseSubject]' . "\n"); $fp = @fopen($loadcontent, "a"); if ($fp) { fputs($fp,$savecontent1."\r\n"); fputs($fp,$savecontent2."\r\n"); fputs($fp,$savecontent3."\r\n"); fclose($fp); } } $fp = @fopen($loadcontent, "a+"); $loadcontent = fread($fp, filesize($loadcontent)); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); ?> <center> <form method=post action="<?=$_SERVER['PHP_SELF']?>"> <b> Date:</b> <br> <input type=text name="savecontent1"> <br> <br> <b> Kurs:</b> <br> <input type=text name="savecontent2"> <br> <br> <b> Subject:</b> <br> <textarea name="savecontent3" cols="70" rows="25" dir=rtl></textarea> <!-- <?=$loadcontent?> --> <br> <input type="submit" name="save_file" value="Save"> </form> </center> </html> probably have lots of mistakes here...but oh well Thanks Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690676 Share on other sites More sharing options...
Dekken Posted November 15, 2008 Author Share Posted November 15, 2008 GRRR cant edit pots? oh well... Nvm, ill just learn some mysql...will be much easier i guess. Thanks Link to comment https://forums.phpfreaks.com/topic/132777-solved-problem-with-adding-table-to-this-piece-of-code/#findComment-690702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.