paul2463 Posted December 19, 2006 Share Posted December 19, 2006 Hi Guysprobably a really silly stupid question but here goes anyway, I have a tabel of group names for multy mail groups sending type stuff, the table only holds the id and then the name of the group(ie. id1 = groupd aministrators id2 = carpet sweepers etc etc)now I will not know how many groups will be set up in the future on the system and I want to pull all the names from the database but assign them an incremented variable here is what I am doing at the moment[code]<?php$query1 = "SELECT groupName from messageGroups";$result = mysql_query($query)or die ('Error in query: $query. ' . mysql_error());while ($row = mysql_fetch_assoc($result)){ //in here I want to asign $name1 to the first row and $name2 to the second and so on untill the end}?>[/code]for some reason I am not figuring it out in my head to make it work, when I try and append a value to end of the variable name it becomes its value not its name. any help gratefully recievedPaul Quote Link to comment https://forums.phpfreaks.com/topic/31220-incrementing-variables-names-indefinatley/ Share on other sites More sharing options...
HuggieBear Posted December 19, 2006 Share Posted December 19, 2006 Why not put them into an array then? The array is keyed on number by default.[code]<?php// Prepare SQL$query = "SELECT groupName from messageGroups";// Execute query$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());// Put each row into an array, keyed on number (starting from 1, not 0)$key = 1while ($row = mysql_fetch_assoc($result)){ $groups[$key] = $row['groupName']; $key++;}?>[/code]This also gives you the added advantage of being able to find out how many groups there are by using the count() function[code]<?php$number_of_groups = count($groups);?>[/code]Also, doing something to each of the groups in turn is as simple as a foreach loop[code]<?phpforeach ($groups as $key => $name){ // Change to first letter of the group to upper case and the rest to lower $groups[$key] = ucfirst(strtolower($name))}?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31220-incrementing-variables-names-indefinatley/#findComment-144376 Share on other sites More sharing options...
ToonMariner Posted December 19, 2006 Share Posted December 19, 2006 Assigning your data to an array is more useful but what you asked for in teh first post is the old variable variable routine..[code]<?php$query1 = "SELECT groupName from messageGroups";$result = mysql_query($query)or die ('Error in query: $query. ' . mysql_error());$temp = "name$i";$i = 1;while ($row = mysql_fetch_assoc($result)){ $$temp = $row; $i++;}?>[/code]then you can traverse these using[code]<?php$i = 1do{ echo $$temp; $i++;} while (isset($$temp));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31220-incrementing-variables-names-indefinatley/#findComment-144391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.