denoteone Posted January 8, 2010 Share Posted January 8, 2010 I know if actionscript you can loop through and assign values to variables and just increment a number in the variable name like so..... Is this possible in php $result = clientTestimonial(); $i = 0; while($row = mysql_fetch_assoc($result)){ $company_$i = $row['company'] ; $name_$i = $row['name'] ; $teaser_$i = $row['teaser'] ; $i++ } Quote Link to comment https://forums.phpfreaks.com/topic/187661-loop-and-assign-value-to-variable/ Share on other sites More sharing options...
denoteone Posted January 8, 2010 Author Share Posted January 8, 2010 wow i have been working to long today. I conveniently forgot about arrays... Now just to get the syntax correct Quote Link to comment https://forums.phpfreaks.com/topic/187661-loop-and-assign-value-to-variable/#findComment-990733 Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 $rows = array(); while($rows = mysql_fetch_assoc($result)) { $rows[] = $row; } You can then access any of the company info using $rows[0]['company'] for example (changing the 0 depending on the offset) Quote Link to comment https://forums.phpfreaks.com/topic/187661-loop-and-assign-value-to-variable/#findComment-990737 Share on other sites More sharing options...
denoteone Posted January 8, 2010 Author Share Posted January 8, 2010 Thanks! @JAY6390 Quote Link to comment https://forums.phpfreaks.com/topic/187661-loop-and-assign-value-to-variable/#findComment-990739 Share on other sites More sharing options...
JAY6390 Posted January 8, 2010 Share Posted January 8, 2010 No problem Quote Link to comment https://forums.phpfreaks.com/topic/187661-loop-and-assign-value-to-variable/#findComment-990741 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2010 Share Posted January 8, 2010 Generating a list of sequentially numbered variables to hold a set of same type data is never a good idea. Once you have created them, you need extra logic to keep track of them and iterate over the correct number of them. Arrays should be used when you have a set of same type data, that's what array data structures are for. Quote Link to comment https://forums.phpfreaks.com/topic/187661-loop-and-assign-value-to-variable/#findComment-990743 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.