Jump to content

How to insert line breaks in php while loop?


schult70

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.  :D

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.