Got a question about technology? Ask it here. Discussion of hardware, software, TiVos, multi-region DVDs, Windows, Macs, LINUX, hand-helds, iPods, anything tech related. Better than any helpdesk!
Anyone know php associative arrays? I've got a small two column mySQL table that i want to stuff into an associative array. The fields in the table are program_id and program_name. I want the array to have it's keys equal to the program_id and the values equal to the program_name.
Here's my code. The query in the first three lines is definitely working correctly, but I include it for completeness. I think the problem is in how I am assigning elements to the array, but I don't know the correct syntax. Help?
$query = "SELECT * FROM tbl_programs";
$query .= " ORDER BY program_name";
$progquery=mysql_query($query);
While ($progArr = mysql_fetch_assoc($progquery)) {
$progTable[] = $progArr['program_id']=>$progArr['program_name'];
}
What do you want the resulting array to look like? I'd probably have the assignment in the loop look like
$progTable[$progArr['program_id']]=$progTable[$progArr['program_name']];
In terms of learning what I've just done to arrays, I do a lot of print_r($array)
so that I can see what got put where, and how I can get it out again. It's very powerful, but damn it's easy to slip something in and not know how to get it back out.
$progTable['program_id']=$progTable['program_name'];
Ahhh, yeah, I think that should work! Thanks. I knew I just needed another pair of eyes. Now if my server would only stop acting up, I could test this...
No! That's not going to work! I'm such a ditz. Let me fix it...gimme a couple seconds, though, because work calls.
Here you go: I neglected to actually put anything from the query result in there, in my rushedness:
$progTable[$progArr['program_id']]=$progTable[$progArr['program_name']];
Wouldn't it be?
$progTable[$progArr['program_id']]=$progArr['program_name'];
My PHP is rusty though, I don't use it very much.
I'm also a ditz for not immediately noticing the error! I think Gud's got it.
Yep. It's working how I want it. Thanks guys!
It's a wonder this board works at all.
I need to keep my fixings asymmetrical.
Or stop trying to work.
It's a wonder this board works at all.
You do an awesome job ita!
I still feel guilty about dropping out of the development effort, I just got overloaded.