SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrays from MYSQL alternating results

    Hello,

    I need to build an array from alternating results of a query. I posted this as a reply to a previous thread but I am attempting to repost as it is actaully a different question.

    If I had a query:

    PHP Code:
    $sql "select MIN(score), MAX(score) from table where id ='$id'"
    $results = ($sql); 
    is it possible to create an array like:

    PHP Code:
    $arr= array(MIN(score), MAX(score), MIN(score), MAX(score), etc..); 
    I have mysql queries creating arrays already but not alternating such as this.

    Could I use something like this:

    PHP Code:
    function highlow($query) {    
    $highlows = array();    
    $i 0;    
    $lw "select MIN(score) from table where company='my company' group by skill order by AVG(score) DESC";
    $hi "select MAX(score) from table where company='my company' group by skill order by AVG(score) DESC";
    $result mysql_query($lw);    
    $result2 mysql_query($hi);    
    while (
    $row mysql_fetch_assoc($result$result2)) {        
    foreach(
    $row as $key){ 
    $value $key;          
     
    $highlows[$i] = number_format($value2);  
     
       }        
      
    $i++;    
      }   
       return 
    $highlows;
          }  
      
    print_r(highlow("")); 

  2. #2
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://us3.php.net/manual/en/functio...etch-assoc.php

    Quote Originally Posted by php's doc site
    mysql_fetch_assoc

    (PHP 4 >= 4.0.3, PHP 5, PECL mysql:1.0)

    mysql_fetch_assoc — Fetch a result row as an associative array
    Description
    array mysql_fetch_assoc ( resource $result )

    Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
    Parameters
    as you can see, you CANNOT pass 2 or more parameters to the function.

    best bet?

    load up 2 arrays. then compare/process them afterwards...
    leo d.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ldivinag View Post
    http://us3.php.net/manual/en/functio...etch-assoc.php



    as you can see, you CANNOT pass 2 or more parameters to the function.

    best bet?

    load up 2 arrays. then compare/process them afterwards...
    Hmm, its for a jpgraph created graph. I need to show high/low ranges on the graph for each axis point and it seems the jpgraph rules are pretty strict on what extra coding i can add.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function highlow () {
        
        
    $return = array();
        
        
    $sql 'SELECT %s FROM %s WHERE company = "%s" GROUP BY skill ORDER BY AVG(score) DESC';
        
        
    $high mysql_query(sprintf($sql'MAX(score) AS maxScore''table''my company'));
        
    $low  mysql_query(sprintf($sql'MIN(score) AS minScore''table''my company'));

        if (
    mysql_result_num($high) != mysql_result_num($low)) {
            
    // Error Uneven results;
            
    return -1;
        }

        while (
    $rhigh mysql_fetch_assoc($high)) {
            while (
    $rlow mysql_fetch_assoc($low)) {
                
    array_push($return$rlow['minScore'], $rhigh['maxScore'])
            }
        }
        
        return 
    $return;

    }

      

    print_r(highlow()); 
    Try that.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    function highlow () {
        
        
    $return = array();
        
        
    $sql 'SELECT %s FROM %s WHERE company = "%s" GROUP BY skill ORDER BY AVG(score) DESC';
        
        
    $high mysql_query(sprintf($sql'MAX(score) AS maxScore''table''my company'));
        
    $low  mysql_query(sprintf($sql'MIN(score) AS minScore''table''my company'));

        if (
    mysql_result_num($high) != mysql_result_num($low)) {
            
    // Error Uneven results;
            
    return -1;
        }

        while (
    $rhigh mysql_fetch_assoc($high)) {
            while (
    $rlow mysql_fetch_assoc($low)) {
                
    array_push($return$rlow['minScore'], $rhigh['maxScore'])
            }
        }
        
        return 
    $return;

    }

      

    print_r(highlow()); 
    Try that.
    syntax error, unexpected '}' on line 97

    that would be where
    PHP Code:
         }
        }
        
        return 
    $return
    but it looks like all the brackets match up.

    -----------------------------------------

    EDIT: Figured it out, the array_push row needed a ; at the end.

    Thanks so much, this works great.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand why the complexity behind the posted code is necessary. You could always just do:

    PHP Code:
    $sql "select MIN(score), MAX(score) from table where id ='$id'";
    $result mysql_query($sql);
    $values = array();
    while (
    $row mysql_fetch_row($result)) {
        
    array_push($values$row[0], $row[1]);

    The overhead with the string formatting, multiple queries, and nested loops seems unnecessary, unless I interpreted the question incorrectly.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xmitchx View Post
    I don't understand why the complexity behind the posted code is necessary. You could always just do:
    ...snip...
    The overhead with the string formatting, multiple queries, and nested loops seems unnecessary, unless I interpreted the question incorrectly.
    Well if I had his data that in the DB I could test and design a more effient function.

    But I didn't so just worked with what i got.

    I Just hate repeating myself so thats why i used the string formatting.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Well if I had his data that in the DB I could test and design a more effient function.

    But I didn't so just worked with what i got.

    I Just hate repeating myself so thats why i used the string formatting.
    Right, well from what I got, the code I wrote should work for his needs with a lot less overhead. I guess I'm just a freak for code efficiency Oh well np.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  9. #9
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xmitchx View Post
    I don't understand why the complexity behind the posted code is necessary. You could always just do:

    PHP Code:
    $sql "select MIN(score), MAX(score) from table where id ='$id'";
    $result mysql_query($sql);
    $values = array();
    while (
    $row mysql_fetch_row($result)) {
        
    array_push($values$row[0], $row[1]);

    The overhead with the string formatting, multiple queries, and nested loops seems unnecessary, unless I interpreted the question incorrectly.
    Touche. This does work as well. Thanks for the help from everyone. Once again, sitepoint is the place to be.


Bookmarks

Posting Permissions

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