Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder DjDanHayden's Avatar
    Join Date
    Nov 2008
    Location
    Ireland
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Question While loop help needed

    Hi all,

    I have the following loop,

    PHP Code:
                                $con $db->query("SELECT team FROM tournamentteams WHERE tournyid='$tournyid' LIMIT 8");
                                while(
    $teams $db->fetch_array($con))
                                {            
                                    
    $getteams=mysql_query("SELECT title FROM team WHERE teamid='$teams[team]'");
                                    
    $getteams=mysql_fetch_array($getteams);
                
                                    echo
    '<tr style="height:40px;">
                                            <td class="team">
                                                <a href="/xbox360/modernwarfare3/tournaments/MW3-9-1/team/hov">'
    .$getteams[title].'</a><span style="float:right; padding-right: 2px;">0</span>
                                            </td>
                                            <td class="nofill"><div class="fill"></div></td>
                                         </tr>
                                         <tr>
                                            <td style="width:153px; height:40px;">
                                                <img src="/images/versus.png" class="versus" id="match-19066">                           
                                            </td>
                                         </tr>'
    ;
                                } 

    My database table 'tournamentteams' has 16 colums

    m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12 m13 m14 m15 m16


    I need this loop to only output the first 8 (m1->m8) then stop.


    Can can anyone help ?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Are you referring to having 16 columns or 16 rows? You say columns, but the current code uses LIMIT 8 which would refer to the rows.
    You may need to verify this structure. With the generic names, its difficult to determine if you actually have a need for 16 columns, or if there is a better alternative for normalization structures.
    Given what you have here, the first thing you want to do is join these queries. They are related on teamid => team according to the code here.
    PHP Code:
    $sQry 'SELECT t.title
            FROM tournamentteams tt
            INNER JOIN team t ON (t.teamid = tt.team)
            WHERE tournyid = "' 
    $tournyid '"
            LIMIT 8'
    ;
    if (
    $qry mysql_query($sQry))
    {
        while (
    $getteams mysql_fetch_assoc($qry))
        {
            echo
    '<tr style="height:40px;">
                <td class="team">
                    <a href="/xbox360/modernwarfare3/tournaments/MW3-9-1/team/hov">'
    .$getteams[title].'</a><span style="float:right; padding-right: 2px;">0</span>
                </td>
                <td class="nofill"><div class="fill"></div></td>
            </tr>
            <tr>
                <td style="width:153px; height:40px;">
                    <img src="/images/versus.png" class="versus" id="match-19066">                           
                </td>
            </tr>'
    ;
        }

    As for columns, what you specify as your column list in the query is what you will get back. If you need 8, you add the 8 that you require. There are several ways of dealing with them, as soon as you fetch it into an associative or indexed based array, you can use a foreach to build the columns. That works fine as long as there is some logical structure that doesn't need any specific handling.

  • Users who have thanked Fou-Lu for this post:

    DjDanHayden (09-16-2012)

  • #3
    New Coder DjDanHayden's Avatar
    Join Date
    Nov 2008
    Location
    Ireland
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Are you referring to having 16 columns or 16 rows? You say columns, but the current code uses LIMIT 8 which would refer to the rows.
    You may need to verify this structure. With the generic names, its difficult to determine if you actually have a need for 16 columns, or if there is a better alternative for normalization structures.
    Given what you have here, the first thing you want to do is join these queries. They are related on teamid => team according to the code here.
    PHP Code:
    $sQry 'SELECT t.title
            FROM tournamentteams tt
            INNER JOIN team t ON (t.teamid = tt.team)
            WHERE tournyid = "' 
    $tournyid '"
            LIMIT 8'
    ;
    if (
    $qry mysql_query($sQry))
    {
        while (
    $getteams mysql_fetch_assoc($qry))
        {
            echo
    '<tr style="height:40px;">
                <td class="team">
                    <a href="/xbox360/modernwarfare3/tournaments/MW3-9-1/team/hov">'
    .$getteams[title].'</a><span style="float:right; padding-right: 2px;">0</span>
                </td>
                <td class="nofill"><div class="fill"></div></td>
            </tr>
            <tr>
                <td style="width:153px; height:40px;">
                    <img src="/images/versus.png" class="versus" id="match-19066">                           
                </td>
            </tr>'
    ;
        }

    As for columns, what you specify as your column list in the query is what you will get back. If you need 8, you add the 8 that you require. There are several ways of dealing with them, as soon as you fetch it into an associative or indexed based array, you can use a foreach to build the columns. That works fine as long as there is some logical structure that doesn't need any specific handling.

    Thanks dude, That sorted it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •