Greetings!

I'm trying to solve a little problem that occurs when I try to kinda make an associative array out of an associative array. I wish to associate 2 fields coming from mySQL so I can make a simple associated print for the records I want (and I do not wish to print all the records coming from the DB)

Here's my code:

$sql = "SELECT tipo_cont, conteudo_cont FROM CONTACTOS WHERE id_menu = '1'";
						
$query = mysql_query($sql);
	if(!$query)
	{
		echo "Erro ao executar a query".mysql_error();
	}		
						
						
							
	while($row = mysql_fetch_assoc($query))
	{	
							
		$cont = array($row['tipo_cont'] => $row['conteudo_cont']);
							
		print_r($cont);
	}
						
	echo $cont['Morada'];

So, what I want is field 'tipo_cont' to index the respective DB field in 'conteudo_cont'.

Would be a lot easier if I didn't wanted just some records to be shown.

Glad for all the possible help

Cheers

Member Avatar for diafol

OK, this loop seems to overwrite $cont every iteration. Try this:

while($row = mysql_fetch_assoc($query))
	{	
 
		$cont[$row['tipo_cont']] = $row['conteudo_cont'];
 
		print_r($cont);
	}
 
	echo $cont['Morada'];

It works!

However it keeps creating n arrays while the loop is not finished. Is there anyway to just create one array? Global array probably?

Not that really important though, as it works like a charm, but, I would be thankful for the knowledge ;)

Member Avatar for diafol

I don't understand the question, sorry.

I assume, something like:

id_menu | tipo_cont | conteudo_cont
  1        Morada     Comporta 7580-909 Alcácer do Sal
  2        Nome       ABÍLIO

But, I can't be sure. Can you just paste a few lines from your DB to give us an idea?

Can the number (e.g. 2) in the id_menu be exist more than once?

My DB is like

ID | tipo_cont | conteudo_cont | id_menu
 1 | something | belongs_to_something | 1
 2 | someone   | belongs_to_someone | 1
 3 | another   | belongs_to_another | 2
 4 | who       | belongs_to_who| 1

//And I want do not want to get all records that have 1 at id_menu. Just, for //example, I want to be able to echo content_cont belonging to something and who and //print it by doing: 

echo $row['who'];
echo $row['something'];

Hope it's understandable ;)

PS: How did you got that adress? :P Are you Tuga? :P

Member Avatar for diafol

PS: How did you got that adress? Are you Tuga?

NO, I guessed you were either Portuguese or Brazilian. :) Eu sou galês.

OK, I think I got you now. If you want $row[...] to work like $cont[...], no it won't. The reason for this is that $row is created again on every loop, so you only get a single record in $row at any given time.

I think my suggestion will be the best way, but anybody else out there with a better suggestion?

Try this one:

$cont = array();
while($row = mysql_fetch_assoc($query))
{					
    $cont[B][][/B] = array($row['tipo_cont'] => $row['conteudo_cont']);
}

var_dump($cont); // show result.
Member Avatar for diafol

Thanks ihateblue, I originally did:

while($row = mysql_fetch_assoc($query))
	{	
 
		$cont[$row['tipo_cont']] = $row['conteudo_cont'];
 
		print_r($cont);
	}
 
	echo $cont['Morada'];

what I should have done was:

while($row = mysql_fetch_assoc($query))
	{	
 
		$cont[$row['tipo_cont']] = $row['conteudo_cont'];
 
	}
 		print_r($cont);

	echo $cont['Morada'];

However, I doesn't change the content of $cont

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.