loading a multi-dimensional array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

loading a multi-dimensional array?

Post by rathlon »

for($x = 1; $x <= $num_rows; $x++)
{
$sql = "select actions from pjstatus where pjid=".$x." order by date desc limit 1";
$sql_res = mysql_query($sql);
while($row_2 = mysql_fetch_row($sql_res))
{
$desc[] = $row_2;
}
}

this loads my array just fine. But what if I want to load the value of '$x' in the position for which it cooresponds to the action select from the query? How would I do that? Example:

right now the code does the following:

select actions where pjid='x'
desc[0][0] = action
desc[1][0] = action
desc[2][0] = action
etc...

what I want is:

select actions where pjid='x'
desc[0][0] = actions & desc[0][1] = x
desc[1][0] = actions & desc[1][1] = x
desc[2][0] = actions & desc[2][1] = x
etc...

How do I accomplish that? Is it possible?
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

Post by rathlon »

This is what I have so far. Of course, it doesn't work. Logically it looks good, but when I try to echo $desc[0][0] or [1][0] so on and so forth, it displays 'Array' insead of the contents. Why?

for($x = 1; $x <= $num_rows; $x++)
{
$sql = "select actions from pjstatus where pjid=".$x." order by date desc limit 1";
$sql_res = mysql_query($sql);
for($u = 0; $u < 1; $u++)
{
$row_2 = mysql_fetch_row($sql_res);
if($row_2)
{
$x = $x - 1;
$desc[$x][$u] = $row_2;
$x = $x + 1;
}
}
}

Why did I use the $x = $x - 1; ? Because in the database, the records start at number 1 and go up (auto-increment). Have to have some way of addressing that it's for the first record in whatever respective table.
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

the reason its printing out Array, is because the results from mysql_fetch_row is an array. if you wanted to get the data from the array, you'd use $row_2[0] to get at the first element in the array.

alternatively you could use mysql_fetch_assoc instead of mysql_fetch_row and use $row_2['action']
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

Post by rathlon »

now that makes perfect sense...where have u been all my life? ;) lol j/k thx for the hlp dude.
Post Reply