Results 1 to 6 of 6

Thread: [RESOLVED] Need help on fixing my code that is suppose to highlight one file in a filelist

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Resolved [RESOLVED] Need help on fixing my code that is suppose to highlight one file in a filelist

    The Solution: http://www.vbforums.com/showpost.php...71&postcount=4


    My Original Question:

    Here is a live preview of the code:

    http://www.alexdata.com/testing/

    On this page only the file containing the same number as the WEEK we're in +1, should be highlighted (aka: highlight the next week, the coming week)

    Right now it highlights lots of weeks, starting with the current week and highlighting the rest of the availible weeks(files)... Any idea where I may have done a biig mistake, or just a stupid logical error?

    Here's the code:
    PHP Code:
    <center><br><br>                    <!-- CENTER EVERYTHING ON THIS PAGE -->

    <?PHP
    $AllowedExtension
    =array('txt');                    //**List Of FileExtensions You Allow To Show**\\
    $FileCount=0;
    $TheDir=".";
    $DirectoryFiles=opendir("$TheDir");                //**The Directory, Where Your Files Are Stored**\\


    $TheDate date("d.F.Y");                        //**The Current Date Today**\\
    $TheTime date("\k\l\ \ H:i");                    //**The Current Time Right Now**\\
    $ThisDay date("D");                            //**This Days Short Name; ex: Sun**\\


    $WeekNumber=date("W");                            //**The Current Week Number**\\
    $NextWeekNumber=date("W");                        //**The Next Weeks Number**\\


    $CheckWeek="ThisWeek: " $WeekNumber;            //**Checks This Weeks Number**\\
    $CheckNextWeek="NextWeek: " $NextWeekNumber;    //**Checks Next Weeks Number**\\
    ?>


    <?PHP                                            //**THIS WRITES THE TOP HEADER TEXT ON THE PAGE**\\
    Echo "<h3><i><u>Week:</u></i> " $WeekNumber " @@ <i><u>Day:</u></i> " $ThisDay " @@ <i><u>Date:</u></i> " $TheDate " @@ <i><u>Time:</u></i> " $TheTime "</h3>";

    Echo 
    "The SpecialDay we're looking for is: <b>".$ThisDay."</b> and that should highlight the file containing this number: <b>".$NextWeekNumber."</b><br><br><br>";
    ?>


    <?PHP

    //**CHECKS ALL FILES IN DIRECTORY FOR ALLOWED EXTENSIONS AND ADD THEM TO THE ARRAY**\\
    while($CurrentFileName readdir($DirectoryFiles))
    //while-start
    if(in_array(strtolower(substr($CurrentFileName,-3)),$AllowedExtension))
    {
    $FileArray[]=$CurrentFileName;
    Sort($FileArray);
    Reset($FileArray);
    $FileCount=$FileCount+1;
    }

    //while-End

    //**WHEN DONE CHECKING THE DIRECTORY AND ADDING FILENAMES TO THE ARRAY**\\

    $TotalArrayFilenameCount=count($FileArray);            //**Count of Filenames in  the Array**\\

    $FileListWidth=2;                                    //**DEFINES HOW MANY FILES TO LIST PR ROW, BEFORE JUMPING TO NEXT LINE**\\
    $FileListCounter=0;                                    //**EMPTIES THE COUNTER FOR THE "HOW MANY FILES TO LIST PR ROW" HEHE**\\
    //$FileListCounter=$FileListCounter-1;


    //** USE THE COUNTED FILENAMES IN THE ARRAY TO PROCESS THE DIFFRENT FILENAMES AND OUTPUT SOME RESULT**\\
        
    for($IncrementCount=0$IncrementCount $TotalArrayFilenameCount$IncrementCount++){ //START THE "FOR" LOOP\\

    //if($ThisDay=="Sun") <--ORIGINAL REMOVED FOR TEST PURPOSES    //**CHECKS TO SEE IF IT IS THE SPECIAL DAY TODAY**\\
    if($ThisDay==$ThisDay)                                    //**CHECKS TO SEE IF IT IS THE SPECIAL DAY TODAY**\\
    {
    //**IF THIS IS THE SPECIAL DAY THAT WE ARE LOOKING FOR THEN DO SPECIAL PROCEDURES FOR THIS DAY AND NEXT WEEK**\\
    //**SPECIAL**\\
    $ToFindNow=$NextWeekNumber;                            //**PUT THIS WEEKS NUMBER INTO THE ToFindNow VARIABLE**\\
    $ToLookNow=$FileArray[$IncrementCount];                //**THE ToLookNow VARIABLE GETS INFO FROM THE ARRAY RECORD EQUAL TO IncrementCount VARIABLE**\\
    echo "SpecialDay";
    }
    else
    {
    //**THIS IS JUST ANY OTHER DAY THAN THE SPECIALDAY, DO NORMAL PROCEDURES FOR THIS WEEK**\\
    $ToFindNow=$WeekNumber;                                //**PUT THIS WEEKS NUMBER INTO THE ToFindNow VARIABLE**\\
    $ToLookNow=$FileArray[$IncrementCount];                //**THE ToLookNow VARIABLE GETS INFO FROM THE ARRAY RECORD EQUAL TO IncrementCount VARIABLE**\\
    echo "NormalDay";
    }


    ereg($ToFindNow$ToLookNow$Matches);                //**CHECK THE TWO STRINGS TO SEE IF THEY MATCH, AND PUT THE RESULT INTO ARRAY Matches**\\
    //if($Matches[0] == $ToFindNow)
    //if (In_String($ToFindNow,$ToLookNow,1))

    if((int)$Matches[0] == (int)$ToFindNow)                //**CHECK IF THE STRINGS ARE EQUAL**\\
    //EQUAL!!
    echo "<b style='border:1px; border-color:black; border-style:solid;'>YES: " $FileArray[$IncrementCount] . "</b>&nbsp;&nbsp;&nbsp;";
    //echo " STR:" . strripos($Matches[0],$ToLookNow) . " | " . (int)$Matches[0] . ":" . (int)$ToLookNow ."<br>";
    }
    else
    //DIFFERENT!!
    echo "<i style='border:1px; border-color:black; border-style:solid;'>NO: " $FileArray[$IncrementCount] . "</i>&nbsp;&nbsp;&nbsp;";
    //echo " STR:" . strripos($Matches[0],$ToLookNow) . " | " . (int)$Matches[0] . ":" . (int)$ToLookNow ."<br>";
    }

    //**SETS THE DISPLAY OF FIlENAMES ON THE PAGE TO ROWS OF THREE FILES PR ROW**\\
    $FileListCounter=$FileListCounter+1;
    if ( 
    $FileListCounter==$FileListWidth ) {$FileListCounter=0; echo"<br><br>";}


        } 
    //ENDS THE "FOR" LOOP\\

    ?>

    <br><br>THIS SHOULD ONLY HIGHLIGHT <b>ONE</b> FILE... RIGHT NOW IT HIGHLIGHTS MANY FILES      <!-- END -->
    I've also added the file "index.php" that contains the whole code as an attachment...
    Thanks a lot for your help...!!!
    Attached Files Attached Files
    Last edited by alexdata; Sep 24th, 2009 at 04:50 AM.
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Need help on fixing my code that is suppose to highlight one file in a filelist

    Your code is terribly hard to read; please practice appropriate indentation, and don't over-comment.

    Anyway, try changing this part...
    Code:
    ereg($ToFindNow, $ToLookNow, $Matches);				//**CHECK THE TWO STRINGS TO SEE IF THEY MATCH, AND PUT THE RESULT INTO ARRAY Matches**\\
    //if($Matches[0] == $ToFindNow)
    //if (In_String($ToFindNow,$ToLookNow,1))
    
    if((int)$Matches[0] == (int)$ToFindNow)
    ...to just this...
    Code:
    if(ereg($ToFindNow, $ToLookNow, $Matches))
    If a match is found, the if statement will evaluate to something non-false; you don't need to check the $Matches array.

    Also note that ereg() is "DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged."

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Red face Re: Need help on fixing my code that is suppose to highlight one file in a filelist

    Quote Originally Posted by SambaNeko View Post
    Your code is terribly hard to read; please practice appropriate indentation, and don't over-comment.
    Hehe, you're absolutely correct, telling me that my indent'ing-skills are a bit weak, that is why I comment a lot, it is readable to me,
    but then again, maybe not so much readable to others, my appologies there...

    Thanks for reading the code, anyways!!

    Quote Originally Posted by SambaNeko View Post
    Anyway, try changing this part...
    PHP Code:
    ereg($ToFindNow$ToLookNow$Matches);            
    if((int)
    $Matches[0] == (int)$ToFindNow
    ...to just this...
    PHP Code:
    if(ereg($ToFindNow$ToLookNow$Matches)) 
    If a match is found, the if statement will evaluate to something non-false; you don't need to check the $Matches array.
    I did change the code to your suggestion, and like a charm, it worked!
    Somehow I have a hard time understanding the real difference between your idea; "evaluate something non-false"
    and my idea "evaluate if something is true" ... and I also dont understand why mine fails, and yours work...

    Quote Originally Posted by SambaNeko View Post
    Also note that ereg() is "DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged."
    This is indeed true... but when i tried with the preg_match() my server somehow did not manage to produce anything but errors,
    even though it worked on a friend of mine's server... so there you go... i had to go back to that deprecated, old code...

    I will ofcourse look into that... And... If by any chance you could try to suggest a Preg_Match() solution to do the above task,
    then im more than willing to RE-TRY that on my server... Thanks a lot...
    Last edited by alexdata; Sep 23rd, 2009 at 12:20 PM.
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Thumbs up Re: [RESOLVED] Need help on fixing my code that is suppose to highlight one file in a

    So here is the full working code for a "simple FILE LISTER" with some extra functionality as to drawing the site differently, depending on what day it is...

    Live example: http://www.alexdata.com/CODEBASE/FileLister/

    PHP Code:
    <center><br><br>                                <!-- CENTER EVERYTHING ON THIS PAGE -->

    <?PHP
    $AllowedExtension
    =array('txt');                    //**List Of FileExtensions You Allow To Show**\\
    $FileCount=0;
    $TheDir=".";
    $DirectoryFiles=opendir("$TheDir");                //**The Directory, Where Your Files Are Stored**\\


    $TheDate date("d.F.Y");                        //**The Current Date Today**\\
    $TheTime date("\k\l\ \ H:i");                    //**The Current Time Right Now**\\
    $ThisDay date("D");                            //**This Days Short Name; ex: Sun**\\


    $WeekNumber=date("W");                            //**The Current Week Number**\\
    $NextWeekNumber=date("W")+1;                        //**The Next Weeks Number**\\


    $CheckWeek="ThisWeek: " $WeekNumber;            //**Checks This Weeks Number**\\
    $CheckNextWeek="NextWeek: " $NextWeekNumber;    //**Checks Next Weeks Number**\\
    ?>


    <?PHP                                            //**THIS WRITES THE TOP HEADER TEXT ON THE PAGE**\\
    Echo "<h3><i><u>Week:</u></i> " $WeekNumber " @@ <i><u>Day:</u></i> " $ThisDay " @@ <i><u>Date:</u></i> " $TheDate " @@ <i><u>Time:</u></i> " $TheTime "</h3>";

    Echo 
    "The Special Day we're looking for is: <b>".$ThisDay."</b> and if we are at that day, then that should highlight the file containing next weeks number: <b>".$NextWeekNumber."</b><br><br><br>";
    ?>


    <?PHP

    //**CHECKS ALL FILES IN DIRECTORY FOR ALLOWED EXTENSIONS AND ADD THEM TO THE ARRAY**\\
    while($CurrentFileName readdir($DirectoryFiles))
    //while-start
        
    if(in_array(strtolower(substr($CurrentFileName,-3)),$AllowedExtension))
        {
        
    $FileArray[]=$CurrentFileName;
        
    Sort($FileArray);
        
    Reset($FileArray);
        
    $FileCount=$FileCount+1;
        }
    //while-End

    //**WHEN DONE CHECKING THE DIRECTORY AND ADDING FILENAMES TO THE ARRAY**\\
    $TotalArrayFilenameCount=count($FileArray);            //**Count of Filenames in  the Array**\\
    $FileListWidth=2;                                    //**DEFINES HOW MANY FILES TO LIST PR ROW, BEFORE JUMPING TO NEXT LINE**\\
    $FileListCounter=0;                                    //**EMPTIES THE COUNTER FOR THE "HOW MANY FILES TO LIST PR ROW" HEHE**\\
    ?>

    <!-- START OF TABLE DESIGN -->
    <table cellpadding="10" cellspacing="10" style='border:3px; border-color:black; border-style:solid;' align="center" valign="center"><tr>

    <?PHP
    //** USE THE COUNTED FILENAMES IN THE ARRAY TO PROCESS THE DIFFRENT FILENAMES AND OUTPUT SOME RESULT**\\
    for($IncrementCount=0$IncrementCount $TotalArrayFilenameCount$IncrementCount++)
    //STARTS THE "FOR" LOOP\\
        //if($ThisDay=="Sun") <--ORIGINAL REMOVED FOR TEST PURPOSES    //**CHECKS TO SEE IF IT IS THE SPECIAL DAY TODAY**\\
        
    if($ThisDay==$ThisDay)                                    //**CHECKS TO SEE IF IT IS THE SPECIAL DAY TODAY**\\
        
    {
        
    //**IF THIS IS THE SPECIAL DAY THAT WE ARE LOOKING FOR THEN DO SPECIAL PROCEDURES FOR THIS DAY AND NEXT WEEK**\\
        //**SPECIAL**\\
        
    $ToFindNow=$NextWeekNumber;                            //**PUT THIS WEEKS NUMBER INTO THE ToFindNow VARIABLE**\\
        
    $ToLookNow=$FileArray[$IncrementCount];                //**THE ToLookNow VARIABLE GETS INFO FROM THE ARRAY RECORD EQUAL TO IncrementCount VARIABLE**\\
    //echo $NextWeekNumber;        
    //echo "SpecialDay";
        
    }
        else
        {
        
    //**THIS IS JUST ANY OTHER DAY THAN THE SPECIALDAY, DO NORMAL PROCEDURES FOR THIS WEEK**\\
        
    $ToFindNow=$WeekNumber;                                //**PUT THIS WEEKS NUMBER INTO THE ToFindNow VARIABLE**\\
        
    $ToLookNow=$FileArray[$IncrementCount];                //**THE ToLookNow VARIABLE GETS INFO FROM THE ARRAY RECORD EQUAL TO IncrementCount VARIABLE**\\
        //echo "NormalDay";
        
    }

        
    //**If a match is found, the if statement will evaluate to something non-false; you don't need to check the $Matches array**\\
        
    if(ereg($ToFindNow$ToLookNow$Matches))
        { 
    //EQUAL!!
        
    echo "<td style='border:1px; border-color:black; border-style:solid;'><a href='".$FileArray[$IncrementCount]."'><b>NextWeek: ".$FileArray[$IncrementCount]."</b></a></td>";
        
    //echo " STR:" . strripos($Matches[0],$ToLookNow) . " | " . (int)$Matches[0] . ":" . (int)$ToLookNow ."<br>";
        
    }
        else
        { 
    //DIFFERENT!!
        
    echo "<td style='border:1px; border-color:black; border-style:solid;'><a href='".$FileArray[$IncrementCount]."'><i>".$FileArray[$IncrementCount]."</i></a></td>";
        
    //echo " STR:" . strripos($Matches[0],$ToLookNow) . " | " . (int)$Matches[0] . ":" . (int)$ToLookNow ."<br>";
        
    }


    //**SETS THE DISPLAY OF FIlENAMES ON THE PAGE TO ROWS OF THREE FILES PR ROW**\\
    $FileListCounter=$FileListCounter+1;
    if ( 
    $FileListCounter==$FileListWidth ) {$FileListCounter=0; echo"</tr><tr>";}


    //ENDS THE "FOR" LOOP\\

    ?>

    <!-- END OF TABLE DESIGN -->
    </tr></table>




    <!-- REMOVED::
    //X ereg($ToFindNow, $ToLookNow, $Matches);                //**CHECK THE TWO STRINGS TO SEE IF THEY MATCH, AND PUT THE RESULT INTO ARRAY Matches**\\
    ////if($Matches[0] == $ToFindNow)
    ////if (In_String($ToFindNow,$ToLookNow,1))
    //X if((int)$Matches[0] == (int)$ToFindNow)                //**CHECK IF THE STRINGS ARE EQUAL**\\
    -->


    <!-- <br><br>THIS SHOULD ONLY HIGHLIGHT <b>ONE</b> FILE... RIGHT NOW IT HIGHLIGHTS MANY FILES  -->
    <br><br>WORKS NOW!!
    <!-- END -->
    Last edited by alexdata; Sep 23rd, 2009 at 12:20 PM.
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Need help on fixing my code that is suppose to highlight one file in a

    just as an aside -- if you would like to post code for others to use, I'd suggest using [php] and [/php] tags rather than the [highlight] tags that you're using. the highlight tags produce HTML entities where they shouldn't (look through your post and I'm sure you'll see some; line 38 for example), and the line numbering makes it hard to copy and paste to work with.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Re: [RESOLVED] Need help on fixing my code that is suppose to highlight one file in a

    Quote Originally Posted by kows View Post
    just as an aside -- if you would like to post code for others to use, I'd suggest using [php] and [/php] tags
    Excellent idea Kows... I hope to have pleased you, and others, by following up on that idea... I've now corrected it to use php tags....

    Thanks...


    -----AND HERE's A REPEAT QUESTION-----

    Somehow I have a hard time understanding the real difference between SambaNeko's idea; "evaluate something non-false"
    and my own idea "evaluate if something is true" ... and I also dont understand why mine fails, and SambaNeko's code works...
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width