Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts

    using field counts

    hi ,

    i have this code


    PHP Code:
    while ($row mysql_fetch_assoc($result))

                                      {
                                                                        
    $newArray[0][] = $row['orderNumber'];

                                                               
    $newArray[1][] = $row['name'];

                                                               
    $newArray[2][] = $row['productName'];
                                                         
    $newArray[3][] = $row['subproduct'];
                                                         
    $newArray[4][] = $row['count'];
                                                         
    $newArray[5][] = $row['run'];
                                     }


                        return 
    $newArray;
        } 


    I want the way where i don't have to mention everytime the number of fields and their names. Something that count the fields and their values automatically and put in array. something like below



    PHP Code:
    $field_count mysql_num_fields($result);
            
    $i 0;
            
            while (
    $i $field_count)
            {
                
    $field mysql_fetch_field($result);
                
    $Arr[$i] = $field[name_of_field];
                
    $arr[$i][$i] = $fields[$i]);
                
    $i++;
            }

    return 
    $arr 

    so that $arr[0] contain the name of field like 'orderNumber'
    $arr[0][0] contain the order '2333333'
    Please de-reputate me
    © 0o0o0o0

    Its better to rule in Hell then to serve in Heaven

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
      $fields = array(=> 'orderNumber',
                            
    => 'name',
                            
    => 'productName',
                            
    => 'subproduct',
                            
    => 'count'
                            
    => 'run']);

      while (
    $row mysql_fetch_assoc($result)){
         foreach(
    $fields as $i => $name){
            
    $newArray[$i] = $row[$fields[$i]];
         }
      }
      return 
    $newArray;

    regards

  • #3
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts
    $fields = array(0 => 'orderNumber',
    1 => 'name',
    2 => 'productName',
    3 => 'subproduct',
    4 => 'count'
    5 => 'run']);
    I don't want these to define as well . the system should get these from the mysql resultset columns-name. like mysql_field_name($count,$i);
    Last edited by o0O0o.o0O0o; 08-07-2008 at 04:58 AM.
    Please de-reputate me
    © 0o0o0o0

    Its better to rule in Hell then to serve in Heaven

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by o0O0o.o0O0o View Post
    I don't want these to define as well . the system should get these from the mysql resultset columns-name.
    PHP Code:
    $query "desc yourtablenamehere";
    $res mysql_query($query);
    if(
    $res){
       
    $fields = array();
       while(
    $fld mysql_fetch_assoc($res)){
           
    $fields[] = $fld['Field'];
       }
       if(!empty(
    $fields)){
          
    // rest of the posted code here
       
    }else{
          
    // errors
       
    }
    }else{
      
    // errors

    Edit: wait! do you want to get the fields name from your query?

    regards
    Last edited by oesxyl; 08-07-2008 at 05:05 AM.

  • #5
    Senior Coder o0O0o.o0O0o's Avatar
    Join Date
    Jan 2008
    Location
    C:\Windows\System32
    Posts
    1,018
    Thanks
    19
    Thanked 9 Times in 9 Posts
    I want to display the report so the table column caption should be the name of the fields in mysql table.

    I just want to pass the query to the function like

    Code:
    getReport("Select * from tblusers");
    then system should get do evrything by self. I had done this before but not able to do it now
    Please de-reputate me
    © 0o0o0o0

    Its better to rule in Hell then to serve in Heaven

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by o0O0o.o0O0o View Post
    I want to display the report so the table column caption should be the name of the fields in mysql table.

    I just want to pass the query to the function like

    Code:
    getReport("Select * from tblusers");
    then system should get do evrything by self. I had done this before but not able to do it now
    PHP Code:
    function getReport($query){
       
    $res mysql_query($query);
       if(
    $res){
          
    $report = array();
          
    $reporthead = array();
          while(
    $row mysql_fetch_assoc($res)){
              if(empty(
    $reporthead)){
                
    $reporthead array_keys($row);
              }
              foreach(
    $reporthead as $fieldname){
                  
    $report[$fieldname][] = $row[$fieldname];
              }
          }
          return 
    $report
       }
       return 
    false;

    regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •