.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   Organize Fopen (http://forums.bots-united.com/showthread.php?t=6897)

Ancient 21-08-2008 22:29

Organize Fopen
 
PHP Code:

$filename "news.txt";
$fp fopen($filename"r");
$contents fread($fpfilesize($filename));
$newsTable explode("|NEWS|"$contents);
for(
$i 0count($newsTable) > $i$i++)
 {
  
$newsTable[$i] = explode("|~|"$newsTable[$i]);
 }
print_r($newsTable); 

Array it makes:
$newsTable[0][0]
$newsTable[0][1]
$newsTable[0][2]
$newsTable[0][3]
$newsTable[0][4]

I want it to make:
$newsTable[0][0]
$newsTable[0][1]
$newsTable[1][0]
$newsTable[1][1]

I'm so frustrated right now. I went through several kinds of file opening techniques and I'm just frustrated right now. Can anyone help me with this?

The Storm 22-08-2008 10:15

Re: Organize Fopen
 
It will not be easy to do it as you want with just a "for" cycle. Better go with "for" via all the arrays and save the info in new array in the way you want.

Cheeseh 22-08-2008 21:57

Re: Organize Fopen
 
hi ancient,

can you show an example of news.txt ?? I don't really understand what the code needs to do.
you want a 2D array? So you should need two loops!?

you need something like this???
PHP Code:


$filename 
"news.txt";
$fp fopen($filename"r");
$contents fread($fpfilesize($filename));
$newsTable explode("|NEWS|"$contents);

$actualNewsTable = array();

for(
$i 0count($newsTable) > $i$i++)
 {
     
$j 0;

     
$str newsTable[$i];

     while ( 
$str explode("|~|"$str) )
          
$actualNewsTable[$i][$j++] = $str;
 }
print_r($actualNewsTable); 


Ancient 22-08-2008 22:15

Re: Organize Fopen
 
PHP Code:

$filename "news.txt";
$fp fopen($filename"r");
$contents fread($fpfilesize($filename));
$newsTable explode("|NEWS|"$contents);
//print_r($newsTable);
for($i 0count($newsTable) > $i$i++)
 {
  
$news[$i] = explode("|~|"$newsTable[$i]);
 }
//print_r($news);
  
for($i count($news); $i$i--)
   {
 
     
// Descend From Newest to oldest
   


This works good, but I don't know if it will use to much PHP memory. I might have to do some obj_flush. It seems to work well. Podbot MM Test Page.

Code:

|NEWS|New Official Beta Release|~|Info
|NEWS|More|~|Body

Should be like

Explode |NEWS|
$newsTable[0] = "New Official Beta Release|~|Info";
(split / explode again: |~|)
$newsTable[0][0] = "New Official Beta Release";
$newsTable[0][1] = "Info";

I just hope I don't need to use obj_flush. I know the file won't be over 8 MB any time soon.

Cheeseh 23-08-2008 00:15

Re: Organize Fopen
 
You could always generate the page on the fly without using a newstable array and just do it within the loop while its reading the file to save memory?

Ancient 23-08-2008 21:16

Re: Organize Fopen
 
I could do that. That's actually a nice idea. Thank you. :)

I was reading in the PHP.net pages that the for function is quite slow compared to while and others.

Right now, I'm just worried about being able to edit the news posts. The way I'm thinking of doing it is read and save the data in the php memory. delete the file create and then write everything in order from 0 to the latest and of course have the edited posts in there somewhere.

Right now the news file is 23k, I don't think it will cause any problems, but I know there are some people who do not have cable and can run things very fast. I know it isn't the user who has to create the files since PHP is server-side. I just don't want to use to much memory and stuff on a single page. Espeically here at BU since VBulletin seems to be pretty slow to load as it is.

Everything is sorting fine for the moment. I even got some BBCodes up for the admins to make things easier.
Podbot MM News

It's been forever since I used a file-based DB. MySQL is my thing now days.


All times are GMT +2. The time now is 05:47.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.