.:: Bots United ::.  
filebase forums irc server wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps waypoints yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
Organize Fopen
Old
  (#1)
Ancient
PodBot MM's Laziest Waypointer
 
Ancient's Avatar
 
Status: Offline
Posts: 1,010
Join Date: Jan 2005
Location: Nebraska, United States of America
Default Organize Fopen - 21-08-2008

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?


[Web Designer][Waypointer][Gamer]
CFE Games Administrator
[CFE]Games.com
[Never Trust the Untrusted]
   
Reply With Quote
Re: Organize Fopen
Old
  (#2)
The Storm
Council Member / E[POD]bot developer
 
The Storm's Avatar
 
Status: Offline
Posts: 1,564
Join Date: Jul 2004
Location: Bulgaria
Default Re: Organize Fopen - 22-08-2008

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.
   
Reply With Quote
Re: Organize Fopen
Old
  (#3)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 362
Join Date: Dec 2003
Location: China
Default Re: Organize Fopen - 22-08-2008

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); 

Last edited by Cheeseh; 22-08-2008 at 20:59.. Reason: [php] not [code] !
   
Reply With Quote
Re: Organize Fopen
Old
  (#4)
Ancient
PodBot MM's Laziest Waypointer
 
Ancient's Avatar
 
Status: Offline
Posts: 1,010
Join Date: Jan 2005
Location: Nebraska, United States of America
Default Re: Organize Fopen - 22-08-2008

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.


[Web Designer][Waypointer][Gamer]
CFE Games Administrator
[CFE]Games.com
[Never Trust the Untrusted]
   
Reply With Quote
Re: Organize Fopen
Old
  (#5)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 362
Join Date: Dec 2003
Location: China
Default Re: Organize Fopen - 22-08-2008

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?
   
Reply With Quote
Re: Organize Fopen
Old
  (#6)
Ancient
PodBot MM's Laziest Waypointer
 
Ancient's Avatar
 
Status: Offline
Posts: 1,010
Join Date: Jan 2005
Location: Nebraska, United States of America
Default Re: Organize Fopen - 23-08-2008

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.


[Web Designer][Waypointer][Gamer]
CFE Games Administrator
[CFE]Games.com
[Never Trust the Untrusted]

Last edited by Ancient; 23-08-2008 at 20:26..
   
Reply With Quote
Reply

Tags
array, fopen, fread, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com