SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach statement

    PHP Code:
    <?php
    class class1 {
        function 
    printout(){
            
    $conn mysql_connect('localhost''user''pass');
                    
    mysql_select_db('db1');
                    
    $sql "SELECT * FROM `table1`";
                    
    $result mysql_query($sql$conn);
                    echo 
    "<table>"
                    
    echo "<tr>"
                    
    foreach (as _){ //rows
                         
    foreach(as _){ //columns
                              
    echo "<td>"._____."</td>"
                         
    }
                         echo 
    "</tr><tr>"
                    
    }
                    echo 
    "</tr></table>"
              
    }
        }
    ?>
    I am trying to use foreach for the first time. Could someone guide me thru this please.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,825
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Sure you will need to use mysql_fetch_assoc

    PHP Code:
                    while ($row mysql_fetch_assoc($query)){ //rows
                         
    foreach($row as $column => $value){ //columns
                              
    echo "<td>".$column."</td>"
                         
    }
                         echo 
    "</tr><tr>"
                    


    However, I should point out, that the better way to approach this is using PDO as the method you are using is discouraged by PHP.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This only prints out column titles for every data row. Is there a way to printout the data?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,825
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Yep, I just simply put the data you had in your example php code and replaced it with the information you indicated, to output the value just echo $value in the same foreach as your echo $column is in.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for clarifying.

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I limit the search to $column in $column['column3']=5 written:

    $column->column3->5?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,825
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    For that, you will use a WHERE clause in your SQL Query (I assumed column3 stores an int value)
    Code:
    $sql = "SELECT * FROM `table1` WHERE column3 = 5";

  8. #8
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it possible to limit the data shown using the:

    foreach($table1->$column as $column3 => 5){

    or something like that?

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,825
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can, it isn't ideal, but you can. Here is an example
    PHP Code:
                    while ($row mysql_fetch_assoc($query)){ //rows
                         
    foreach($row as $column => $value){ //columns
                              
    if ($column == 'column3' && $value == 5) {
                                   echo 
    "<td>".$column."</td>"
                              
    }
                         }
                         echo 
    "</tr><tr>"
                    


  10. #10
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you write that with the "->" operator?


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
  •