Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 4th, 2006, 8:21 PM   #1
kworden
Newbie
 
Join Date: Dec 2006
Posts: 3
Rep Power: 0 kworden is on a distinguished road
Adding a # value to the end of another variable

What I'm trying to do is display a list of Values with A # value added to after the original value it represents...

See an example below:
[PHP]
$ga1 = 4;
$ga2 = 3;
$ga3 = 1;
$ga4 = 2;
$ga5 = 4;
$ga6 = 2;

$numberofgaquestions = 34;

for($i=1; $i<= $numberofgaquestions; $i++ ){
echo "The # $i: ". $ga$i ."<br>";
}
[/PHP]

The result I'm looking for is:
The # 1: 4
The # 2: 3
The # 3: 1
The # 4: 2
The # 5: 4
The # 6: 2

I get an error saying "Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' " So how can i add the # value to another variable?

Thanks for your help in advance....

K
kworden is offline   Reply With Quote
Old Dec 4th, 2006, 8:36 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
You're not concatenating the variables. Try this:
 $ga . $i
Although you could probably get by with leaving them inside the quotes anyways
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Dec 4th, 2006, 8:45 PM   #3
kworden
Newbie
 
Join Date: Dec 2006
Posts: 3
Rep Power: 0 kworden is on a distinguished road
If i do that i get an error saying $ga can't be found and then the #

[PHP]
for($i=1; $i<= $numberofgaquestions; $i++ ){
echo "Ga # $i:". $ga.$i ."<br>";
}
[/PHP]

Here is what i get:
Notice: Undefined variable: ga in /final_hr_attributes_7.php on line 329
Ga # 1:1
Notice: Undefined variable: ga in /final_hr_attributes_7.php on line 329
Ga # 2:2
Notice: Undefined variable: ga in /final_hr_attributes_7.php on line 329
Ga # 3:3

What i need is to form the variable $ga1 with this [php]echo "Ga # $i:". $ga.$i ."<br>";[/php]

But thanks for your help
kworden is offline   Reply With Quote
Old Dec 4th, 2006, 8:56 PM   #4
kworden
Newbie
 
Join Date: Dec 2006
Posts: 3
Rep Power: 0 kworden is on a distinguished road
I was able to solve the problem with:

[PHP]
for($i=1; $i<= $numberofgaquestions; $i++ ){
$varname = 'ga' . $i; //create name of variable as a string
echo "The # $i: ". $$varname ."<br>"; // notice $$
}
[/PHP]
kworden is offline   Reply With Quote
Old Dec 4th, 2006, 10:18 PM   #5
PhilBon
Hobbyist Programmer
 
PhilBon's Avatar
 
Join Date: Nov 2005
Posts: 171
Rep Power: 3 PhilBon is on a distinguished road
Send a message via AIM to PhilBon Send a message via MSN to PhilBon
I would suggest looking into Arrays, because that is excatly what $ga1, $ga2 look like. Instead they'd be $ga[1], ga[2].ga[3].ga[4], etc. One reason we have arrays is to use them in loops like this.
PhilBon is offline   Reply With Quote
Old Dec 13th, 2006, 2:05 AM   #6
headzoo
Newbie
 
Join Date: Oct 2006
Posts: 16
Rep Power: 0 headzoo is on a distinguished road
PhilBon is correct. Arrays are meant to deal with these kinds of situations. Your code should have looked more like this:

$ga = array(4, 3, 1, 2, 4, 2);
for ($i = 0; $i < count($ga); $i++) {
	echo "The # $i: " . $ga[$i] . "<br />";
}

- Sean
headzoo is offline   Reply With Quote
Old Dec 13th, 2006, 12:26 PM   #7
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
dynamic varible names are useful in many situations (like dynamicly generate html forms where you don't know how many fields there are and the field names come from a database or something like that), but most of the time, you can be making use of arrays, which require a bit less code. I have used dynamic varibles when working with excel sheets from php, makes life much easier (for sheet names etc, so you don't have to hard code them).

to use an array with an html form, simply name the field (lets say in this case a bunch of text fields) with a name like:

<input type=text name="text[]">
<input type=text name="text[]">
<input type=text name="text[]">


then, when your php script gets called, you can access the array like:


[PHP]<?PHP
for ($x=0; $x<3; $x++)
{
echo $_POST["text"][$x];
}
?>
[/PHP]

i know it's probably not what you are doing, but i thought you might like to know a little more information on how you can make use of arrays in php/html.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this declaring a variable? waveform C++ 21 Jun 23rd, 2006 3:48 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
Pointers in C (Part II) Stack Overflow C 2 Apr 29th, 2005 10:39 AM
Pointers in C (Part I) Stack Overflow C 4 Apr 28th, 2005 7:03 PM
variable problem robert_sun C 1 Apr 12th, 2005 2:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC