Jump to content

incrementing variables names indefinatley


paul2463

Recommended Posts

Hi Guys

probably 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 recieved

Paul
Link to comment
Share on other sites

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 = 1
while ($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]<?php
foreach ($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]

Regards
Huggie
Link to comment
Share on other sites

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 = 1
do
{
echo $$temp;
$i++;
} while (isset($$temp));
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.