MySQL Join question

    Date: 04/03/07 (PHP Community)    Keywords: php, mysql, sql

    I have two tables "Clients" and "ShippingAddresses" that I am trying to join to output two separate form dropdown boxes. Unfortunately I am only now beginning to use the Join command and so I'm running into issues, and could really use some assistance. Below the cut is my old code:

    $grabClients = mysql_query("SELECT Client_ID, Client_Company FROM Clients ORDER BY Client_Company ASC") or die(mysql_error());
    if (mysql_num_rows($grabClients) == 0) {
      $clientList = "";
      $shippingList = "";
    } else {
    
      $clientList = "";
      $shippingList = "";
    
    
      while ($loopClients = mysql_fetch_array($grabClients)) {
    
        $grabShipping = mysql_query("SELECT Address_ID, Address_Company, Client_ID FROM ShippingAddresses WHERE Client_ID = '$loopClients[Client_ID]' ORDER BY Address_ID ASC") or die(mysql_error());
        if (mysql_num_rows($grabShipping) == 0) {
          $clientList .= "";
        } else {
    
          $clientList .= "";
          $shippingList .= "";
            
          while ($loopShipping = mysql_fetch_array($grabShipping)) {
            $shippingList .= "";
          }
        
          $shippingList .= "";
        }
      }
    }


    This code, which probably makes many of you cry inside, outputs two variables full of form options. Variable 1 $clientList is just a simple list of clients. Variable 2 $shippingList is a bit more complex in that it shows an OPTGROUP with a label of the Client, and then a listing of all secondary shipping addresses associated with that client.

    My attempts at joining were pretty much failures - in two particular areas.

    1.) How do I prevent $clientList from outputting duplicate entries for Client names? I have 2 clients and 2 Addresses - both addresses belong to a single client. So on the $clientList it's outputting that client twice.

    2.) How do I loop so that for each Address_ID it outputs a new option value? My attempts result in a very odd PHP error about memory limits being exceeded; so I'm sure my loop format is just... broken.

    Source: http://community.livejournal.com/php/558380.html

« php weather questions || Overloading PHP in FastCGI... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home