SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comparing arrays - can do better?

    I have to arrays that comes from a db. I want to compare the results from them, and then echo something. This is my code. How can I make it better?

    PHP Code:
    foreach ($order_array as $row//make the main array usable for displaying its data
      
    {
        
    $class = ( $class == "alternate2" ) ? "alternate3" "alternate2";
          echo 
    '<tr class="'.$class.'"><td class="border">';
        foreach (
    $ny_array as $ny//compare second array with the result from the first array
        
    {
            if (
    $row['orderid'] == $ny['orderid'])  //If a match then echo this
            
    {
                echo 
    '<font color="green">Ny order &rarr;</font>';
            }
        }
        echo 
    '</td><td class="border">';
    //Here comes the output from the main (first) array
        
    echo $row['orderid'] + 3200;
        echo 
    '</td><td class="border">'.$row['lev_namn'].'</td><td class="border">'.strtok($row['datum'],' ').'</td>';
          if (
    $row['makulera']==1)
          {
              echo 
    '<td class="border" style="background:thistle;color:red">Makulerad</td>';
          }
          else
          {
              echo 
    '<td class="border"><input type="checkbox" name="makulera[]" value="'.$row['orderid'].'"> Makulera</td>';
          }
          echo 
    '<td class="border"><a href="show_order.php?datum='.$row['datum'].'&orderid='.$row['orderid'].'">Visa order</a></td></tr>';
        
      } 
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, PHP seems to have a similar function to something I use in S which is extract. That may do it.

    Alternatively, you could use in_array() to cut it down. Or this looks pretty good too.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried with a variation of different array-functions, to see if I could understand how to get it to work, but I just didn't know how to put it together.

    What I wanted to do was this...
    Based on two criterias I wanted to find out what orders were new. First I needed a list of all orders to display, and then I did a cross referense to another table (with invoices) to get those that wasn't there. So... IF there is NO INVOICE, and the orders IS NOT void (makulera), the order is to be marked as "New order".

    So I took my table with orders, and added a new column to it where I wanted the words to appear, and of course put it next to the proper orderID.

    So... on each row, check to see if the order is new or not, and then echo the apropriate tablecell content.

    Maybe I should do this differently all together? I use two different MySQL queries to fetch the information.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  4. #4
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone have any ideas?
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am assuming your $ny array looks like [0] => array ('field1'=>val2,'field2'=>val2...)...
    You might try restructuring $na to be an assosiative array of id points to row values: i.e.
    PHP Code:
    $nya = array();
    foreach(
    $ny as $row) {
      
    $nya[$row['orderid']] = $row;

    Then, instead of looping over the whole $ny array, you can use:
    PHP Code:
    if (array_key_exists($row['orderid'], $nya)) {
      ...
      echo 
    $nya[$row['orderid']]['otherfield'];
      ...
    } else {
      
    //stuff for orderid not present in $ny
      
    ...

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That sounds interesting! I'll try that
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...


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
  •