Okay, so here's my problem: I want to store the results of a MySQL query as a multidimensional array. This is for the purpose of eliminating an unneeded query.

Basically, I'm pulling info from my database. At a later point I need to retrieve the name of a tab based on its id in the database. I'm thinking if I had the variable as something like this:

$row['2']['name'];

It would then show as the name of the tab with an id of 2. Anyone know how this can be done? Got any other ideas? It just seems I have an extra query I can get rid of.

Here's where my code stands now, if it matters:

$sql = "SELECT id, name, position, compulsive FROM phpbb_nulavatar_layers ORDER BY position";
$result = mysql_query($sql);
	if ( $row_count = mysql_num_rows( $result )) {
		$row = mysql_fetch_assoc($result);
		if ( !row ) {
			echo "There was an error.";
		}
		else {
			extract( $row );
		}
	}
	else {
		echo "No layers detected.";
	}

echo "<ul>";
	for ( $i = 1; $i <= $row_count; $i++ ) {
		$sql2 = "SELECT name FROM phpbb_nulavatar_layers WHERE id = " . $i . "";
		$result2 = mysql_query($sql2);
		$row2 = mysql_fetch_assoc($result2);
		extract( $row2 );
		echo "<li><a href=\"tabs_select.php?tab=" . $i . "\"><span>" . $name . "</span></a></li>
	";
}
echo "</ul>

    Basically:

    $data = array();
    while($row = mysql_fetch_assoc($result))
    {
       $data[$row['id']]['name'] = $row['name'];
       // whatever else you want to do at this time....
    }
    

      I can't quite figure out how to incorporate that with my current code. ):

        give this a shot.

        $data = array();
        $sql = "SELECT id, name, position, compulsive FROM phpbb_nulavatar_layers ORDER BY position";
        if($result = mysql_query($sql)){
        	if(mysql_num_rows($result) > 0){
        		echo "<ul>";
        		while($rows = mysql_fetch_assoc($result)){
        			echo "<li><a href=\"tabs_select.php?tab=" . $rows['id'] . "\"><span>" . $rows['name'] . "</span></a></li>";
        			$data[$rows['id']] = $rows['name'];
        		}
        		echo "</ul>";
        	}
        	else{
        		echo "No layers detected.";
        	}
        }
        else{
        	echo "There was an error";
        }
        

        sorry forgot to bump it into the new array. fixed

          That works perfectly! Thank you soooo much, zypher! You're the best. <3

            Write a Reply...