Jump to content

Variable variable question.


Sabmin

Recommended Posts

I'm trying to loop through a database and assign the results to a variable that is created with the loop with the variables name based on which instance is currently running in the loop.  I'll go ahead and include the code I thought might work, but I obviously have no idea what I'm doing in this case

$i = 0;
while ($i < $num_results) {
	$ip = ($i++);
	$result = ("$res_" . "$ip");
	$loop_date = ("$res_" . "$ip" . "_date");
	$get_result = "SELECT * FROM table WHERE id = '$result'";
	$que_result = mysql_query($get_display);
	while ($sub_res = mysql_fetch_array($que_result, MYSQL_BOTH)) {
		$loop_date = $sub_res['date'];
	}
	$i++;
}
echo $res_1_date;

Can anyone please clue me in on how I can get this to work please.  It works with $result but it isn't working with the last $res_1_date... As I'm writing this I see where the problem is but just not quite sure how to make it work...

Link to comment
Share on other sites

There is almost never a good reason to execute a query inside of a loop, where all you are doing is getting different values from each query that is executed.

 

In general, you should execute 1 (one) query that gets all the data you want, in the order that you want it, and then simply iterate over that data in your presentation logic.

 

What exactly are you trying to accomplish? What is your data and what is the expected output?

Link to comment
Share on other sites

Wow thanks for fast responses!

 

I'm trying to define $res_1_date through $loop_date... Or in the beginnings of the loop I'm defining $loop_date with another variable, and what I need to do is grab information from the DB and assign it to the variable inside $loop_date, rather than just assigning it to $loop_date as its currently doing.

 

As far as what I'm trying to accomplish I guess the best way to explain it is similar to a forum where your generating x number of topics per page based on the page number selected...

 

Does that make more sense?

Link to comment
Share on other sites

Is this some kind of employment test you are doing or a classroom assignment?

 

Your's is about the 6th thread recently where someone is attempting to use or think they should be using variable variables.

 

If you are somehow required to use variable variables, be advised you are not going to like the answers you get on a programming help forum from experienced programmers. You should not be using variable variables for what you are doing. If you need to buffer/store the results from your query (instead of using the results directly inside of the loop, which is what is normally done as it consumes less memory and takes less processor time) you should be using an array.

Link to comment
Share on other sites

Neither actually, just a personal project as I'm trying to learn all I can of the language.... It never occured to me to use a multi-dimensional array here maybe because I had so much trouble with them when I learned how to use them. Sub-concious block? :)

 

But you're saying I shouldn't even do that, instead use the loop to display the content as its pulled rather than store it?

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.