Sabmin Posted November 27, 2010 Share Posted November 27, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/ Share on other sites More sharing options...
n1concepts Posted November 27, 2010 Share Posted November 27, 2010 Maybe I'm over looking it but I don't see where you defined $res_1_date. Please advise to help us understand the code. Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140346 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2010 Share Posted November 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140348 Share on other sites More sharing options...
Sabmin Posted November 27, 2010 Author Share Posted November 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140349 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2010 Share Posted November 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140353 Share on other sites More sharing options...
Sabmin Posted November 27, 2010 Author Share Posted November 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140383 Share on other sites More sharing options...
jcbones Posted November 27, 2010 Share Posted November 27, 2010 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... That is called Pagination. Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140453 Share on other sites More sharing options...
Sabmin Posted November 28, 2010 Author Share Posted November 28, 2010 Pagination thats the word I needed! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/220007-variable-variable-question/#findComment-1140492 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.