How to allocate in dynamic array in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin1983
    New Member
    • Oct 2007
    • 99

    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.

    Code:
    $fetch = mysql_query("SELECT distinct(c3) FROM abc") or die(mysql_error());
    
    $count = mysql_num_rows($fetch) or die(mysql_error());
    echo $count;
    $i = 0;
    while($distinct_c3= mysql_fetch_array($fetch)){
    	for($i=0; $i<$count; $i++){
    		$value[$i] = $distinct_c3['c3'];
    		
    	}
    	//echo $value[$i];
    $i++;
    }
    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
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    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?

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

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

    Code:
    SELECT *, COUNT(*) as `count` FROM `table_name` GROUP BY `column_name`;

    Comment

    • robin1983
      New Member
      • Oct 2007
      • 99

      #3
      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.
      Code:
      $fetch = mysql_query("SELECT c, count(*) as n_c from abusedetails GROUP BY salesPerson") or die(mysql_error());
      if(isset($fetch)){
          while($row = mysql_fetch_array($fetch) or die(mysql_error())){
              echo $row['c']."=".$row['n_c']."<br>";
          }
      }
      I have an array with the following
      Code:
      $values = array(
                  "C1"=>n_c1,
                  "c2"=>n_c2,
                 "c3" =>n_c3
                  
      );
      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

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Still not sure what your problem is.
        If you need to store the data in an array, then do it. o.O

        Code:
        $totals = array();
        $result = mysql_query("select count(*) as `count` from `table` group by `column`");
        
        while ($data = mysql_fetch_assoc($result)) {
            $totals[] = $data['count'];
        }

        Comment

        Working...