简体   繁体   中英

Line Break within echo in a while loop

Quick question, again, I'm sure this is ridiculously simple but I don't see what I'm doing wrong!

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
echo  "<a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']} </a>";
}

This produces all my links to be stacked up one after the other. I want to order them in a list so I have tried:

echo "<ul>";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
echo  "<li><a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']}    </a> </li>";
}
echo "</ul>" ;

and

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
echo  "<a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']}    </a> <br />";
}

The ultimate result I wish to see is :
-Link 1
-Link 2
-Link 3
-Link 4
What am I doing wrong? Thanks in advance!

I can't spot anything wrong with it. Even a <br/> should work:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo  "<a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']} </a><br/>";
}

使用echo "<a href="…>link</a>\\n在生成的源代码中添加换行符

Try this

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$layout .= "<div style='display:block;'><a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']}</a></div>";
}

echo $layout;

For a line break in HTML try using <br /> , your code can look like something along this line:

echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{ 
echo  "<li><a href=\"http://mysite.com/{$row['row1']}/{$row['row2']} \">{$row['row3']}</a></li> <br />";
}
echo "</ul>";

EDIT

Also note, as mentioned before in the comments, <br /> isn't needed, if I take it out of my code my output stays the same.

I made a test php file similar and used my suggestion above and works fine, here is my code

<?php
echo "<ul>";
$i = 0;
do {
$i++;
echo  "<li><a href=\"http://mysite.com/{$i}/{$i} \">{$i}</a></li> <br />";
} while($i < 10);
echo "</ul>";
?>

Displayed below:

看到

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM