468,278 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

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());
  2.  
  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'];
  9.  
  10.     }
  11.     //echo $value[$i];
  12. $i++;
  13. }
  14.  
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.

Regards,

Robindra
New Delhi - India
Dec 12 '09 #1
3 3979
kovik
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. }
  7.  
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
  5.  
  6. );
  7.  
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
kovik
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`");
  3.  
  4. while ($data = mysql_fetch_assoc($result)) {
  5.     $totals[] = $data['count'];
  6. }
Dec 14 '09 #4

Post your reply

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

Similar topics

5 posts views Thread by meyousikmann | last post: by
3 posts views Thread by Simon | last post: by
8 posts views Thread by M. Moennigmann | last post: by
2 posts views Thread by xhunga | last post: by
7 posts views Thread by huohaodian | last post: by
11 posts views Thread by Bryan Parkoff | last post: by
3 posts views Thread by Samant.Trupti | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.