Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: PHP Code help please

  1. #1
    Join Date
    Jun 2003
    Location
    Launceston, Tasmania
    Posts
    12,347
    Total Downloaded
    0

    PHP Code help please

    Ok, i'm trying to sort out the REMLR membership page.

    I can get all of the info to display here

    Member summary list - REMLR

    and this is the code

    <?php
    $hostname="";
    $username="";
    $password="";
    $dbname="richar3_remlr";
    //connect to the database
    $dbhandle = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");
    //select a database to work with
    $selected = mysql_select_db($dbname,$dbhandle) or die("Could not select examples");
    $result = mysql_query("SELECT membership.membership_no, membership.name_public, membership.state, arn.ARN, census.census_short, arn.vehicle_name"
    . " FROM arn INNER JOIN"
    . " mem2arn ON arn.ARN = mem2arn.ARN INNER JOIN"
    . " census ON arn.census = census.census RIGHT OUTER JOIN"
    . " membership ON mem2arn.membership_no = membership.membership_no");
    //fetch tha data from the database
    echo "<center><table width=600 border=0 cellspacing=8 cellpadding=0>";
    while ($row = mysql_fetch_array($result)) {echo "
    <tr bgcolor=#CCCCCC>
    <td colspan=4>REMLR No. ".$row{'membership_no'}."
    </td>
    </TR>
    <TR>
    <td colspan=4>".$row{'name_public'}."
    </td>
    </TR>
    <tr>
    <td width=150>&nbsp;
    </td>
    <td>".$row{'ARN'}."
    </td>
    <td> -
    </td>
    <td> ".$row{'census_short'}. "
    </td>
    </tr>
    <TR>
    <td colspan=4 align=right>".$row{'state'}."
    </td>
    </TR>";
    }
    echo "</table></center>";
    ?>

    however it produces one entry per member. what I want to do is produce one member, and show each of their vehicles in a list, then go to thenext member.

    I know I need another nested statement in there, but I can't seem to get it to work.
    1994 Discovery TDi
    2004 Discovery 2 TD5
    2010 Discovery 4 TDV6
    1961, Series 2 Ambulance. 108-098 - Eden

    Registry of Ex Military Land Rovers Mem. 129
    Defence Transport Heritage Tasmania Member

  2. #2
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    4,656
    Total Downloaded
    0
    "Boys, get daddy's exorcism bag..."

    Ned Flanders



    Greeny's posessed - he's talkin' in tongues!
    [B][I]Andrew[/I][/B]

    [COLOR="YellowGreen"][U]1958 Series II SWB - "Gus"[/U][/COLOR]
    [COLOR="DarkGreen"][U]1965 Series IIA Ambulance 113-896 - "Ambrose"[/U][/COLOR]
    [COLOR="#DAA520"][U]1981 Mercedes 300D[/U][/COLOR]
    [U]1995 Defender 110[/U]
    [SIGPIC][/SIGPIC]

  3. #3
    mikehzz Guest
    Hello,

    I think you need 2 queries instead of the 1 with the join. Loop through the members, and create the new query by fetching the cars for that member

    Something like-

    Loop1 from member list (select * from members)
    print member
    loop2 get car list for the member (select * from cars where member=".$row["memberid"])
    print cars
    end of loop2
    end of loop1

    Does this help?

    Mike

  4. #4
    Join Date
    Jun 2003
    Location
    Launceston, Tasmania
    Posts
    12,347
    Total Downloaded
    0
    Shonky, . this is what i have to do to make these pages work. Not as easy as it appears on the outside!

    Mike, I was trying to avoid that because it generates a lot more statements, so instead of one select statement, it will create 300ish. I know I can do it that way, just trying to avoid it.
    1994 Discovery TDi
    2004 Discovery 2 TD5
    2010 Discovery 4 TDV6
    1961, Series 2 Ambulance. 108-098 - Eden

    Registry of Ex Military Land Rovers Mem. 129
    Defence Transport Heritage Tasmania Member

  5. #5
    Join Date
    Apr 2009
    Location
    Cairns, QLD
    Posts
    115
    Total Downloaded
    0
    Can you give a list of the fields for each table from the database (just the relevant tables) just so that i can see what the links are (not visualising it very well at the time of the morning.

    Cheers
    Josh

  6. #6
    Join Date
    Jun 2003
    Location
    Launceston, Tasmania
    Posts
    12,347
    Total Downloaded
    0
    No problems. the four tables involved are

    arn, census, membership and mem2arn

    arn has all the vehicle information and census code

    census has census code and nomenaclature information

    mem2arn has a list of arn's corresponding to member numbers

    Membership has members name and accress and member no etc.

    arn has:
    arn, wheelbase, date, contract, series, census, engine, chassis, body, notes, disposal, tiresize, vehicle_name, service

    census has:
    census, nomenaclature, liability, notes, census short, type

    mem2arn has:
    membership_no, arn

    Membership has:
    membership_no, name, name_public, state, address, email, AULRO, phone, mobile, dob

    in case anybody is looking, DOB is a new one to try and keep, so that we can reasonably presume a member has passed away if over 100 years. a good idea from a helpful REMLR member).
    1994 Discovery TDi
    2004 Discovery 2 TD5
    2010 Discovery 4 TDV6
    1961, Series 2 Ambulance. 108-098 - Eden

    Registry of Ex Military Land Rovers Mem. 129
    Defence Transport Heritage Tasmania Member

  7. #7
    Join Date
    Apr 2009
    Location
    Cairns, QLD
    Posts
    115
    Total Downloaded
    0
    On pondering it i think Mikes idea is the way to go. I don't quite understand what you mean when you said

    Quote Originally Posted by Phoenix View Post

    Mike, I was trying to avoid that because it generates a lot more statements, so instead of one select statement, it will create 300ish.

  8. #8
    Join Date
    Jun 2003
    Location
    Launceston, Tasmania
    Posts
    12,347
    Total Downloaded
    0
    what I mean is, that it does another select statement for each member that it comes to, but at the moment there are already 300 members, or am I being overly pedantic about how many select statements are called?
    1994 Discovery TDi
    2004 Discovery 2 TD5
    2010 Discovery 4 TDV6
    1961, Series 2 Ambulance. 108-098 - Eden

    Registry of Ex Military Land Rovers Mem. 129
    Defence Transport Heritage Tasmania Member

  9. #9
    Join Date
    Apr 2009
    Location
    Cairns, QLD
    Posts
    115
    Total Downloaded
    0
    It will only require two queries that it will loop over and over but processing time should be quite small.

  10. #10
    Join Date
    Apr 2009
    Location
    Cairns, QLD
    Posts
    115
    Total Downloaded
    0
    Just so that i can make sure we're on the same page here is your code changed round a bit to do the 2 query 2 loop way.



    <?php
    $hostname="";
    $username="";
    $password="";
    $dbname="richar3_remlr";
    //connect to the database
    $dbhandle = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");
    //select a database to work with
    $selected = mysql_select_db($dbname,$dbhandle) or die("Could not select examples");

    THIS QUERY WILL GO TO SOMETHING SIMPLE LIKE

    $result = mysql_query("SELECT membership.membership_no, membership.name_public, membership.state,
    FROM membership);

    THUS MAKING THIS QUERY NOT NEEDED
    $result = mysql_query("SELECT membership.membership_no, membership.name_public, membership.state, arn.ARN, census.census_short, arn.vehicle_name"
    . " FROM arn INNER JOIN"
    . " mem2arn ON arn.ARN = mem2arn.ARN INNER JOIN"
    . " census ON arn.census = census.census RIGHT OUTER JOIN"
    . " membership ON mem2arn.membership_no = membership.membership_no");


    //fetch tha data from the database
    echo "<center><table width=600 border=0 cellspacing=8 cellpadding=0>";
    while ($row = mysql_fetch_array($result)) {echo "
    <tr bgcolor=#CCCCCC>
    <td colspan=4>REMLR No. ".$row{'membership_no'}."
    </td>
    </TR>
    <TR>
    <td colspan=4>".$row{'name_public'}."
    </td>
    </TR>
    ADD SECOND QUERY HERE
    SOMTHING LIKE

    $secondResult = mysql_query("SELECT arn.ARN, census.census_short, arn.vehicle_name"
    . "FROM (all the fun join parts )"
    ."WHERE mem2arn.membership_no = " . $row{membership_no});

    THEN ADD A SECOND WHILE
    while ($secondRow = mysql_fetch_array($secondResult)) {echo

    <tr>
    <td width=150>&nbsp;
    </td>
    <td>".$secondRow{'ARN'}."
    </td>
    <td> -
    </td>
    <td> ".$secondRow{'census_short'}. "

    THEN ADD A CLOSE OF LOOP HERE ie }

    </td>
    </tr>
    <TR>
    <td colspan=4 align=right>".$row{'state'}."
    </td>
    </TR>";
    }
    echo "</table></center>";
    ?>


    It needs a bit of fixing up but think the general principles are their for it.

Page 1 of 3 123 LastLast

Bookmarks

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
  •  
Search AULRO.com ONLY!
Search All the Web!