SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and HTML layout problem

    Hello, I am trying to fetch some results from a mysql database to my html code, however the layout I want presents a problem because it includes two cells in one row:

    Code:
    <table>
    <tr>
    <td>result</td>
    <td>result</td>
    </tr>
    </table>
    So my question is how can I show the records from the database using this layout, without having each record twice in both cells.

    For example, this is how I obtain the records from the mysql database:

    PHP Code:

      
    // Query the database
      
    $equipo_ = @mysql_query('SELECT id, rub FROM aut_equ_rub');
      if (!
    $equipo_) {
      exit(
    "<p>$message</p>");
      }

    // Process the result and show the records

    while ($equipo mysql_fetch_array($equipo_)) {
        
    $eid $equipo['id'];
        
    $enom htmlspecialchars($equipo['rub']);
        
        
    $result = @mysql_query(
          
    "SELECT * FROM aut_equ WHERE aut_id='$id' AND rub_id='$eid'");
        if (!
    $result) {
          exit(
    '<p>Error: ' mysql_error() . '</p>');
        }
        
        if (
    mysql_num_rows($result)) {
          echo 
    "<tr><td><input type='checkbox' checked='checked' name='equ[]' value='$eid' />$enom</td><td><input type='checkbox' checked='checked' name='equ[]' value='$eid' />$enom</td></tr>\n";
        } else {
          echo 
    "<tr><td><input type='checkbox' name='equ[]' value='$eid' />$enom</td><td><input type='checkbox' checked='checked' name='equ[]' value='$eid' />$enom</td></tr>\n";
        }
        
      } 
    As you can see, I get the same result twice per each row, how can I avoid this. Thanks a lot

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // Query the database
      
    $equipo_ = @mysql_query('SELECT id, rub FROM aut_equ_rub');
      if (!
    $equipo_) {
      exit(
    "<p>$message</p>");
      }

    // Process the result and show the records

    $i 0;
    while (
    $equipo mysql_fetch_array($equipo_)) {
        
    $eid $equipo['id'];
        
    $enom htmlspecialchars($equipo['rub']);
        
        
    $result = @mysql_query(
          
    "SELECT COUNT(*) FROM aut_equ WHERE aut_id='$id' AND rub_id='$eid'");
        if (!
    $result) {
          exit(
    '<p>Error: ' mysql_error() . '</p>');
        }
        
        
    //start new row every other iteration
        
    if ($i && $i == 0) {
            echo 
    "</tr><tr>";
        }
        
    $i++;

        if (
    mysql_result($result0) > 0) {
          echo 
    "<td><input type='checkbox' checked='checked' name='equ[]' value='$eid' />$enom</td>\n";
        } else {
          echo 
    "<td><input type='checkbox' name='equ[]' value='$eid' />$enom</td>\n";
        }
        
      } 
    Also, you can do this with only one database query, I just don't know where $id comes from...

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works fine, I have not quite figured it out, but definitely works.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    &#37; is the modulus operator, it gives you the remainder of integer division (like you did before you learned about decimal points and fractions).

    $i is just a counter for how many times the loop has run. That number, divided by two, will have a remainder of 0 whenever the number is even, since all even numbers are evenly divisible by 2 (have no remainder after division).

    Whenever $i % 2 == 0, a new row is started (</tr><tr>). On both odd and even iterations of the loop, a single cell is printed out <td>checkbox</td>.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, thanks for the explanation, however if its ok I would like to ask you about this piece of code:

    if (mysql_result($result, 0) > 0)

    I dont understand why a comma after $result and the > 0.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I modified your query to select a COUNT() of rows instead of the contents of the rows since all you were doing was checking how many were returned. It's incredibly inefficient (crippling with a lot of data) to ask for the entire contents of every matching row just to check how many rows there are.

    mysql_result($result, 0) will give you the 0th row of the result set, which contains only one column, so the return value is just the result of the COUNT().. the number of rows.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for your time. All set now


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
  •