PDA

View Full Version : PHP arrays and such.



Cyberman
May 29th, 2004, 04:31
Ok.. I have a script I wrote that's fairly simple a series of news articles that are in a text file are read then formated by the script. I want to reverse the order of the articles as they appear in the news list (IE most resent is the LAST item in the file). The problem is Arrays do not seem to work quite right.

I read the subject date and author into 3 arrays then the new text into a third (with CRLF's). However when I go through the array I get empty elements from the array.

Some of the data grabing code...


function showarticle($NewsH)
{
$Poster= fgets($NewsH);
if (!feof($NewsH))
{
$Date= fgets($NewsH);
if (!feof($NewsH))
{
$Title= fgets($NewsH);
if (!feof($NewsH))
{
showhead($Poster, $Date, $Title);
echo "<tr><td valign=\"top\">\r\n";
$Titles[] = $Title;
$Authors[]= $Poster;
$Dates[] = $Date;


disgorging the data..


echo "<center>\r\n";
echo " <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" valign=\"top\" width=\"95%\">\r\n";
for($Index = $Count-1; $Index > -1; $Index--)
{
showhead(
$Authors[$Index],
$Dates[$Index],
$Subject[$Index]
);
echo " <tr><td valign=\"top\">\r\n";
printf("%d<br>\r\n", $Index);
echo $Texts[$Index];
echo " </tr></td>\r\n";
}
*/
echo " </table>\r\n";
echo "</center>\r\n";


This produces nothing but empty articles, however outputing things in the order they are read in works <if not looks ugly>. OK I need to fix the formating issues as the html put out isn't quite right.

What is my main idiocy here? :) What am I doing wrong?

Cyb

Teamz
May 29th, 2004, 07:43
http://www.php.net/manual/en/function.array-reverse.php

Cyberman
May 30th, 2004, 02:52
http://www.php.net/manual/en/function.array-reverse.php
I think I failed to also note that it is blank either way. That is if I read the array forward or backward. Reversing the array has no affect so I am wondering if I am not reading things in or missing something.

Cyb

The Khan Artist
May 30th, 2004, 04:17
Hmm... I don't see anything wrong with that. The first part is definately putting the data into the array.

My only guess would be scope problems. Could you post your entire script along with a data file?

zAlbee
May 30th, 2004, 05:46
yeah, it might be a scope problem. it doesn't look like you set your array objects to be accessible globally. ie. something like this:


function showarticle($NewsH)
{
global $Titles;
global $Authors;
global $Dates;

though i haven't seen the whole thing, so this might not be the problem.