Jump to content

[SOLVED] Problem with adding Table to this piece of code.


Dekken

Recommended Posts

Hey!

started learning PHP yesterday...so i took on myself small project for my sister

News board i guess? some kind of? :D

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:

xndh87.jpg

but whatever i tried got screwed up and messed up!

also is there more efficient way of doing this?

 

Thanks!! :-*

 

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  :-\

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>

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

 

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.