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

}

Link to comment
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

}

 

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

Link to comment
Share on other sites

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.

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.