Jump to content

help getting field values with a loop...?


glennn.php

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/208201-help-getting-field-values-with-a-loop/
Share on other sites

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

}

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...

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.