matvespa Posted August 29, 2010 Share Posted August 29, 2010 I have this code which works fine. I want to improve it by displaying it into multiple columns. Current output: TITLE 1 JOB1 --------- TITLE 2 JOB 2 --------- ETC... I want it to display like this instead: TITLE 1 | TITLE 2 JOB 1 | JOB 2 Here is my code: $sql = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)){ $id = $row["id"]; $firstname = $row["firstname"]; $lastname = $row["lastname"]; $position = $row["position"]; $image = $row["image"]; ?> <table width="183" border="1"> <tr> <td align="center" height="18"> <?php echo "<img id='image_shadowCoaches' src=" . $image . ">"; ?><br /> <?php echo "<div id='Coaches_Detail'>".$firstname." ".$lastname."</div>" ?> <?php echo "<div id='Coaches_DetailLower'>".$position."</div>" ?> </td> </tr> </table> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/ Share on other sites More sharing options...
PaulRyan Posted August 29, 2010 Share Posted August 29, 2010 Hey Matvespa, try the following <table cellpadding="0" cellspacing="0" border="1"> <tr> <?PHP // Create the the query $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; // Execute the above query here $result = mysql_query($sql); // While records exists keep fetching them while($row = mysql_fetch_array($result)){ // Give each of the records a $variable name $id = $row['id']; $firstname = $row['firstname']; $lastname = $row['lastname']; $position = $row['position']; $image = $row['image']; ?> <td align="center" height="18" width="183"> <?php echo '<img id="image_shadowCoaches" src='.$image.'>'; ?><br /> <?php echo '<div id="Coaches_Detail">'.$firstname.' '.$lastname.'</div>'; ?> <?php echo '<div id="Coaches_DetailLower">'.$position.'</div>'; ?> </td> <?php } ?> </tr> </table> Hope it helps you, if not just let me know. Thanks, Paul. Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104822 Share on other sites More sharing options...
matvespa Posted August 29, 2010 Author Share Posted August 29, 2010 Hey Paul, Thanks you very much. It works. But unfortunately, how do i limit the no. of column per row. Like for example only allowing 2 output in each row. If my database has 3 output, there would be 2 output in row1 and 1 output in row2. Any idea? Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104826 Share on other sites More sharing options...
Birdmansplace Posted August 29, 2010 Share Posted August 29, 2010 try changing this $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; to $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach' LIMIT 2"; (edit) oops my bad miss understood you. Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104827 Share on other sites More sharing options...
matvespa Posted August 29, 2010 Author Share Posted August 29, 2010 Thank you for your contribution but Limit 2 only shows 2 output instead of showing the remaining on a next row. Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104828 Share on other sites More sharing options...
PaulRyan Posted August 29, 2010 Share Posted August 29, 2010 I was just about to say that Matvespa, he wasn't far off though Try the following <table cellpadding="0" cellspacing="0" border="1"> <tr> <?PHP $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; // Create the the query $result = mysql_query($sql); // Execute the above query here $number = '0'; // Set a number so we can limit the number of TD's per row $tbNumber = '2'; // Set how many TD's you want per row // While records exists keep fetching them while($row = mysql_fetch_array($result)){ // Give each of the records a $variable name $id = $row['id']; $firstname = $row['firstname']; $lastname = $row['lastname']; $position = $row['position']; $image = $row['image']; ?> <td align="center" height="18" width="183"> <?php echo '<img id="image_shadowCoaches" src='.$image.'>'; ?><br /> <?php echo '<div id="Coaches_Detail">'.$firstname.' '.$lastname.'</div>'; ?> <?php echo '<div id="Coaches_DetailLower">'.$position.'</div>'; ?> </td> <?php if(is_int($number/$tbNumber)) { echo '</tr><tr>'; } } ?> </tr> </table> This should work now, if not let me know. Thanks, Paul. Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104829 Share on other sites More sharing options...
matvespa Posted August 29, 2010 Author Share Posted August 29, 2010 Thank you for your reply Paul. It seem not to be working. Its back to square 1 now. It shows: TITLE 1 TEXT 1 ---------- TITLE 2 TEXT 2 ---------- TITLE 3 TEXT 3 Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104830 Share on other sites More sharing options...
PaulRyan Posted August 29, 2010 Share Posted August 29, 2010 Sorry about that Matvespa, I over looked a simple little bit of code, I didn't increment the $number variable, use the following... <table cellpadding="0" cellspacing="0" border="1"> <tr> <?PHP $myQuery1 = "SELECT * FROM coaches WHERE position = 'Junior Coach'"; // Create the the query $result = mysql_query($sql); // Execute the above query here $number = '1'; // Set a number so we can limit the number of TD's per row $tbNumber = '2'; // Set how many TD's you want per row // While records exists keep fetching them while($row = mysql_fetch_array($result)){ // Give each of the records a $variable name $id = $row['id']; $firstname = $row['firstname']; $lastname = $row['lastname']; $position = $row['position']; $image = $row['image']; ?> <td align="center" height="18" width="183"> <?php echo '<img id="image_shadowCoaches" src='.$image.'>'; ?><br /> <?php echo '<div id="Coaches_Detail">'.$firstname.' '.$lastname.'</div>'; ?> <?php echo '<div id="Coaches_DetailLower">'.$position.'</div>'; ?> </td> <?php if(is_int($number/$tbNumber)) { echo '</tr><tr>'; } $number ++; } ?> </tr> </table> Thanks, Paul. Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104832 Share on other sites More sharing options...
matvespa Posted August 29, 2010 Author Share Posted August 29, 2010 Hey thanks Paul. It WORKS! Appreciate it alot! Link to comment https://forums.phpfreaks.com/topic/211993-display-multiple-columns/#findComment-1104841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.