Tag Sorting PHP

To make the various tag pages on this site (eg; fanfic ship tags) I used the plugin shortcode exec php to insert the following code into a page;

$dbhost = ‘localhost’;
$dbuser = ‘dbname’;
$dbpass = ‘pass’;
$dbname = ‘dbname’;

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’ . mysql_error());
mysql_select_db($dbname);

$sql = “SELECT wp_terms.name, wp_terms.slug FROM wp_terms, wp_term_taxonomy WHERE wp_term_taxonomy.term_id = wp_terms.term_id AND wp_term_taxonomy.taxonomy = ‘post_tag’ ORDER BY name”;
$result = mysql_query($sql) or die(mysql_error());

$letter = ‘a’;
$lettered = false;

echo “<p align=’center’><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> </p>”;

// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
$name = $row['name'];
if ($name[0] == ‘s’ && $name[1] == ‘h’ && $name[2] == ‘i’ && $name[3] == ‘p’ && $name[4] == ‘:’ && $name[5] == ‘ ‘ ) {
$name = substr($name, 6);
$slug = $row['slug'];

if ($name[0] == $letter && $lettered == false) {
echo “<a name=’$letter’></a><b>” . ucfirst($letter) . “</b><br>”;
$lettered = true;
}
else if ($name[0] != $letter) {
while ($name[0] != $letter) {
$letter = ++$letter;
echo “<a name=’$letter’></a><b>” . ucfirst($letter) . “</b><br>”;
}
}

echo “<a href=’https://psychopixi.com/tag/$slug/’>” . ucwords($name) . “</a>”;
echo “<br />”;
}
}

while ($letter != ‘z’) {
$letter = ++$letter;
echo “<a name=’$letter’></a><b>” . ucfirst($letter) . “</b><br>”;
}

// Close the database connection
mysql_close($conn);

Leave a Reply