Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Insanity

Status
Not open for further replies.

GiffordS

Programmer
May 31, 2001
194
0
0
CA
Ok, once again I've managed to over-think a piece of code. I've gotten this to the point where nothing I try completely works. The problem is a simple one. I need to generate variables from a MySql query result. No biggie. However, I want to iterate the variable names. For example, let's say I have a database of cars. I then have a field in the db called "Make". Ok, now, when I run a query I want to pull down the Makes of cars, and use them as $car1, $car2, $car3 etc. The while loop is no problem, and I figure that I can increment the number by one each time by setting a variable, ($x) to one before the while loop, and at the end of each loop increase $x by one. No problem. The issue is in the syntax of naming the variable. I can't, for example, use

$car$x = $row["Make"]

I've tried

$$car$x = $row["Make"]

but that's not right either. I want to be able to access this variable later by calling the value of $car1, or $car6, etc. Does anyone know the correct syntax for instantiating that variable?
 
Geeze, no.

I'm going to make a strong suggestion that you use arrays instead though... much simpler, and PHP has some support which makes them even easier to use than most other languages.

so have
$cars[] = $row["Make"], then later you can do

foreach ($cars as $car) {
echo $car;
}

or

echo $cars[2];

or if you think it'll be useful to know the index of each, use a counter and explicitly state

$cars[$x] = $row["Make"];

where $x is whatever changing value you like, integers, words, phrases, whatever floats your boat.


-Rob
 
I recommend that you put the values in an array:

$car = array();
$car[] = $row["Make"];

It makes it a lot easier to reference the values. Your way will require a lot of jumping through hoops to loop through $car1, $car2, etc. With an array, it's just $car[1].
Want the best answers? Ask the best questions: TANSTAAFL!
 
I considered this, but I guess I didn't give enough information. First of all, "Make" is just one of the variables. But it would be the primary one, so that I may also have Year, Color, etc. Ideally, I would have $car1, $car1yr, $car1cl, etc. The other issue with the array, where I thought it would be easier to define all of the variables up front, is that the values will be echoed at different times in the display. I may want to display $car3, $car3yr, $car3cl, etc., and then further on down call the next, or previous, car. I'm sure that you guys are probably still right and for whatever reason I just can't see it. It's one of those things where I just need to stop looking at it for an hour and it will all make sense and then I'll feel like a total yabohead.
 
Sounds to me like you should use a multi-dimensional array.

Let's say that your database query returns columns named "Make", "Model", "Year", and "Color" in each loop through the retrieval.

Then:

$cars = array();
while ($row = mysql_fetch_assoc ($resource))
{
$cars[] = $row;
}

will put all the values in the array.

Then you can retrieve them as:

$cars[1]["Make"] will get the make of car 1.
$cars[2]["Model"] will get the model of car 2.
etc. Want the best answers? Ask the best questions: TANSTAAFL!
 
I'm not saying you're wrong. In fact, I will say that as I just read through my original question, if I were reading that post I would suggest a multi-dimensional array as well. What will eventually happen here is that I will either use the array or change the way I deal with the query in the first place. But, just so I can stop my brain from churning on this, what would be the proper syntax for increment variable names as I mentioned? It drives me nuts to have something like that and not know the answer.
 
I'm not sure if this is what you're looking for, and I'm no expert, so this might not be the perfect solution. But I would use this: ${"car$x"} to refer to $car1, $car2, $car3, etc. in a loop where you increment by $x.

Hope that helps!
Susan :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top