Re: Assign 2 values from Mysql to an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Anton Heuschen schreef:
> This might sound trivial, but for the live of me cant seem to get it to
> work, and I am not familiar with such a thing.

seems like you've done your best to make it sound as confusing as possible,
this happens sometimes when you no longer see the wood for the trees :-)

> 
> What I have is a query lets say :
> 
> select  country,name,population from USERS where id= 'some_id' ";
> 

'USERS' seems like an odd namew for the table in question.

> 
> Now I want to assign the result to one set (The above example might have 3+
> entries per telephone, thus what would be nice is something like an array of

per telephone? where do these telephones come from? is that a freudian slip?
did the phone ring whilst you we're writing that sentence?

keep reading ... it gets more php related as we go one ...

> :
> 
> 
> [id][country][name]  = population .......... or to be exact if I echo the
> value for each id and country and name to get the population value
> 
> 
> Like :
> 
> Array {
> 
>   [id] array {
>          [country] array {
>                     [0] = USA
>           }
>   [name] array {
>          [0] = test
> 
>   }
> 
> }
> 

I'm not grokking that array structure much.

> 
> 
> 
> I dont know something like that, maybe Im over comlicating the question now
> even, the main thing is wondered was in the first place was with a standard
> query and variable assign, from the query like:
> 
> select  country,name,population from USERS where id= 'some_id' ";
> 
> normally you would just assign each field to one variable. like
> 
> $country = result["country"]
> $name = result["name"]

sidenote: I never see the point of assign the vars like that anyhow, why not just use
$result['name'] and do away with the exytra $name var?

> 
> 
> But now I want all 3 fields as one variable (which would be an array) ..but
> how ?

why? I'd guess it's because you want to do a 'look-up' of the data, that
would suggest you don't want to be extracting the data on a row by row basis,
but rather grab all the data and parse it into a format you can use for
multiple look ups, here's a little class idea,maybe it offers some
inspiration/insight/relief (it untested because I can't be bothered to setup a DB):

abstract class Populations
{
	static function getAll($type = null)
	{
		self::init();

		if ($type) {
			return isset(self::$data[$type]) ? self::$data[$type] : null;
		}	
		
		return self::$data;
	}

	static function byId($id)
	{
		return self::get('names', intval($id));
	}

	static function byPlaceName($name)
	{
		return self::get('names', strtolower(trim($name)));
	}

	static function byCountryName($name)
	{
		return self::get('countries', strtolower(trim($name)));
	}

	static private function get($k, $v)
	{
		self::init();

		if (isset(self::$data[$k][$v]))
			return self::$data[$k][$v];

		return null;		
	}

	private static function init()
	{
		if (!isset(self::$data))
			self::loadData();
	}

	private static function loadData()
	{
		self::$data = array(
			'ids'   	=> array(),
			'names' 	=> array(),
			'countries' 	=> array(),
		);

		// let's assume that the following query will only return
		// a reasonable number of rows (i.e. not a very large number)

		// let's also assume that a db connection has been setup

		$res = mysql_query('SELECT id, country, name, population FROM users');

		if ($res && mysql_num_rows($res)) {
			while ($row = mysql_fetch_assoc($res)) {
				$i = $row['id'];
				$n = strtolower(trim($row['name']));
				$c = strtolower(trim($row['country']));
			
				self::$data['ids'][$i]   =
				self::$data['names'][$n] = $row['population'];

				if (!isset(self::$data['countries'][$c]))
					self::$data['countries'][$c] = 0;

				self::$data['countries'][$c] += $row['population'];
			}
		}			
	}
}

you would use this class something like:

echo Population::byId(1), "\n";
echo Population::byPlaceName("Texas"), "\n";
echo Population::byCountryName("USA"), "\n";

var_dump(Population::getAll("names"));
var_dump(Population::getAll());


> 
> I hope I have not totally confused the question now.

if you didn't then I probably have. enjoy.


> 
> Thanks in advance
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux