Page 1 of 1

Problems within the code, inhibit it's function, any help?

Posted: Fri Jun 02, 2006 10:04 am
by akimm
Through many pains and labors the kind folks of this forum helped me develop this code I have thus far. Especially aerodromoi, whom by the way is very helpful, I have this much completed. What it's purpose is, what it should be doing is from this link scripts.php?part=p is should draw from the textfile (words.txt) the word Pulchritude and then output it to a specific page for p's only. However, whenever I click the letters I know words exist for it still comes up as if there is nothing there.

I'm been trying to mess with this code for about 10 hours, to make it work for me, I am just not skilled enough ti mind the (most likely) miniscule error, that exists which inhibits the programs functionality. If you can help me I appreciate it, in the meantime I will continue to try to figure this thing out.

Thanks!


This is script.php:

Code: Select all

<?php 
$part     = substr($_GET['part'], 0, 1); 
if (empty($part)) $part = "empty"; 
  
$fname    = "words.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 "".$entryarray[$i]['word']." ".$entryarray[$i]['definition'] . $entryarray[$i]['author']." . <br /> . \n"; 
      $found = true; 
    } 
  } 
  if (!$found) echo "Sorry - no entries found!"; 
} 

$entryarray_old = explode("<br />",$string); 
$entryarray = array(); 

for ($i=0;$i<count($entryarray_old);$i++){ 
  $dataarray = explode("<br />","<br />", $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); 
?>
Here is the text file words.txt

Code: Select all

Pulchritude = Great beauty

Contributed by: Chris

Shibby = sweet

Contributed by: jarred

freaktard = A combination of freak and retarded, usually used as someone who is stupid

contributed by: Malina

Re: Problems within the code, inhibit it's function, any hel

Posted: Fri Jun 02, 2006 10:58 am
by aerodromoi
akimm wrote:Through many pains and labors the kind folks of this forum helped me develop this code I have thus far. Especially aerodromoi, whom by the way is very helpful, I have this much completed. What it's purpose is, what it should be doing is from this link scripts.php?part=p is should draw from the textfile (words.txt) the word Pulchritude and then output it to a specific page for p's only. However, whenever I click the letters I know words exist for it still comes up as if there is nothing there.

I'm been trying to mess with this code for about 10 hours, to make it work for me, I am just not skilled enough ti mind the (most likely) miniscule error, that exists which inhibits the programs functionality. If you can help me I appreciate it, in the meantime I will continue to try to figure this thing out.

Thanks!
Thanks for the praise ;)

Here's a working example using your words and definitions: http://dev.marcschnabel.de/thread49384/array.php
I changed the code slightly to include links, to check whether the flatfile actually exists and to deal with upper and lowercase syntax
(which caused the error you mentioned).

Code: Select all

<?php
$part     = strtolower(substr($_GET['part'], 0, 1));
if (empty($part)) $part = "empty";
$fname    = "test2.txt";

if (!file_exists($fname)) die("Sorry - the file you specified does not exist");
$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 (strtolower($sorted[$j][$column]) > strtolower($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" && strtolower(substr($entryarray[$i]['word'], 0, 1)) == $part) || $part == "empty"){
      if (strtolower(substr($entryarray[$i]['word'], 0, 1)) != $char) {
        $char = strtolower(substr($entryarray[$i]['word'], 0, 1)); 
        echo "<h2><a href="".$_SERVER['PHP_SELF'];
        if ($part != $char) echo "?part=".$char;
        echo "">- ".$char." -</a></h2>\n";
      }
      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);
?>
the flatfile:

Code: Select all

Chris#datasep#Pulchritude#datasep#Great beauty#entrysep#
Chris#datasep#pulcher#datasep#beautiful#entrysep#
jarred#datasep#shibby#datasep#sweet#entrysep#
Malina#datasep#freaktard#datasep#A combination of freak and retarded, usually used as someone who is stupid
cy,
aerodromoi

btw: This code definitely looks messy - I must get a grip on OOP sometime... ;)

Well Thank you

Posted: Fri Jun 02, 2006 11:50 am
by akimm
After you left the hint "btw there are plenty of other good developers" I was hoping not to trouble you again with this, but Thanks. I will study this code now and try to pick up some tricks. Thanks for the help I am going to implement it immediately and provide details on whether it works now..

Re: Well Thank you

Posted: Fri Jun 02, 2006 11:57 am
by aerodromoi
akimm wrote:After you left the hint "btw there are plenty of other good developers" I was hoping not to trouble you again with this, but Thanks. I will study this code now and try to pick up some tricks. Thanks for the help I am going to implement it immediately and provide details on whether it works now..
It's no trouble at all, I just prefer keeping questions in the thread.
Having a third or nth pair of eyes taking a closer look at the code won't do any harm,
it usually speeds up things.

