SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Regex Help

Hybrid View

  1. #1
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex Help

    Hello Guys,

    I am having the database table with say 100 records.
    I want to display the records sorted by title field.
    How I want to display the records is that

    A

    All the titles starting from A
    ----
    -----

    B

    All the titles starting from B
    ----
    -----

    and so on

    I am having array as
    PHP Code:
    $alpha_array=array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
    $i=0;
    while(
    $row=@mysql_fetch_array($result))
    {
    $title=$row['title'];
    //Here i want to match the first character of $title with $alpha_array[$i]
    //if matches then display the alphabet and then records that start with that //alphabet
    $id=$row['id'];
    $i++;

    Can anyone help me out please?
    Thanks,
    Barbara

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use ORDER BY Title in your query. This way you only have to check the first character of the title.
    Be sure to trim the Titles before inserting these records in the table, so you won't have any whitespace characters at the beginning of the Title.
    It would be best to group special characters too.

    PHP Code:
    $result mysql_query('Select * from Records order by Title ASC');
    $previousCharacter '';
    while (
    $row mysql_fetch_array($result)) {
        
    $char ctype_alphastrtouppersubstr(ltrim($row['Title']), 01) ) ) ? $char 'Other';
        if (
    $char != $previousCharacter) {
            echo 
    "<h3>$char</h3>";
            
    $previousCharacter $char;
        }
        echo 
    "<div><strong>$row[Title]</strong> - $row[Author]</div>";

    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a quick and easy tutorial showing how to do it
    http://www.phpfreaks.com/tutorials/146/0.php
    chrome is a wrapper that combines a browser with spyware

  4. #4
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    It is possible to reduce the functions required to pull the character.
    Php allows us to walk through strings just as it was an array, character by character. This is the preferred method to use if possible as it is lightning fast.

    PHP Code:
    if (!ctype_alpha($row['Title'][0])) $char 'Other';
        else 
    $char strtoupper($row['Title'][0]); 
    Of course the sentence above can be written as a ternary expression as well.

  5. #5
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Thanks to all people,
    @hexburner
    In the given code I just made one change and works great.
    PHP Code:
    $result mysql_query('Select * from Records order by Title ASC');
    $previousCharacter '';
    while (
    $row mysql_fetch_array($result)) {
        
    //$char = ctype_alpha( strtoupper( substr(ltrim($row['Title']), 0, 1) ) ) ? $char : 'Other';

    $char =strtouppersubstr(ltrim($row['Title']), 01) )  ;
        if (
    $char != $previousCharacter) {
            echo 
    "<h3>$char</h3>";
            
    $previousCharacter $char;
        }
        echo 
    "<div><strong>$row[Title]</strong> - $row[Author]</div>";

    @-T-
    The link you given works great

    @TheRedDevil
    Characters are displaying but for each of the record like
    A
    ATitle1.
    A
    ATitle2
    B
    BTitle1
    B
    BTitle2

    May be I am doing certain wrong.
    PHP Code:
    if (!ctype_alpha($row['Title'][0])) $char 'Other';
    else 
    $char strtoupper($row['Title'][0]); 
    Check Here
    PHP Code:
    if (!ctype_alpha($title[0]))
    $char 'Other';
    else
    {
    $char strtoupper($title[0]); 
    echo 
    "<h3>$char</h3>";

    Thanks guys for the replies and giving solution.
    Barbara

  6. #6
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The code I wrote earlier would only replace this line:
    $char = ctype_alpha( strtoupper( substr(ltrim($row['Title']), 0, 1) ) ) ? $char : 'Other';

    If the other code is left intact it will work exactly as it did with the line above, only that it require less resources.


Bookmarks

Posting Permissions

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