Skip to main content
Being Idea Innovations
Display mysql database records alphabetical order using php
Back to Blog

Display mysql database records alphabetical order using php

16 January 20181 min readMySql
Share:

Here we have a php array of alphabets and some numbers.Loop works for each alphabets take one by one and put into mysql query in where

$a_z=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','U','V','W','X','Y','Z','2','&');

Now use foreach loop with MySql query.

foreach ($a_z as $latters) {
	echo '<div class="big-section-title">
            		<h2><span>'.$latters.'</span></h2>
          </div>';

    echo '<table class="table table-bordered"><tbody><tr>';

    $sql =mysql_query("SELECT cat_title,cat_slug FROM tbl_designer WHERE cat_title LIKE '$latters%' ORDER BY cat_title ASC");
    while($rw=mysql_fetch_object($sql)){
    	echo '<td><a title="'.$rw->cat_title.'" href="'.SITE_URL.'by-designer/'.$rw->cat_slug.'">'.$rw->cat_title.'</a></td>';
    }

	echo '</tr></tbody></table>';						           
}

Database structure (This database store designers name)

CREATE TABLE IF NOT EXISTS `tbl_designer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cat_title` varchar(255) NOT NULL,
  `cat_slug` varchar(255) NOT NULL,
  `cat_description` text NOT NULL,
  `cat_image` varchar(255) DEFAULT NULL,
  `meta_title` varchar(255) DEFAULT NULL,
  `meta_description` text,
  `meta_keywords` text,
  `created` varchar(255) DEFAULT NULL,
  `modify` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

This tutorial a best example for learn about php array and loops and mysql query with where condition. Just use this code using mysql database connection in index file and run wher you want.

Share:

Want to talk tech?

We ship software that scales. Let's work together.

No long-term contracts
Senior engineers only
US · AU · NZ timezone coverage
14-day trial on retainers