Page 1 of 1

reverse array displaying data in a weird order. Please help

Posted: Wed Apr 20, 2005 3:31 pm
by pallarisari
Hi

ive recently managed to create an array reverse script which reverses the data from the text file. Unfortunately it also displays the data in the wrong order but for the life of me i dont know how to fix this:

correct ordering of data - but posts new data to the bottom
http://gnrlies.com/test/data.php

reverse array - posting data to the top but in wrong order: http://gnrlies.com/test/data4.php

heres my code for data4.php:

Code: Select all

<?php

include("header.inc");

$page = $_GET['page'];
if ($page == "") { $page = 1; }
$fwd = $page - 1;
$rwd = $page +1;

// Setting the default values for number of records per page -------------------------
$perpage = 100;
$filename = "data.php";

// Reading in all the records, putting each entry in one Array Element -----

$fd = fopen ("data.txt", "r");
while (!feof ($fd)) 
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);

// Counting the total number of entries (lines) in the data text file ----------------

$reverse_line = array_reverse($lines);
$result = count($lines);
$count = $result-1;

// Caclulate how many pages there are ----------------------------------------

if ($count == 0) { $totalpages = 0; }
else { $totalpages = intval(($count - 1) / $perpage) + 1; }

$end = ($page * $perpage) - 1;
$start = $end - ($perpage-1); if ($start <= 1) { $start = 0; }


if ($start < 0) { $start = 0; }

for ($i = $end; $i>=$start; $i--) 
{
	echo (stripslashes($lines[$i]));
}

echo "<center>";

// Creating the Forward and Backward links -------------------------------------

if ($fwd > 0 && $rwd > 0 && $rwd<$totalpages+1)
{
echo "<br><a href=\"$filename?page=$fwd\">&lt&lt</a>";
echo "<a href=\"$filename?page=$rwd\">&gt&gt</a><br>";
}
else if ($fwd == 0)
{ echo "<a href=\"$filename?page=$rwd\">&gt&gt</a><br>"; }
else if ($rwd == 0)
{ echo "<br><a href=\"$filename?page=$fwd\">&lt&lt</a>"; }
else if ($rwd == $totalpages+1)
{ echo "<a href=\"$filename?page=$fwd\">&lt&lt</a><br>"; }


for ($i = 1; $i<=$totalpages; $i++)
{
echo " [<a href=\"$filename?page=$i\">$i</a>] ";
}
echo "</center>";
?>
<?php
include("footer.inc");
?>

my code for my form handler:

Code: Select all

<?php
include("header.inc");
//writing data to log
{
    $arrow = $_POST['arrow'];
	$filename = "data.txt"; // File which holds all data
	$content = '<br><br><img src="images/amazonarrow.gif" align="absmiddle" width="10" height="10">';
	$fp = fopen($filename, "a"); // Open the data file, file points at the end of file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
{
    $TimeFrame = $_POST['TimeFrame'];
	$filename = "data.txt"; // File which holds all data
    $content = " <b>$TimeFrame\n</b>"; // Content to write in the data file
	$fp = fopen($filename, "a"); // Open the data file, file points at the end of file
	}

$Time = $_POST['Time'];
if(!$Time) // If time scale isnt entered show error message
{
    echo "Please enter dates for <b>Week Commencing</b>"; 
	echo '<br><br><a href="javascript:history.go(-1)">Go Back</a><br><br>';
	include("footer.inc");
    exit;
}
else
{
	$fw = fwrite( $fp, $content ); // Write the content on the file
}
{
    $filename = "data.txt"; // File which holds all data
    $content = nl2br("$Time\n"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	// Promotional text and message and percent
	{
    $promotionaltxt = $_POST['promotionaltxt'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("\n<b>$promotionaltxt</b>"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$Promo = $_POST['Promo'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br ("$Promo\n"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$Promotional = $_POST['Promotional'];
    $filename = "data.txt"; // File which holds all data
    $content = nl2br("\n$Promotional\n"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	// ending promotional element field data input
	
	// starting large projects data field
	{
	$largeprojectstxt = $_POST['largeprojectstxt'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("\n<b>$largeprojectstxt</b>"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$LProject = $_POST['LProject'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("$LProject\n"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$LargeProjects = $_POST['LargeProjects'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("\n$LargeProjects\n"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	//end large projects
	//start admin data fields
	{
	$admintxt = $_POST['admintxt'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("\n<b>$admintxt</b>"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$AdminPercent = $_POST['AdminPercent'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("$AdminPercent\n"); // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$Admin = $_POST['Admin'];
	$filename = "data.txt"; // File which holds all data
    $content = nl2br("\n$Admin\n\n"); // Content to write in the data file
	$fp = fopen($filename, "a"); // Open the data file, file points at the end of file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	
// Function to breakup log words in message -------------------------
function wordbreak($text, $wordsize) {

if (strlen($text) <= $wordsize) { return $text; } # No breaking necessary, return original text.

$text = str_replace("\n", "", $text); # Strip linefeeds
$done = "false";
$newtext = "";
$start = 0; # Initialize starting position
$segment = substr($text, $start, $wordsize + 1); # Initialize first segment

while ($done == "false") { # Parse text

	$lastspace = strrpos($segment, " ");
	$lastbreak = strrpos($segment, "\r");

	if ( $lastspace == "" AND $lastbreak == "" ) { # Break segment
		$newtext .= substr($text, $start, $wordsize) . " ";
		$start = $start + $wordsize; }
	else { # Move start to last space or break
		$last = max($lastspace, $lastbreak);
		$newtext .= substr($segment, 0, $last + 1);
		$start = $start + $last + 1;
	} # End If - Break segment

	$segment = substr($text, $start, $wordsize + 1);

	if ( strlen($segment) <= $wordsize ) { # Final segment is smaller than word size.
		$newtext .= $segment;
		$done = "true";
	} # End If - Final segment is smaller than word size.

} # End While - Parse text

$newtext = str_replace("\r", "\r\n", $newtext); # Replace linefeeds
return $newtext;

} # End of function - Word Break
	
	$breaker = $_POST['breaker'];
	$filename = "data.txt"; // File which holds all data
	$content ='<br><img src="images/linebreak.gif" width="100%" height="10"><br>';
	$fp = fopen($filename, "a"); // Open the data file, file points at the end of file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	
	fclose( $fp ); // Close the file after writing
	if(!$fw) echo "Couldn't write the entry.";
    else echo "Your entry has been successful";
	echo '<br><br><a href="data.php"><b>View Workload Report</b></a><br><br>';
}
?> 
<?php
include("footer.inc");
?>



please help me fix this, i am in dire straits and have been trying to fix this for over 4days now :(

thx
ari

Posted: Thu Apr 21, 2005 3:07 am
by JayBird
im not sure what your text file contains, but would array_reverse not done the trick?

http://uk.php.net/manual/en/function.array-reverse.php