How could I connect those two loops?

Discussion in 'PHP' started by x0x, Jan 24, 2010.

  1. #1
    First loop:


    $get = $DB->query("SELECT * FROM $tab[fam] WHERE uid = '$uid' ORDER BY id ASC", __FILE__, __LINE__);
    while ($results = $DB->fetch_array($get)){

    //second loop inside it

    $get2 = $DB->query("SELECT * FROM $tab[mob] WHERE fam = '$results[id]' ORDER BY newrank ASC", __FILE__, __LINE__);
    while ($memb = $DB->fetch_array($get2)){

    }


    How would I get this into one loop (query)? I can only write simple queries....

    edit: basically the point is to get everything from the $tab[mob] table where fam = id of the table $tab[fam] - (there are multiple per $tab[fam].id)
     
    Last edited: Jan 24, 2010
    x0x, Jan 24, 2010 IP
  2. hireme

    hireme Member

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #2
    I think your query should be something like this:

    SELECT * FROM $tab[fam] a, $tab[mob] b WHERE a.uid=$uid AND b.fam=a.id ORDER BY id, newrank

    something like that.. :)

    tell me if it works.. :)
     
    hireme, Jan 24, 2010 IP
    CarbonLife likes this.
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks, it works.

    Here's my loop:
    
    $get = $DB->query("SELECT * FROM $tab[fam] a, $tab[mob] b WHERE a.uid=$uid AND b.fam=a.id", __FILE__, __LINE__);
         while ($results = $DB->fetch_array($get)){
    Code (markup):

    I can access table $tab[fam] with $results. Is there a way to access $tab[mob] too since it's also pulled? It's not so important. Just wondering.



    EDIT:


    Also wondering about this one:

    if(!$DB->fetchone("SELECT * FROM $tab[fam] a, $tab[mob] b WHERE a.uid='$uid' AND b.fam=a.id' AND a.id='$findID'", __FILE__, __LINE__)){ $message .= "Not found. ";

    Why doesn't that work? It should do the same what before AND THEN check if $findID matches an id from the results pulled from the $tab[mob] table.

    EDIT2: I can't believe I didn't see that little " ' " before the part in bold. But other than that it should work, right?
     
    Last edited: Jan 24, 2010
    x0x, Jan 24, 2010 IP
  4. hireme

    hireme Member

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #4
    yup you can access it.. :) note, though that if you have similar column names (for example id for $tab[fam] and id for $tab[mob]), it would be better to specify which columns you need in your select statement and change similar column names using AS (for example: select a.id as aid, b.id as bid ...)

    what's the error for the second problem? One thing I can see is this:

    b.fam=a.id' (the quote)

    although I don't know if you just made a mistake in typing here..
     
    hireme, Jan 24, 2010 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Yes, that was the problem, the quote.

    But how can I access it through variables? $results['name'] gives me the name field from the mob table. I tried $results['a.name'] to get the name field from the fam table but it did not work...
     
    x0x, Jan 24, 2010 IP
  6. hireme

    hireme Member

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #6
    yup, that was what I was saying.. to access it you have to specify it on your select statement.. the only problem with this is that you have to specify all variables you need..

    for example

    SELECT a.name as aname, b.name as bname, a.uid, b.fam...

    something like that..

    then you can now access it by $results['aname'],$results['bname']
     
    hireme, Jan 24, 2010 IP