472,955 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,955 software developers and data experts.

How to allocate in dynamic array in PHP

Dear All,

I have a query regarding the array. The problem is that I want fetch some value from a table and store the value in an array and again want to run one more query on the basis of the earlier value which I have stored in the array.

For more details kindly find the below details.

table name: abc, it contain colum c1, c2, c3, c4,

now I want run a query to fetch distinct value of column c3 and now want to put all the distinct value in an array.

then, again want to run another query from the same table abc with condion of value allocate in the array.

Expand|Select|Wrap|Line Numbers
  1. $fetch = mysql_query("SELECT distinct(c3) FROM abc") or die(mysql_error());
  3. $count = mysql_num_rows($fetch) or die(mysql_error());
  4. echo $count;
  5. $i = 0;
  6. while($distinct_c3= mysql_fetch_array($fetch)){
  7.     for($i=0; $i<$count; $i++){
  8.         $value[$i] = $distinct_c3['c3'];
  10.     }
  11.     //echo $value[$i];
  12. $i++;
  13. }
then I want run another query separtly from the same table and get count of $value[$i]; Again, want to put count and $value[$i] into another array.

I am not able to do the same. Kindly help in executing the code.


New Delhi - India
Dec 12 '09 #1
3 4264
1,044 Expert 1GB
I'm not exactly sure what you are asking, but I'll take a stab at it.

The first thing you are saying sounds as if you want to find rows in your table, but only for distinct values of a certain column. Have you tried using GROUP BY in your SQL query?

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `table_name` GROUP BY `column_name`;

The second thing you are asking sounds as if you want to count how many rows of a table have a particular value for a row. GROUP BY can also be used to accomplish this.

Expand|Select|Wrap|Line Numbers
  1. SELECT `column_name`, COUNT(*) as `count` FROM `table_name` GROUP BY `column_name`;

Because these queries are similar, you could combine them to get the first row in your table for each distinct value of a column, and the total amount of columns that share the value.

Expand|Select|Wrap|Line Numbers
  1. SELECT *, COUNT(*) as `count` FROM `table_name` GROUP BY `column_name`;
Dec 12 '09 #2
thanks for the prompt reply. Now i am able to fetch the colum with total. Still my problem is not solve, as i want to put both the values like the culumn name count of value to an array.
Expand|Select|Wrap|Line Numbers
  1. $fetch = mysql_query("SELECT c, count(*) as n_c from abusedetails GROUP BY salesPerson") or die(mysql_error());
  2. if(isset($fetch)){
  3.     while($row = mysql_fetch_array($fetch) or die(mysql_error())){
  4.         echo $row['c']."=".$row['n_c']."<br>";
  5.     }
  6. }
I have an array with the following
Expand|Select|Wrap|Line Numbers
  1. $values = array(
  2.             "C1"=>n_c1,
  3.             "c2"=>n_c2,
  4.            "c3" =>n_c3
  6. );
I want to store the c value and count of c (n_c) in the array called $values. Kindly suggest me how to store this value to the array. Then I need to call both the key and value of the array $values.

With Regards,
Robindra Singha
New Delhi - India
Dec 14 '09 #3
1,044 Expert 1GB
Still not sure what your problem is.
If you need to store the data in an array, then do it. o.O

Expand|Select|Wrap|Line Numbers
  1. $totals = array();
  2. $result = mysql_query("select count(*) as `count` from `table` group by `column`");
  4. while ($data = mysql_fetch_assoc($result)) {
  5.     $totals[] = $data['count'];
  6. }
Dec 14 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
by: Simon | last post by:
Hello everybody, I've got a problem. I'm trying to allocate an array inside a function with Parameter int n: main(){ int i; .... i = strlen(ch); array(i); ....
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
by: diwakar09 | last post by:
in case of dynamic memory allocation with the use of new operator i have to allocate memory of integer array of 10 elements in constructor and similarly deallocate it in destructor. please help...
by: huohaodian | last post by:
Hi, I have a char * mFileList; how can i dynamically allocate it to save some strings? Thanks in advance
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
by: Samant.Trupti | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++) //print each...
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.