I am at a loss where to go from here.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

I am at a loss where to go from here.

Post by akimm »

I have a small program that uses:

fopen()

fwrite()

fclose()

to write to a text file/ and or a flatfile database.

What I want to do is take the info from this flatfile and put it in alphabetical order, and have the letters output to a specific corresponding page. It's a dictionary, so A would go with a's and so-on.

Anyone have any idea on how I could go about this?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: I am at a loss where to go from here.

Post by aerodromoi »

akimm wrote:I have a small program that uses:

fopen()

fwrite()

fclose()

to write to a text file/ and or a flatfile database.

What I want to do is take the info from this flatfile and put it in alphabetical order, and have the letters output to a specific corresponding page. It's a dictionary, so A would go with a's and so-on.

Anyone have any idea on how I could go about this?
You might want to take a look at usort. However, as it was already mentioned in your previous thread, a "real" database might be an option here as well. It would not only allow for an easier approach to sorting your tables, but also facilitate handling more entries.

Here's the code for your flatfile needs:

Code: Select all

<?php
function columnSort($unsorted, $column) {
   $sorted = $unsorted;
   for ($i=0; $i < sizeof($sorted)-1; $i++) {
     for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
       if ($sorted[$j][$column] > $sorted[$j+1][$column]) {
         $tmp = $sorted[$j];
         $sorted[$j] = $sorted[$j+1];
         $sorted[$j+1] = $tmp;
     }
   }
   return $sorted;
}

function printarray($entryarray){
  for ($i=0;$i<count($entryarray);$i++){
    echo "<b>author:</b> ".$entryarray[$i]['author']." <b>word:</b> ".$entryarray[$i]['word']." <b>def:</b> ".$entryarray[$i]['definition']."<br />\n";
  }
}


$string = "John#datasep#lorem#datasep#yourdefinition1#entrysep#
Pete#datasep#ipsum#datasep#yourdefinition3#entrysep#
Kim#datasep#dolor#datasep#yourdefinition2#entrysep#
Name2#datasep#sit#datasep#yourdefinition5#entrysep#
Name3#datasep#amet#datasep#yourdefinition4";

$entryarray_old = explode("#entrysep#",$string);
$entryarray = array();

for ($i=0;$i<count($entryarray_old);$i++){
  $dataarray = explode("#datasep#",$entryarray_old[$i]);
  $entryarray[$i]['author']     = $dataarray[0];
  $entryarray[$i]['word']       = $dataarray[1];
  $entryarray[$i]['definition'] = $dataarray[2];  
}

$entryarray = columnSort($entryarray, 'author');

printarray($entryarray);
?>
function by Jeremy Swinborne

aerodromoi
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Assuming one word per line:

Code: Select all

