schult70 Posted March 27, 2011 Share Posted March 27, 2011 I have developed the following code, which queries a MySQL database and returns multiple results using several WHILE loops: <?php # if submit_hash is present, then pull up the attendance from specified hash if($_GET['submit_hash']) { # select all the hashers from the specified hash $query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = " . $_GET['hash_id'] . " && hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id ORDER BY ha.hasher_name "; $result = mysql_query($query) or die('Error selecting Hash attendance.'); $x = 1; while($row = mysql_fetch_array($result)) { if($x == 1) { echo '<strong>' . $row['hash_num'] . ' ' . $row['hash_name'] . ' - ' . date("D, d M Y", strtotime($row['hash_date'])) . '</strong><em>Total Hashers: ' . mysql_num_rows($result) . '</em>'; $x++; } # see if this person was the hare if($row['hare_id'] == $row['hasher_id'] || $row['hare2_id'] == $row['hasher_id'] || $row['hare3_id'] == $row['hasher_id']) { $hare = '- hare'; } else { $hare = ''; } echo $row['hasher_name'] . ' <b>' . $hare . '</b>'; } } else if($_GET['submit_hasher']) { # if submit_hasher is present, pull up all of their hashes and aliases # select all the hashes that this person has attended $a_query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id && hr.hasher_id = " . $_GET['hasher_id'] . " ORDER BY h.hash_date DESC "; $a_result = mysql_query($a_query) or die('Error selecting the person\'s Hashes . '); $x = 1; while($a_row = mysql_fetch_array($a_result)) { if($x == 1) { echo '<strong>Hash Attendance Record For ' . $a_row['hasher_name'] . '</strong> <em>(' . mysql_num_rows($a_result) . ' total hashes)</em>'; $x++; } echo '#' . $a_row['hash_num'].' ' . $a_row['hash_name'] . ' on ' . date("D, d M Y", strtotime($a_row['hash_date'])); # see if this person was a hare if($a_row['hasher_id'] == $a_row['hare_id'] || $a_row['hasher_id'] == $a_row['hare2_id'] || $a_row['hasher_id'] == $a_row['hare3_id']) { echo ' - <b>hare</b>'; } echo ''; } echo ''; } echo ' <table width="100%"> <br /> <tbody> <tr> <br /> <td> '; ?> The problem is that everything that is returned from the WHILE loops is displayed in a single line. However, I want the data displayed with a line break between each result, like this: Header Return 1 from WHILE loop Return 2 from WHILE loop Return 3 from WHLIE loop How do I need to modify the code to make it display appropriately? Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/ Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 echo '<strong>' . $row['hash_num'] . ' ' . $row['hash_name'] . ' - ' . '<br>' I just took a portion of your first output in the code you listed. All you have to do is add <br> like above where you want a new line. echo '<br>' . $row['hasher_name'] . ' <b>' . $hare . '</b><br>'; In the code you output to see if the person was the hare. By adding <br> to the front and end you'll get a new line before and after this output. I hope this helps you! Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192934 Share on other sites More sharing options...
schult70 Posted March 27, 2011 Author Share Posted March 27, 2011 Thank you for your reply! So I entered the concatenated <br> tags as you suggested, but it still isn't working. Everything still displays on a singular line with no line breaks. Below is the new code. Suggestions? <?php # if submit_hash is present, then pull up the attendance from specified hash if($_GET['submit_hash']) { # select all the hashers from the specified hash $query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = " . $_GET['hash_id'] . " && hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id ORDER BY ha.hasher_name "; $result = mysql_query($query) or die('Error selecting Hash attendance.'); $x = 1; while($row = mysql_fetch_array($result)) { if($x == 1) { echo '<strong>' . $row['hash_num'] . ' ' . $row['hash_name'] . ' - ' . date("D, d M Y", strtotime($row['hash_date'])) . '</strong><em>Total Hashers: ' . mysql_num_rows($result) . '</em>' . '<br />'; $x++; } # see if this person was the hare if($row['hare_id'] == $row['hasher_id'] || $row['hare2_id'] == $row['hasher_id'] || $row['hare3_id'] == $row['hasher_id']) { $hare = '- hare'; } else { $hare = ''; } echo '<br />' . $row['hasher_name'] . ' <b>' . $hare . '</b>'; } } else if($_GET['submit_hasher']) { # if submit_hasher is present, pull up all of their hashes and aliases # select all the hashes that this person has attended $a_query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id && hr.hasher_id = " . $_GET['hasher_id'] . " ORDER BY h.hash_date DESC "; $a_result = mysql_query($a_query) or die('Error selecting the person\'s Hashes . '); $x = 1; while($a_row = mysql_fetch_array($a_result)) { if($x == 1) { echo '<strong>Hash Attendance Record For ' . $a_row['hasher_name'] . '</strong> <em>(' . mysql_num_rows($a_result) . ' total hashes)</em>' . '<br />'; $x++; } echo '<br />' . '#' . $a_row['hash_num'].' ' . $a_row['hash_name'] . ' on ' . date("D, d M Y", strtotime($a_row['hash_date'])); # see if this person was a hare if($a_row['hasher_id'] == $a_row['hare_id'] || $a_row['hasher_id'] == $a_row['hare2_id'] || $a_row['hasher_id'] == $a_row['hare3_id']) { echo ' - <b>hare</b>'; } echo ''; } echo ''; } echo ' <table width="100%"> <br /> <tbody> <tr> <br /> <td> '; ?> Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192946 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 That's how I do it with my while loops hmm. Try adding another echo line with just the <br> tag after your output. echo '<strong>' . $row['hash_num'] . ' ' . $row['hash_name'] . ' - ' . date("D, d M Y", strtotime($row['hash_date'])) . '</strong><em>Total Hashers: ' . mysql_num_rows($result) . '</em>'; echo '<br>'; Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192957 Share on other sites More sharing options...
schult70 Posted March 27, 2011 Author Share Posted March 27, 2011 Unfortunately, no luck. I don't know what the problem is. It's like it just doesn't want to read the line break tag. It is especially crazy in the concatenated header string... it is reading the <strong>, the <em>, and displaying appropriately. It just refuses to read the <br />! Here is the newest non-working code: <?php # if submit_hash is present, then pull up the attendance from specified hash if($_GET['submit_hash']) { # select all the hashers from the specified hash $query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = " . $_GET['hash_id'] . " && hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id ORDER BY ha.hasher_name "; $result = mysql_query($query) or die('Error selecting Hash attendance.'); $x = 1; while($row = mysql_fetch_array($result)) { if($x == 1) { echo '<strong>' . '#' . $row['hash_num'] . ' ' . $row['hash_name'] . ' - ' . date("D, d M Y", strtotime($row['hash_date'])) . '</strong><em> Total Hashers: ' . mysql_num_rows($result) . '</em>' . '<br />'; echo '<br />'; $x++; } # see if this person was the hare if($row['hare_id'] == $row['hasher_id'] || $row['hare2_id'] == $row['hasher_id'] || $row['hare3_id'] == $row['hasher_id']) { $hare = '- hare'; } else { $hare = ''; } echo '<br />'; echo '<br />' . $row['hasher_name'] . ' <b>' . $hare . '</b></br>'; } } else if($_GET['submit_hasher']) { # if submit_hasher is present, pull up all of their hashes and aliases # select all the hashes that this person has attended $a_query = " SELECT h.*, ha.* FROM hashes as h, hashers as ha, hash_records as hr WHERE hr.hash_id = h.hash_id && hr.hasher_id = ha.hasher_id && hr.hasher_id = " . $_GET['hasher_id'] . " ORDER BY h.hash_date DESC "; $a_result = mysql_query($a_query) or die('Error selecting the person\'s Hashes . '); $x = 1; while($a_row = mysql_fetch_array($a_result)) { if($x == 1) { echo '<strong>Hash Attendance Record For ' . $a_row['hasher_name'] . '</strong> <em>(' . mysql_num_rows($a_result) . ' total hashes)</em>' . '<br />'; echo '<br />'; $x++; } echo '<br />'; echo '<br />' . '#' . $a_row['hash_num'].' ' . $a_row['hash_name'] . ' on ' . date("D, d M Y", strtotime($a_row['hash_date'])); # see if this person was a hare if($a_row['hasher_id'] == $a_row['hare_id'] || $a_row['hasher_id'] == $a_row['hare2_id'] || $a_row['hasher_id'] == $a_row['hare3_id']) { echo ' - <b>hare</b>'; } echo ''; } echo ''; } echo ' <table width="100%"> <tbody> <tr> <td> '; ?> Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192964 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 I use CSS for applying styles so I'm not sure if that has something to do with it. But try <br> instead of <br />. Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192967 Share on other sites More sharing options...
schult70 Posted March 27, 2011 Author Share Posted March 27, 2011 When I type in <br>, it is automatically converted to <br /> when I save it. (Working in a Joomla article.) Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192969 Share on other sites More sharing options...
Skewled Posted March 27, 2011 Share Posted March 27, 2011 I've never used Joomla, but from what I know in my experience so far that should have done the trick for you. But you may want to seek out some Joomla experts because from what I can tell the system itself is doing something with the code. http://www.joomlablogger.net/joomla-tutorials/joomla-template-tutorials/tutorial-create-line-breaks-in-joomla-article-titles/ I did some searching and found the above article that is used for applying line breaks to joomla articles, so this may help you understand how they do it and possibly help you get the line breaks you desire. Link to comment https://forums.phpfreaks.com/topic/231873-how-to-insert-line-breaks-in-php-while-loop/#findComment-1192981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.