0

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

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by diafol
0

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'];
0

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 ;)

0

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?

0

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

0

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?

Edited by diafol: n/a

0

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.
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.