I have 3 functions in this file and they have repetitive lines of code and I'm not sure how to go about slimming things down. The file is basically 3x as large as it needs to be in my opinion. Can I create a class with all 3 menu creating functions, while still including the database connecting parts?
Or should I just trim these areas out?
//connect to database -> in all three functions
//select database -> in all three functions
//close connection -> in all three functions
Here is one of the functions from the functions.inc file.
PHP Code:
function category_menu()
{
//connect to database
$db_connect = mysql_connect("localhost", "root", "") or die ("Could not connect");
//select database
$db = mysql_select_db('em', $db_connect);
if (!$db) {
die('Could not select em database: ' . mysql_error());
} else { echo 'Database Selected.<br />';
}
// execute the query
$rResult = mysql_query("SELECT `category_id`, `category_field` FROM `em_category`");
if (!is_resource($rResult))
{
die('Could not query the em_category table: ' . mysql_error());
} else {echo 'Table query sent.<br />';
}
// echo tags to create menu
echo "<select name = 'category_field' size='5'>";
while ($row = mysql_fetch_array($rResult))
{
echo "<option name = '" . $row['category_field'] . "'>". $row['category_field'] . "</option>";
}
echo "</select>";
//close connection. required in all functions?
mysql_close($db_connect);
}
Bookmarks