list tablenames of mysql database by length of tablename

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jforce93
Forum Newbie
Posts: 2
Joined: Tue Sep 01, 2009 2:46 pm

list tablenames of mysql database by length of tablename

Post by jforce93 »

I am trying to create library catalog software, and I am trying to have it so when someone searches for a book by title, the shortest title comes up first, the second shortest second and so on. How would I do that? Here is my code:

Code: Select all

<html>
<body>
<h1> Here Are Your Results </h1>
<?php
 
$table = 'user';
//get names of tables
$result_handle = mysql_list_tables ($title)
   or die ("mysql_list_tables () failed with this error message: '" . mysql_error () . "'");
 
//number of tables
$number_tables = mysql_num_rows ($result_handle);
 
echo "Your search returned $number_tables table(s):<ol> number of books";
 
for ($index=0; $index < $number_tables; ++$index) {
$User_Input = str_ireplace(" ","",$title);
$Title_Of_Book = str_ireplace(" ","",$result_handle); 
$pos = strpos($Title_Of_Book,$User_Input);
if ($pos === true)
{
 
 
   echo '<li>', mysql_result ($result_handle, $index, 0), '</li>';
}
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: list tablenames of mysql database by length of tablename

Post by requinix »

Stick the table names into an array, then

Code: Select all

usort($array_of_table_names, create_function('$a,$b', 'return strlen($a) - strlen($b);'));
print_r($array_of_table_names);
Post Reply