Jump to content

db_opersations class => select method, need some advice


marcusfaye87

Recommended Posts

Ok so I've created a class that handles the database operations. but it gets tricky in the SELECT part of it.

I'm just wondering if you pro's could look through my method and give me some pointers on how to improve it, because it seems a little far fetched.

thanks in advance

 

EDIT:

thought I might add this to help you figure out what I'm trying to pull

 

$search contains nested arrays

[1] contains the field and value  e.g: $search[1]['username'] = "marcusfaye87";

[2] contains the AND or OR operator also in an array

 

<?php
/** SELECT database information */
public function select ($components, $table, $search, $order, $order_mode, $limit)
{
	// Check $table
	if (!$table)
	{
		// Return error
	}
	else
	{
		// Check $components
		if (!$components) { $components = '*'; }

		// Setup the query
		$query = "SELECT " . implode (', ', $components) . " FROM " . $table;

		// Check $search
		if (is_array($search) && is_array($search[1]) && is_array($search[2]))
		{
		$query = $query . " WHERE";

			if (count ($search[1]) == 1)
			{
				foreach ($search[1] as $key => $value)
				{
					$query = $query . " `" . $key . "` = '" . $value . "' ";
				}
			}
			elseif (count ($search[1]) == (count ($search[2]) - 1))
			{
				foreach ($search[1] as $key => $value)
				{
					$query = $query . " `" . $key . "` = '" . $value . "' ";
					foreach ($search[2] as $value2)
					{
						$query = $query . $value2;
					}
				}
			}
			else
			{
				// Return error
			}

		}

		// Check $order and $order_mode
		if (!$order) { $order = 'id'; }
		if (!$order_mode || $order_mode != 'ASC' || $order_mode !='DESC') { $order_mode = "DESC"; }

		$query = $query . " ORDER BY " . $order . " " . $order_mode;

		// Check $limit
		if ($limit && is_int ($limit))
		{
			$query = $query . " LIMIT" . $limit;
		}
	}
}
?>

Sorry guys, overlooked something, here is the altered code that actually does work.

 

<?php
/** SELECT database information */
public function select ($components, $table, $search, $order, $order_mode, $limit)
{
	// Check $table
	if (!$table)
	{
		// Return error
	}
	else
	{
		// Check $components
		if (!$components) { $components = '*'; }

		// Setup the query
		$query = "SELECT " . implode (', ', $components) . " FROM " . $table;

		// Check $search
		if (is_array($search) && is_array($search[1]) && is_array($search[2]))
		{
		$query = $query . " WHERE";

			if (count ($search[1]) == 1)
			{
				foreach ($search[1] as $key => $value)
				{
					$query = $query . " `" . $key . "` = '" . $value . "' ";
				}
			}
			elseif (count ($search[1]) == (count ($search[2]) - 1))
			{
				$loop = 0;
				$count = count ($search[2]) - 1;

				foreach ($search[1] as $key => $value)
				{
					$query = $query . " `" . $key . "` = '" . $value . "' ";
					$loop++;
					if ($loop <= $count)
					{
						$query = $query . $search2[$loop];
					}
				}
			}
			else
			{
				// Return error
			}

		}

		// Check $order and $order_mode
		if (!$order) { $order = 'id'; }
		if (!$order_mode || $order_mode != 'ASC' || $order_mode !='DESC') { $order_mode = "DESC"; }

		$query = $query . " ORDER BY " . $order . " " . $order_mode;

		// Check $limit
		if ($limit && is_int ($limit))
		{
			$query = $query . " LIMIT" . $limit;
		}
	}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.