flemingmike Posted January 18, 2011 Share Posted January 18, 2011 hello, normally when i query mysql, it returns my results line by line. im trying to make a print view now that will put 3 results in a row before going to the next row. any help appreciated. Quote Link to comment Share on other sites More sharing options...
johnny86 Posted January 18, 2011 Share Posted January 18, 2011 You have to be a little more specific than that. Do you need to pull data out of three seperate tables? If that's the case you'll need some joins in your statement. There is a tutorial about that in PHPFreaks tutorials section. Quote Link to comment Share on other sites More sharing options...
flemingmike Posted January 18, 2011 Author Share Posted January 18, 2011 no, sorry. im pulling from one table. im hoping that the results will be displayed in 3 columns, and as many rows as needed. Quote Link to comment Share on other sites More sharing options...
gergy008 Posted January 18, 2011 Share Posted January 18, 2011 <?php //Connect to the database prior to this code ?> <table width="200px" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td width="33%" align="center" bgcolor="#00005F"><strong>col</strong></td> <td width="33%" align="center" bgcolor="#00005F"><strong>col2</strong></td> <td width="33%" align="center" bgcolor="#00005F"><strong>col3</strong></td> </tr> <?php $sql="SELECT `col`, `col2`, `col3` FROM `table` WHERE 1=1;"; //Change the 1=1 to why you want to pull from the database because of. $result=mysql_query($sql, $link); while($rows = mysql_fetch_array($result)){ ?> <tr> <td <?php echo $rows['col']; ?></td> <td <?php echo $rows['col2']; ?></td> <td><?php echo $rows['col3']; ?></td> </tr> <?php } ?> </table> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 18, 2011 Share Posted January 18, 2011 I believe what he is trying to achive is to have records displayed in a grid where each row in the grid will display three records - not one record per row with three fields. So, row one displayes records 1-3, row 2 displayes records 4-6, etc. Here is some pseudo code //Variable to determine number of records in a row $recordsPerRow = 3; $query = "SELECT * FROM table"; $result = mysql_query($query); $recIdx = 0; while($row = mysql_fetch_assoc($result)) { //Increment the record index $recIdx++; //Open new row if first record in row if($recIdx%$recordsPerRow==1) { echo "<tr>\n"; } //Display the record echo "<td>{$row['somevalue']}</td>\n"; //Close row if last record in row if($recIdx%$recordsPerRow==0) { echo "</tr>\n"; } } //Close last row if not already done if($recIdx%$recordsPerRow!=0) { echo "</tr>\n"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.