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 5 of 5

Thread: PHP mysql_query

  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    PHP mysql_query

    Okay, so I have a question and I'm wondering and how this would be done.

    So I already know how to query stuff from the database, I know how to update, delete.... And all of the fun stuff. But one of the things that I've been wondering is how would I when I loop the data from the database get it to do in a orderly fashion..

    example: Say I want it to do something like this...

    <table align="center">
    PHP Code:

    //so I know I'd make a while for or foreach but.. I need to order so many right 
    //then </tr> and start a new line

    while($results mysql_fetch_assoc($query)) {

    echo 
    "<tr>";
    echo 
    "<td>" $results['name']  . "</td>"// 1
    echo "<td>" $results['name']  . "</td>"// 2
    echo "<td>" $results['name']  . "</td>"// 3
    //now </tr>
    echo "</tr>";


    </table>

    You get the idea.. But normally you could only do something like this

    PHP Code:

    while($results mysql_fetch_assoc($query)) {

    echo 
    "<tr>";
    echo 
    "<td>" $results['name'] . "</td>"//1
    echo "</tr>";


    Just because a loop like that wouldn't work.. So how is this done?

    Thanks,
    Jon W

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Whats is exactly you're trying to achieve? You want to print 3 columns of names and then start a new line?

  • #3
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Hooray for the for loop! I'd recommend just using a nice i variable to make your <td> breaks.

    Alright, let's say you've got a DB structure like this:

    Code:
    |---carID---|-----carName-----|-----carColor-----|
    |-----------|------------------|------------------|
    |----1------|------Civic------- |-----black -------|
    |----2------|------Mustang----|-----red----------|
    |----2------|------Lotus-------|-----yellow-------|
    I could then implement a while loop such as the one below to print all the results out:

    PHP Code:
    <?php

    // Open the connection to your database
    openDatabaseConnectionFunction();

    // Grab your data using a mysql_query
    $sql mysql_query"SELECT * FROM cars_table ORDER BY `carID` ASC" );

    // Let's use a classic incremental variable, i
    $i 0;

    // Now, so we don't create a new <tr> if the table has ended
    $totalEntries mysql_num_rows$sql );

    while( 
    $row mysql_fetch_array$sql ) ){
        
        
    // Let's reduce the total entries to the number after this loop instance
        
    $totalEntries--;
        
        
    // Create a new table if the increment is back at the beginning so long as there are more entries
        
    if( $i == && $totalEntries ){
            
            echo 
    "<tr>\n";
            
        }
        
        if( 
    $totalEntries  <= ){
            
            
    // If there are no more entries after this loop, close off the table.
            
    echo "<td>" $row['carName'] . "</td>\n";
            echo 
    "</tr>\n";
            
        }else{
            
            
    // If there are more entries then just print the entry without closing the table
            
    echo "<td>\n" $row['carName'] . "</td>\n";
            
        }

         
    // If we're at the end of a block, let's start a new one
         
    if( $i == ){

              
    // Close up the table and then start a new one
              
    echo "</tr>\n";
              
    $i 0;
              
         }else{
             
             
    // Increment $i
             
    $i++;
             
         }
         
    }

    ?>
    That should then print:

    Code:
    <tr>
    <td>Civic</td>
    <td>Mustang</td>
    <td>Lotus</td>
    </tr>
    Last edited by ShaneC; 03-24-2009 at 05:11 AM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    So just that I'm understanding this. when you say...

    PHP Code:
    $totalEntries--; 
    That is the same as

    PHP Code:
    $totalEntries ==''
    Right?

  • #5
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    No, $totalEntries--; has the opposite effect of $i++;. Essentially, with ++ you are adding one to that variable, with -- you are subtracting one.

    So if $totalEntries was 10, and you performed $totalEntries--; then $totalEntries would now be 9.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com


  •  

    Posting Permissions

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