Forum Moderators: coopster
I'm tweaking an existing script that allows me to input information like a URL and TITLE to a flat-file but when I attempt to customize the script to write a DESC (description) to the file the field gets filled with a value called "Array".
$lfile = fopen("data.dat", "w");
foreach ($site as $num=>$href) {
if($href!= "" && $text[$num]!= "" && $desc!= "") {
$daysstart = "";
$daysremaining = "";
if(trim($sdays[$num])!= "") { $daysstart = formatdays($sdays[$num]); }
if(trim($edays[$num])!= "") { $daysremaining = formatdays($edays[$num]); }
fwrite($lfile, $href . "¦¦¦" . $text[$num] . "¦¦¦" . $desc . "¦¦¦" . $daysstart . "¦¦¦" . $daysremaining . "\n");
}
}
fclose($lfile);
Any ideas?
...thanks in advance for any assistance.
as tekomp metioned your code for setting $desc would help.
I can tell you that the reason it is writing array is because the variable $desc is actually an array. You will have add an array index to your write line
maybe just something like $desc[0] would work, no clue with out seeing the code that creates the variable though
Here is the complete code which may help:
<?php session_start();
if(isset($_POST['pass'])) { $_SESSION['admin'] = $_POST['pass']; }
if($_SESSION['admin']!= "newpass") {
login();
return 0;
}
if($_POST['dowhat'] == "write") {
$site = $_POST['site'];
$text = $_POST['text'];
$desc = $_POST['desc'];
$sdays = $_POST['sdays'];
$edays = $_POST['edays'];
$lfile = fopen("data.dat", "w");
foreach ($site as $num=>$href) {
if($href!= "" && $text[$num]!= "" && $desc!= "") {
$daysstart = "";
$daysremaining = "";
if(trim($sdays[$num])!= "") { $daysstart = formatdays($sdays[$num]); }
if(trim($edays[$num])!= "") { $daysremaining = formatdays($edays[$num]); }
fwrite($lfile, $href . "¦¦¦" . $text[$num] . "¦¦¦" . $desc . "¦¦¦" . $daysstart . "¦¦¦" . $daysremaining . "\n");
}
}
fclose($lfile);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title Here</title>
</head>
<body><br />
<form method="post">
<div align="center">
<div id="manager" align="center">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="20%"><strong>URL</strong></td>
<td width="20%"><strong>Title</strong></td>
<td width="30%"><strong>Description</strong></td>
<td width="15%"><strong>Start</strong></td>
<td width="15%"><strong>Remain</strong></td>
</tr>
<?php
$lines = file('links.cgi');
$num = 0;
foreach ($lines as $line) {
$data = explode("¦¦¦", $line);
$syear = substr($data[3],0,4);
$sday = substr($data[3],4);
$eyear = substr($data[4],0,4);
$eday = substr($data[4],4);
$daysstart = "";
$daysremaining = "";
if(trim($data[3])!= "") { $daysstart = formatcode($syear,$sday); }
if(trim($data[4])!= "") { $daysremaining = formatcode($eyear,$eday); }
echo "<tr>\n<td><input type=\"text\" name=\"site[]\" value=\"$data[0]\"></td>";
echo "<td><input type=\"text\" name=\"text[]\" value=\"$data[1]\"></td>";
echo "<td><input type=\"text\" name=\"desc[]\" value=\"$data[2]\"></td>";
echo "<td><input type=\"text\" name=\"sdays[]\" id=\"s" . $num . "\" value=\"" . $daysstart . "\" size=\"2\"></td>";
echo "<td><input type=\"text\" name=\"edays[]\" id=\"e" . $num . "\" value=\"" . $daysremaining . "\" size=\"2\"></td>\n</tr>";
$num++;
}
?>
<tr>
<td><input type="text" name="site[]" value=""></td>
<td><input type="text" name="text[]" value=""></td>
<td><input type="text" name="desc[]" value=""></td>
<td><input type="text" name="sdays[]" id="s<?php echo $num?>" value="" size="2"></td>
<td><input type="text" name="edays[]" id="e<?php echo $num?>" value="" size="2"></td>
</tr>
</table>
<input type="hidden" name="dowhat" value="write">
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
<?php
function login() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Link Manager Login</title>
<link rel="stylesheet" type="text/css" href="layout.css" />
<link rel="stylesheet" type="text/css" href="presentation.css" />
</head>
<body>
<br /><br /><br />
<div style="text-align: center">
<div id="links">
<form method="post">
Please Login: <input type="text" name="pass">
<input type="submit" value="Login">
</form>
</div>
</div>
</body>
</html>
</html>
<?php
}
function formatdays($daystogo) {
$ryear = date("Y");
$rday = date("z");
$rday += $daystogo;
while($rday > 364) {
$rday -= 365;
$ryear++;
}
return ($ryear . $rday);
}
function formatcode($year, $day) {
$ryear = date("Y");
$rday = date("z");
$daysleft = "";
if($ryear == $year) { $daysleft = $day - $rday; }
else {
$rday = 364 - $rday;
$ryear++;
while($ryear < $year) {
$rday = 365 + $rday;
$ryear++;
}
$daysleft = ($rday + $day) + 1;
}
return ($daysleft);
}
?>
Thank you all for your assistance, I really don't understand a lot of this stuff but generarally I can play around with the code enough to get it to do what I want. In this case I added all the "desc" stuff and formatted it exactly like the existing code and it works fine except when writing the data to the file. I can manually insert the data into "data.dat" and the PHP file reads it correctly; however the minute I save the data all the "desc" values in the "data.dat" file get overwritten with the value "Array"...
echo "<td><input type=\"text\" name=\"desc[]\" value=\"$data[2]\"></td>";
you are assigning the values to an array, I assume it loops to create a number of those same elements in the form (I didn't really read it all in detail). So when you are read the values out of the $_POST array there will actually be an array here
$desc = $_POST['desc'];
my guess is you will need to treat desc like $text[$num], you will need the $num portion.
This will be the same for all your vars, they are all stuck into the $_POST array in arrays.