$filename = 'myfile.txt';
$lines = sort(file($filename));
$char = '';
foreach ($lines as $line) {
    if (substr($line, 0, 1) != $char) {
        $char = substr($line, 0, 1);
        echo "- $char -<br/>";
    }
    echo "$line<br/>";
}
(#10850)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Oh jeez

Post by akimm »

I didn't mean for you to write it, I feel bad! That is awfuly kind of you, but I was just trying to figure out what direction I should take. Thank you tho, I'll try this.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

akimm wrote:I didn't mean for you to write it, I feel bad! That is awfuly kind of you, but I was just trying to figure out what direction I should take. Thank you tho, I'll try this.
@pm:

Just a quick explanation: arborint's code basically reads the content of the txt file, puts it into an array and sorts it. While looping through the array, the first letter of each word is stored in the variable $char. If the 1st letter of the word currently in the loop differs from the var $char (the first letter of the last word), the script will echo it.

My code assumes that you have a string $string with the content of the flatfile.

Script to put the content of test.txt into the string $string:

Code: Select all

$fname    = "test.txt";
$handle   = fopen($fname, "r");
$string   = fread($handle, filesize($fname));
fclose($handle);
If you've got a string which looks like this:

Code: Select all

John#datasep#lorem#datasep#yourdefinition1#entrysep#
Pete#datasep#ipsum#datasep#yourdefinition3#entrysep#
Kim#datasep#dolor#datasep#yourdefinition2#entrysep#
Name2#datasep#sit#datasep#yourdefinition5#entrysep#
Name3#datasep#amet#datasep#yourdefinition4
You can then define whether you want the result sorted by author, word or definition.

Code: Select all

$entryarray = columnSort($entryarray, 'author'); // just replace "author" with "word" or "definition"
The output will look like this (sorted by author):

Code: Select all

author: Kim word: dolor def: yourdefinition2
author: Name2 word: sit def: yourdefinition5
author: Name3 word: amet def: yourdefinition4
author: Pete word: ipsum def: yourdefinition3
author: John word: lorem def: yourdefinition1
aerodromoi
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok-

Post by akimm »

I wrote:
What if I wrote the code exclusively to sort a like perhaps slightly modify your code you gave me, and reuse it for each letter, to sort strictly each letter or the alphabet, then do you think with an include I could add in this A function B function and so on to their corresponding pages? Also, as I thought last night, I need to modify the string to fit my liking correct? Is there a way i can automate the sorting process. (Please don't write the code if there is, I'd feel bad and that's not the purpose of these forums) it's for morons like me to find advise from smarter folks like yourself.
aerodromoi wrote:


The sorting process is automated - you just need a txt file containing your words, definitions and authors. You can then read it into the script with the snippet I posted. The script will sort it for you (from what I gather, you'll want to sort it by the words). I've outlined how to change this parameter in my last post. Of course it's possible to combine the two scripts so as to have -a- ... -b- ... . I'd take the same approach as arborint. However, why don't we discuss this in the actual thread?

aerodromoi
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ok.

Post by akimm »

Code: Select all

<?php
$filename = 'words.txt'; 
$lines = sort(file($filename)); 
$char = ''; 
foreach ($lines as $line) { 
    if (substr($line, 0, 1) != $char) { 
        $char = substr($line, 0, 1); 
        echo "- $char -<br/>"; 
    } 
    echo "$line<br/>"; 
} 

function columnSort($unsorted, $column) { 
   $sorted = $unsorted; 
   for ($i=0; $i < sizeof($sorted)-1; $i++) { 
     for ($j=0; $j<sizeof($sorted)-1-$i; $j++) 
       if ($sorted[$j][$column] > $sorted[$j+1][$column]) { 
         $tmp = $sorted[$j]; 
         $sorted[$j] = $sorted[$j+1]; 
         $sorted[$j+1] = $tmp; 
     } 
   } 
   return $sorted; 
} 

function printarray($entryarray){ 
  for ($i=0;$i<count($entryarray);$i++){ 
    echo "<b>author:</b> ".$entryarray[$i]['author']." <b>word:</b> ".$entryarray[$i]['word']." <b>def:</b> ".$entryarray[$i]['definition']."<br />\n"; 
  } 
} 


$string = "John#datasep#lorem#datasep#yourdefinition1#entrysep# 
Pete#datasep#ipsum#datasep#yourdefinition3#entrysep# 
Kim#datasep#dolor#datasep#yourdefinition2#entrysep# 
Name2#datasep#sit#datasep#yourdefinition5#entrysep# 
Name3#datasep#amet#datasep#yourdefinition4"; 

$entryarray_old = explode("#entrysep#",$string); 
$entryarray = array(); 

for ($i=0;$i<count($entryarray_old);$i++){ 
  $dataarray = explode("#datasep#",$entryarray_old[$i]); 
  $entryarray[$i]['author']     = $dataarray[0]; 
  $entryarray[$i]['word']       = $dataarray[1]; 
  $entryarray[$i]['definition'] = $dataarray[2];  
} 

$entryarray = columnSort($entryarray, 'definition'); 

printarray($entryarray); 
?>
The problem with the first part of the code is his snipplet assumes each definition is 1 line, if there is more the sorting process might cut short a definition.

By the way I do understand a mysql database would be immensly easier, however, I do not have one available to me on my server.


About sorting the A, B, C, D et cetera how could i modify his for each loop to spit out single letter groups like all words that begin with A, and perhaps then funnel that to a specific page.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Ok.

Post by Christopher »

akimm wrote:By the way I do understand a mysql database would be immensly easier, however, I do not have one available to me on my server.
You might want to look at the SQLite library. It allows SQL queries, but the database tables are just files and no server is required (remember to put those db files in an .htaccess protected directory or outside your web directory).
(#10850)
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Ok.

Post by aerodromoi »

akimm wrote: About sorting the A, B, C, D et cetera how could i modify his for each loop to spit out single letter groups like all words that begin with A, and perhaps then funnel that to a specific page.
Here's a modified version of my script using arborint's idea to group the entries as well.
Assuming you've got a txt file called test.txt containing the data in the format I suggested in earlier posts, this script
echos them sorted and grouped by words. If you call the script using the get var "part" like script.php?part=a it'll print out all
entries in the flatfile where the word begins with an a (or an error message, if there are no entries to be found).

Code: Select all

<?php
$part     = substr($_GET['part'], 0, 1);
if (empty($part)) $part = "empty";
 
$fname    = "test.txt";
$handle   = fopen($fname, "r");
$string   = fread($handle, filesize($fname));
fclose($handle);

function columnSort($unsorted, $column) {
   $sorted = $unsorted;
   for ($i=0; $i < sizeof($sorted)-1; $i++) {
     for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
       if ($sorted[$j][$column] > $sorted[$j+1][$column]) {
         $tmp = $sorted[$j];
         $sorted[$j] = $sorted[$j+1];
         $sorted[$j+1] = $tmp;
     }
   }
   return $sorted;
}

function printarray($entryarray,$part){
  $found = false;
  for ($i=0;$i<count($entryarray);$i++){
    if (($part != "empty" && substr($entryarray[$i]['word'], 0, 1) == $part) || $part == "empty"){
      if (substr($entryarray[$i]['word'], 0, 1) != $char) {
        echo "<h2>- ".substr($entryarray[$i]['word'], 0, 1)." -</h2>";
        $char = substr($entryarray[$i]['word'], 0, 1); 
      }
      echo "<b>word:</b> ".$entryarray[$i]['word']." <b>def:</b> ".$entryarray[$i]['definition']." <b>author:</b> ".$entryarray[$i]['author']." <br />\n";
      $found = true;
    }
  }
  if (!$found) echo "Sorry - no entries found!";
}

$entryarray_old = explode("#entrysep#",$string);
$entryarray = array();

for ($i=0;$i<count($entryarray_old);$i++){
  $dataarray = explode("#datasep#",$entryarray_old[$i]);
  $entryarray[$i]['author']     = $dataarray[0];
  $entryarray[$i]['word']       = $dataarray[1];
  $entryarray[$i]['definition'] = $dataarray[2];  
}

$entryarray = columnSort($entryarray, 'word');

printarray($entryarray,$part);
?>
aerodromoi
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks again

Post by akimm »

Though, I feel bad because, I didn't mean for the entire set of functions to be written by you, however, thank you very much.

Just so I am understanding if I include <a href="script.php?part=a"> that should bring up my a, if a exists at all.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

The script works partially.

Post by akimm »

word: def: author: Contributed by Pulchritude = Great beauty

which is the first word I have come up, but Pulchritude is not the name of the person, I will edit that code myself, don't ask why it's doing that, because, I could tell you, moreover, my problem is no matter what the script.php?b or script.php?y it always brings up all the file contents, rather than just one set of words which is what I need.

script.php?a returns an error
"Warning: fread(): Length parameter must be greater than 0. in /nfs/cust/8/25/05/650528/web/script.php on line 7
word: def: author: "
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: The script works partially.

Post by aerodromoi »

akimm wrote:word: def: author: Contributed by Pulchritude = Great beauty

which is the first word I have come up, but Pulchritude is not the name of the person, I will edit that code myself, don't ask why it's doing that, because, I could tell you, moreover, my problem is no matter what the script.php?b or script.php?y it always brings up all the file contents, rather than just one set of words which is what I need.

script.php?a returns an error
"Warning: fread(): Length parameter must be greater than 0. in /nfs/cust/8/25/05/650528/web/script.php on line 7
word: def: author: "
You'll have to use the full url as I indicated: script.php?part=a or script.php?part=b
If the variable "part" is empty or not set at all, the script will return all entries.
Furthermore, you'll have to stick to the syntax for the flatfile, otherwise the script won't know whether it's a name or a word or a definition.

Ok, I should check whether a file exists and whether it's empty before I start...

aerodromoi

ps: Here's a working example:
http://dev.marcschnabel.de/thread49384/script.php
http://dev.marcschnabel.de/thread49384/ ... php?part=a
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

akimm wrote:However, it is not like the working examples, I used exactly as you prescribed. Though, I have 3-5 words and I try to use the full URL and it doesn't bring up the words, it says it has no entrys. I also am trying to add "\n"; to different parts of the code to allow for a new line like in places where there are more than one word on any page, so that it allows some space. I tried <br /> and that isn't doing the trick either, I have been tweaking it a lot but nothing thus far has prevailed.
I'm no clairvoyant - you'll have to post the code you modified and a sample flatfile so that one can check what's wrong.
If there are no words beginning with b, the script will tell you that. However, do take one step at a time. As long as the script does
not work for you, don't waste time on its looks.

btw: As I've already told you - there's quite a bunch of capable developers out there - you don't have to pm me.

aerodromoi
Post Reply