Display mysql database records alphabetical order using php

Display mysql database records alphabetical order using php

Jan 16, 2018 | MySql, PHP

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.

Being Idea is a web platform of programming tutorials to make better programming skills and provides Software Development Solutions.

0 Comments

Leave a Reply