#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Incrementing variable, to get new html table row


    I need a new html table row for every 3rd result returned from the mysql query (2 per cell). However, when I try to use a while condition to do it, I get no results. This is what I'm using (i have no idea what I'm doing in this regard). Do you see any mistakes?

    PHP Code:
    <table width="600px">
    <?php
    require ('./includes/mysql_connectPublic.php'); // Connect to DB
    $query "SELECT DISTINCT(subclass.subclass), subclass.sub_cls_key, subclass.sub_cls_pic, model.cls_key, model.sub_cls_key FROM subclass, model WHERE model.cls_key='$_GET[cls_key]' AND subclass.sub_cls_key=model.sub_cls_key ORDER BY subclass ASC";
    $result = @mysql_query ($query); // Run the query
    while ($row mysql_fetch_array($result)) {
    $subclass $row['subclass'];
    $sub_cls_key $row['sub_cls_key'];
    $sub_cls_pic $row['sub_cls_pic'];
    $i 0;
    while (
    $i >= 2) {
    $varCell "<tr><td>";
    $varCellEnd "</tr></td>";
    }
    echo 
    $varCell."<a href=equipment.php?cls_key=".$_GET['cls_key']."&sub_cls_key=".$sub_cls_key."><img src=".$sub_cls_pic."><b>\n".$subclass."</a></b>".$varCellEnd;
    }
    ?>
    </td></tr>
    </table>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    this makes no sense. When you use a "while" loop with a condition that's true from the beginning and never changes, you simply get an infinite loop. I guess what you actually wanted is a counter outside of the "while ($row = ..." and an "if" statement that checks whether the current counter value is a multiple of 2:

    PHP Code:
    $i 0;
    while (
    $row mysql_fetch_array($result)) { 
        ...
        if (
    $i == 0) { 
            
    $varCell "<tr><td>"
            
    $varCellEnd "</tr></td>"
        } 
        echo 
    $varCell ...
        
    $i++;

  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,871
    Rep Power
    6351
    I see a number of mistakes, yes.

    1) @something() suppresses errors from that call. If your function is throwing an error, you're purposely refusing to read it.

    2) if $i = 0, $i is never > 2. That inner while will never run.

    You want something more like:

    PHP Code:
    while ($row mysql_fetch_array($result) && ++$i) { 
    $subclass $row['subclass']; 
    $sub_cls_key $row['sub_cls_key']; 
    $sub_cls_pic $row['sub_cls_pic']; 
    echo 
    "<td><a href=equipment.php?cls_key=".$_GET['cls_key']."&sub_cls_key=".$sub_cls_key."><img src=".$sub_cls_pic."><b>\n".$subclass."</a></b></td>"
    if ( 
    $i == ) {
      echo 
    "</tr><tr>";
    }

    Comments on this post

    • BitZoid agrees : This is a helpful post.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Thank you


    I was way over thinking this problem. That modulus operator is useful, thank you for your support. I have been mulling over this and a few other questions in my head for days now. Good to get it out of the way and learn something in the process.

    Thanks again.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    You can avoid logical errors like this by waiting with the PHP stuff until you really know what you want to do. Draw a diagram, maybe write down some pseudo code. And only then do the actual programming.

    I think many people make the mistake of fumbling with "if"s and "while"s way too early. That's not a good approach, because it will distract you from the basic ideas. When I have to solve a complicated problem, I turn off the computer screen and sit down with a piece of paper. And I won't write a single line of code before I've thought everything through.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    11
    Rep Power
    0
    That is exactly what I was about to suggest when I started reading. (along with some other comments). Good answer.

    Originally Posted by ManiacDan
    PHP Code:
    if ( $i == ) { echo "</tr><tr>"; } 

IMN logo majestic logo threadwatch logo seochat tools logo