i'm a beginner with php, and i'm trying to save textarea content inside a news.txt file.
After saving, i'll display the results in another page. Everything works fine, the ddate and the title is displayed perfectly, and even the textarea content, but if i press enter to create a new line, it doesn't work anymore.
here's what happen and some code.
Notice: Undefined offset: 1 in C:\Virtual Servers\xampp\htdocs\read.php on line 17
Notice: Undefined offset: 2 in C:\Virtual Servers\xampp\htdocs\read.php on line 18
and my code:
read.php page
<html>
<head>
<style>
.news{background:#f1f1f1;width:960px;height:500px;margin-bottom:15px;}
.title{color:red;}
.date{font-style:italic;}
.text{color:green;font-weight:bold;}
</style>
</head>
<body>
<?php
$i = 0;
$array = file("news/news.txt");
foreach($array as $row){
$split = explode('|', "$row");
$data = $split[0];
$titolo = $split[1];
$testo = $split[2];
$i++;
print '<div id="'.$i.'" class="news"><div class="date">'.$data.'</div><div class="title">'.$titolo.'</div><div class="text">'.$testo.'</div></div>';
}
?>
</body>
</html>
index.php page:
<div class="container">
<form name="myform" onsubmit="return validateForm()" action="welcome.php" method="post">
<?php echo date('d-m-Y'); ?></br>
<h1>Titolo:</h1><input type="text" id="Title" name="titolo" class="textStyleTitle"><br>
<p>Testo:</p><textarea id="Text" name="testo" class="textStyleText"></textarea><br>
<input type="submit" value="salva">
</form>
</div>
Any solutions?
thanks a lot.
!!UPDATE!! With the solution mentioned below, i've solved in part. I have no errors, but here's how it looks like:
<body>
<div id="1" class="news">
<div class="date">04-01-2014</div>
<div class="title">Testing title</div>
<div class="text">Testing textarea row 1 (enter pressed)</div>
</div>
<div id="2" class="news">
<div class="date">Testing textarea row 2 (enter pressed)</div>
<div class="title"></div><div class="text"></div></div>
<div id="3" class="news">
<div class="date">Testing textarea row 3 (enter pressed)</div>
<div class="title"></div><div class="text"></div></div>
<div id="4" class="news">
<div class="date">Testing textarea row 4</div>
<div class="title"></div><div class="text"></div>
</div></body>
unfortunally, what i'm tryin' to reach is something like this:
<body>
<div id="1" class="news">
<div class="date">04-01-2014</div>
<div class="title">Testing title</div>
<div class="text">Testing textarea row 1 (enter pressed)
Testing textarea row 2 (enter pressed)
Testing textarea row 3 (enter pressed)
Testing textarea row 4
</div>
</div>
You have made a mistake here :
$split = explode('|','$row')
, No need to set$row
into quotes, because it will use it as astring
not as arrayYou need to use thhis
$split = explode('|',$row)
to explode you string into arraytry this code.
first remove quotes for $row in explode make as below:
$split = explode('|', $row);
thenuse:
instead
same for
$split[1]
and$split[2]