aerodromoi :)

Well Friend.

Posted: Fri Jun 02, 2006 12:01 pm
by akimm

Re: Well Friend.

Posted: Fri Jun 02, 2006 12:10 pm
by aerodromoi
akimm wrote:I don't want to upset you.. but as of now the same problems exist.

you can check it yourself, maybe I'm wrong, but it appears I am not..

http://www.akimm.com/script.php?part=p
http://www.akimm.com/script.php?part=P
http://www.akimm.com/script.php?part=F
http://www.akimm.com/script.php?part=f
http://www.akimm.com/script.php?part=S
http://www.akimm.com/script.php?part=s
Have you changed the variable $fname? Because if you haven't, it's no surprise - text2.txt does not exist on your server
(at least not under http://www.akimm.com/text2.txt). Sorry, I changed it for testing (ok, that's mean).

aerodromoi

No sir.

Posted: Fri Jun 02, 2006 12:11 pm
by akimm
excuse me.

Posted: Fri Jun 02, 2006 12:17 pm
by aerodromoi
And off we go - it works! :D 8)

btw: Right now, I've got a network timeout every five seconds for devnet ... :(

yes,

Posted: Fri Jun 02, 2006 1:03 pm
by akimm
It works, I just have a little problem, but it was caused by me i'm most sure, so I will fix it.

Re: yes,

Posted: Sun Jun 04, 2006 2:45 am
by aerodromoi
Here's an OOP version of the same script:

Code: Select all

<?php
class Dictionary {

    var $outputstring;
    
    function Dictionary($backend)
    {   
       if (!file_exists($backend)) die("Sorry - the file you specified does not exist!");
       $handle   = fopen($backend, "r");
       $string   = fread($handle, filesize($backend));
       fclose($handle);
       $this->setdata($string);
    }
    
    
    function setdata($string)
    {
        if (!is_string($string)) {
            return false;
        }
        $this->entryarray = explode("#entrysep#",$string);
        
        for ($i=0;$i<count($this->entryarray);$i++){
           $dataarray = explode("#datasep#",$this->entryarray[$i]);           
           $this->uentryarray[$i]['author']     = $dataarray[0];
           $this->uentryarray[$i]['word']       = $dataarray[1];
           $this->uentryarray[$i]['definition'] = $dataarray[2];
        }
        $this->entryarray = $this->columnSort($this->uentryarray, "word");
        return $this->entryarray;
    }
    
    function columnSort($unsorted, $column) 
    {
        $sorted = $unsorted;
        for ($i=0; $i < sizeof($sorted)-1; $i++) {
           for ($j=0; $j<sizeof($sorted)-1-$i; $j++){
              if (strtolower($sorted[$j][$column]) > strtolower($sorted[$j+1][$column])) {
                $tmp = $sorted[$j];
                $sorted[$j] = $sorted[$j+1];
                $sorted[$j+1] = $tmp;
              }
           }
        }
        return $sorted;
    }

    function getdata($part)
    {
        $this->found = false;
        
        for ($i=0;$i<count($this->entryarray);$i++){
          if (($part != "empty" && strtolower(substr($this->entryarray[$i]['word'], 0, 1)) == $part) || $part == "empty"){  
            if (strtolower(substr($this->entryarray[$i]['word'], 0, 1)) != $char) {
               $char = strtolower(substr($this->entryarray[$i]['word'], 0, 1)); 
               $this->outputstring .= "<h3><a href=\"".$_SERVER['PHP_SELF'];
               if ($part != $char) $this->outputstring .= "?part=".$char;
               $this->outputstring .= "\">- ".$char." -</a></h3>\n";
            }           
            $this->outputstring .= "<b>word:</b> ".$this->entryarray[$i]['word']." "; 
            $this->outputstring .= "<b>definition:</b> ".$this->entryarray[$i]['definition']." "; 
            $this->outputstring .= "<b>author:</b> ".$this->entryarray[$i]['author']."<br />\n";  
            
            $this->found = true;
          }         
        }
        if (!$this->found){
            $this->outputstring  = "No entries found.";
        }  
        
        return $this->outputstring;
    }
    
    function printdata($part)
    {
        $this->message  = "<h2>Dictionary ";
        if ($part != "empty") $this->message .= "(".strtoupper($part).")";
        $this->message .= "</h2>\n".$this->getdata($part)."\n";
        return $this->message;
    }
    
    function partf($part){
        $this->part = strtolower(substr($part, 0, 1));
        if (empty($this->part) || !eregi("^[a-z]$",$this->part)) $this->part = "empty";
        return $this->part;
    }
}


$xdic = new Dictionary("test2.txt");
$part = $xdic->partf($_GET['part']);
print $xdic->printdata($part);
?>
aerodromoi