Jump to content

grmihel

Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by grmihel

  1. I guess there are a couple ways. One way would be to get the customers first, and loop through those and send a query for each one to get the apartments. That's the most obvious way, but it's never a good idea to put a query in a loop for scalability reasons. A join would work, where the customer information would get duplicated on each apartment row, and you'd need some logic to keep track of the last customer you saw so that when you're getting the next row from the result you can determine if this is a new customer where you would close the previous customer and open a new one before listing the apartments. It's going to be more of a manual process to create the XML then the combination of XML and SQL you have in the C# code. In PHP those are separate, your XML document isn't connected to the database. Although you may be able to find a class or library that someone has written to approximate what you have there.
    Thanks for your input. I didn't think about how to loop through each apartment for each customer.The problem with the joint is excatly as you wrote, that you have to make some code that track if it is a new customer or a apartment that should append to the current customer.For now I have created the solution where I:1. Looping through each Customer with a while for row fetching.2. For each Customer I'm looping through, I'm creating a new loop, with a SELECT DISTINCT for each Apartment, where the Apartment's FK is equal with the Customer's PK. There is a bit more coding than I'm used to with C#, but when using the following structure:
    while($row1 = sqlsrv_fetch_array($result_AllCustomers, SQLSRV_FETCH_ASSOC)){	//Set CustID	$custID = $row1['CustID'];	//Create customernode	$node = $root->appendChild(				$dom->createElement("customer"));	//Create Customername as attribute to customer node	$node->appendChild(			$dom->createAttribute("customerName"))->appendChild(					$dom->createTextNode(utf8_encode($row1['CustName'])));etc...

    Its still kinda readable and easy to overview in the source. And even more important, it now has the same XML output structure as my original C# code. Is there any pros and cons for the solution I'm using now? I mean, are there any basics do's and don'ts that I'm violated with this loop with a loop, that I should be aware of?

  2. I'm probably too damaged of working with C# and LinQ, so havn't done a real SQL sentance since my work in Java.I have two tables, which are combined with a PK and FK. Table_MyCust with PK CustID and Table_CustApartments which contain CustID as FK.I want to make a XML document with PHP, where there is a node for each MyCust row, and for each MyCust there is a CustApartments for each apartment that have the current CustID as FK. In C# I've do something like the following:

    XElement xml = new XElement("customers",			    from p in DatabaseConnection.Custs			    orderby p.CustName			    select new XElement("customer",				    new XAttribute("customerName", p.CustName),				    new XElement("Addresses",					    (from l in p.Aparts.Distinct()					    orderby l.Street  where l.Street != null && !l.Postal.Equals("1010")					    select new XElement("Address", (string)l.Street);			    )));

    But how does I do in PHP? Does you create a SQL sentance with some kind of a join operation? Or do you create two SQL statments, and check for equals on the PK and FK while running through all the rows? I'm not sure whats the 'correct' way to do in PHP, since I'm fairly new to PHP scripting yet.

×
×
  • Create New...