www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: Clickable Aphabetical Listing from MySQL

  1. #1
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197

    Clickable Aphabetical Listing from MySQL

    Hi

    I have a table and I want to group the entrys by the first letter and offer a link at the top of the page so that when the user clicks it all enteries found starting with the letter say R is displayed

    has anyone seen a tutorial showing this

    thank you

  2. #2
    Join Date
    Oct 2006
    Posts
    1,297
    PHP Code:
    $sql "Select * From `Listing` Where `AlphaField` Like 'R%';" 

  3. #3
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    thank you for the reply, I need it to loop through the alphabet.

    at the top of my page I want to have

    A B C D... and if there is something found that starts with A then I want the letter A to be linkable so that all entrys found that starts with A is dispalyed

  4. #4
    Join Date
    Oct 2006
    Posts
    1,297
    Perhaps you are talking about this -- which I just converted from some ASP code I have. This creates a hyperlink index at the top of a table and insures matching anchors (bookmarks) for every letter of the alphabet -- even if the selected data may happen to be missing a few letters of the alphabet.
    PHP Code:
    <?php
    function FillIndex($prev$next)
    {
        
    $str '';
        for (
    $chr ord($prev) + 1$chr <= ord($next); ++$chr):
            
    $str .= '        <a name="' .chr($chr). '"></a>' ."\n";
        endfor;
        return 
    $str;
    }
    ?>
    <div style="text-align:center;">
      <table>
        <tr>
          <th>Name</th>
          <th>Description</th>
        </tr>
    <?
        $query 
    "Select * From `Listings` Order by `AlphaField`;";
        
    $result mysql_query($query) or die (mysql_error());
        
    $row mysql_fetch_assoc($result);
        if (
    $row):
    ?>
        <tr>
          <td colspan="2" style="text-align:right;"><span
           style="font:small-caps 10pt 'Courier New',monospace;">Index:
            <a href="#A">A</a>
            <a href="#B">B</a>
            <a href="#C">C</a>
            <a href="#D">D</a>
            <a href="#E">E</a>
            <a href="#F">F</a>
            <a href="#G">G</a>
            <a href="#H">H</a>
            <a href="#I">I</a>
            <a href="#J">J</a>
            <a href="#K">K</a>
            <a href="#L">L</a>
            <a href="#M">M</a>
            <a href="#N">N</a>
            <a href="#O">O</a>
            <a href="#P">P</a>
            <a href="#Q">Q</a>
            <a href="#R">R</a>
            <a href="#S">S</a>
            <a href="#T">T</a>
            <a href="#U">U</a>
            <a href="#V">V</a>
            <a href="#W">W</a>
            <a href="#X">X</a>
            <a href="#Y">Y</a>
            <a href="#Z">Z</a></span>
          </td>
        </tr>
    <?    else:    ?>
        <tr><td colspan="2">No listings records found.</td></tr>
    <?
        
    endif;
        
    $ctl "@";
        while(
    $row):
    ?>
        <tr>
          <td style="text-align:left; vertical-align:top;">
    <?
            $chr 
    strtoupper(substr($row['AlphaField'],0,1));
            if (
    $chr != $ctl):
                echo 
    FillIndex($ctl$byt);
            endif;
            
    $ctl $byt;
            echo 
    $row['AlphaField'];
    ?>
          </td>
          <td style="text-align:left; vertical-align:top;">
            <p><?=$row['Description']?></p>
            [ <a href="#Top">Top</a> ]
          </td>
        </tr>
    <?
            $row 
    mysql_fetch_assoc($result);
        endwhile;
    ?>
      </table>
    <?
        
    if ($ctl != 'Z'):
            echo 
    FillIndex($ctl'Z');
        endif;
    ?>
    </div>

  5. #5
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    So is this,

    Can you point out to me where in the PHP manual it shows syntax like you're showing? as far as I can see, PHP has no endif or colon based operations like what you're showing here.

  6. #6
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115

  7. #7
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    However, so_is_this,

    Using outdated paradigms can slow down your script, as well as make it unreadable. Readers of your script might not be familiar with PHP's outdated functions. Nevertheless, if outdated code is discovered, don't feel it is necessary to replace it. Just be sure to avoid using this code for future scripting.

    An example of an outdated paradigm, which many programmers seem to grasp onto, is the beginControlStructure .. endControlStructure; syntax:
    PHP Code:
    <?php

    // Bad/Outdated Practice

    while (1):
        print 
    "5";
        if (
    $idx++ == 5):
            break;
        endif;
    endwhile;

    // Better Practice 
    // (the code could be optimized though)

    while (1)
    {
        print 
    "5";
        if (
    $idx++ == 5) {
            break;
        }
    }

    ?>
    This is a bad practice for the following reasons:
    • It is not widely used, and therefore, many people learning the language will be confused by the two syntaxes.
    • It is not compatible with other languages, meaning that it is harder to read for people who are transitioning.
    • Most importantly, someday in the future it might be disabled, forcing you to rewrite the code that implemented it. Brackets, however, will always be a part of the PHP language.

    The above is but one outdated paradigm example seen in a great amount of PHP code. There are more. As a rule, you should follow the style present in the PHP manual. For the most part it is up to date. It also uses the latest functions for its examples. Regularly check the manual if you're considering extending PHP's functionality. In this manner, you won't end up writing functions that already exist.

    Taken From: Zend Technologies - Articles - Top 21 PHP progamming mistakes - Part I: Seven Textbook Mistakes.

  8. #8
    Join Date
    Oct 2006
    Posts
    1,297
    You cannot dictate style to other programmers. Each programmer is free to do as they see fit within the bounds of the language. If something is unacceptable, then the language itself should be changed to reflect that. As long as a language "insists" on continuing to support a thing, then that thing is free to be employed as seen fit by either the programmer or the employer of said programmer. You are certainly entitled to your opinions and others are certainly entitled to theirs. But, by the same token, I am entitled to mine.

    Thanks for your interest.

    Salud!

  9. #9
    Join Date
    Oct 2006
    Posts
    1,297
    Quote Originally Posted by chazzy
    Can you point out to me where in the PHP manual it shows syntax like you're showing? as far as I can see, PHP has no endif or colon based operations like what you're showing here.
    You could have just tried a sample of that syntax.

    Salud!

  10. #10
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    Quote Originally Posted by so_is_this
    You could have just tried a sample of that syntax.

    Salud!
    Trying a sample and seeing that it works is one thing, seeing actual documentation saying that it works is another. What you are posting could quite actually be a bug, not an alternate form of code.

    Also, about dictating coding styles, while it is probably wrong for someone to say "You have to write your code this way," most companies out in the real world do that. There are typically guidelines saying "you must format things a certain way."

    For example,

    In all cases of conditionals, the syntax to use shall always be

    Code:
    if(condition){
    code
    }
    else if(condition){
    code
    }
    else{
    code
    }
    The reason to use this format is to provide standard formatting for all developers to be able to understand. All deviant code usage should be found during code review periods.

    Caveats:
    - This is retroactive to all old code.
    - else if blocks can be omitted when unnecessary.
    - else blocks can contain nil code. (code that is useless/non existent)
    - You may not use a format like the below:
    Code:
    if(condition)
        code
    else
        code
    It's just typically part of best business practices. Have you developed in a team environment before?

    Also, you probably don't want to argue with Zend, since they are the ones that actually put together PHP.

  11. #11
    Join Date
    Oct 2006
    Posts
    1,297
    Quote Originally Posted by chazzy
    ... most companies out in the real world do that.
    I said that.
    Quote Originally Posted by chazzy
    Also, you probably don't want to argue with Zend, since they are the ones that actually put together PHP.
    Then they are the ones I have to thank for continuing to support -- and document -- such alternate structure syntax. I happen to prefer such alternate syntax -- as it is easier to read, reduces potential errors when a section of code is first written, and therefore makes such coding go faster due to a reduction in debugging efforts. My employers happen to agree with me.

    Salud!
    Last edited by so_is_this; 11-25-2006 at 10:59 PM.

  12. #12
    Join Date
    Oct 2006
    Posts
    1,297
    Quote Originally Posted by chazzy
    Trying a sample and seeing that it works is one thing, seeing actual documentation saying that it works is another. What you are posting could quite actually be a bug, not an alternate form of code.
    Lastly... I don't know what is the more ridiculous concept, that all of the following structural syntax might be a bug or that one needs documentation on all of the following structural syntax in order to know that it works as intended by the developers of the language.
    PHP Code:
    if():
    elseif():
    else:
    endif;

    while():
    endwhile;

    for():
    endfor;

    foreach():
    endforeach;

    switch():
    case:
    default:
    endswitch; 
    Though a relatively minor point, note that even the syntax coloring for this BB software knows that this syntax is perfectly valid.

  13. #13
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    so_is_this,

    Yes, documentation should be out there, and no I did not see this section in the manual before. Yes, it is an alternate syntax, but no, it is not widely used or supported. I bet that if you used that syntax in a real work place, and they reviewed your code, they would tell you to redo it since it's not standard code.

    People need documentation to understand what it is that they are using. A first time coder cannot generate a set of classes that will render pages, connect to databases, and write flat log files. It takes some time to understand the language, and the logic needed in order to write the code properly.

    All I'm trying to do is talk to you about coding best practices. I am not arguing with you that you cannot use the code syntax, I am trying to explain to you why, even though it's easier for you to read, it's not easier to read for ~95% of the PHP developers in the world. It is my opinion that you know your stuff, but if you want to be taken seriously you should follow standard development practices.

    (* note that if you had PM's turned on, I would have sent you most of this via PM)

    Just a test, to see how good the syntax highlighting is:
    PHP Code:
    IF(======== B); 
    That's not valid PHP. But does the code highlighter say it's valid?

    Edit: Yes, it does. So that means you can't count on the code parser here to show you what is valid/invalid PHP.

  14. #14
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    1,197
    Hi gokes

    interesting debate. I'm still struggling trying to get a alpha listing accros the top of a page so that users can click a letter to display all data that starts with that letter.

    thank you

  15. #15
    Join Date
    Oct 2006
    Posts
    1,297
    Quote Originally Posted by chazzy
    I bet that if you used that syntax in a real work place, and they reviewed your code, they would tell you to redo it since it's not standard code.
    I won that bet. You missed my earlier statement in this area:
    Quote Originally Posted by so_is_this
    My employers happen to agree with me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles