glennn.php Posted July 19, 2010 Share Posted July 19, 2010 ok, i have this small table with 'client_id' and 'p_net' as fields. the table will have several rows with the same client_id - i can get the array, "SELECT * from table WHERE client_id = '1'"; while ($row = mysql_fetch_array($query)) { echo "$row['p_net'].", "; } just fine, but i need to get EACH available p_net value into it's own variable so that i can use it elsewhere i've tried various foreach() methods inside the while() loop, but i'm not that good with it... help? thanks very much!! GN Quote Link to comment https://forums.phpfreaks.com/topic/208201-help-getting-field-values-with-a-loop/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2010 Share Posted July 19, 2010 You would use an array (each value is an element in the resulting array) - "SELECT * from table WHERE client_id = '1'"; $data = array(); // define as an empty array while ($row = mysql_fetch_array($query)) { $data[] = $row['p_net']; // add each value as an element to the array } Quote Link to comment https://forums.phpfreaks.com/topic/208201-help-getting-field-values-with-a-loop/#findComment-1088231 Share on other sites More sharing options...
glennn.php Posted July 19, 2010 Author Share Posted July 19, 2010 You would use an array (each value is an element in the resulting array) - "SELECT * from table WHERE client_id = '1'"; $data = array(); // define as an empty array while ($row = mysql_fetch_array($query)) { $data[] = $row['p_net']; // add each value as an element to the array } ok, so then i'd use them as data[0], data[1], etc... as long as there were values... ? thanks - i've GOT to learn arrays... Quote Link to comment https://forums.phpfreaks.com/topic/208201-help-getting-field-values-with-a-loop/#findComment-1088233 Share on other sites More sharing options...
PFMaBiSmAd Posted July 20, 2010 Share Posted July 20, 2010 Part of the point of using an array to hold a set of data that you will process is you can use array functions to loop over the data, such as foreach(), and if you happen to need to know how many pieces of date there are you could use count(). You would not necessarily write out all the individual lines of code that reference $data[0], $data[1], ... $data[x] unless you felt like you needed practice typing and you wanted to edit the code every time the amount of data changes. Quote Link to comment https://forums.phpfreaks.com/topic/208201-help-getting-field-values-with-a-loop/#findComment-1088689 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.