开发者

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开发者_Go百科.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/>";
}


use echo "<a href="…>link</a>\n to add a newline in the generated sourcecode


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:

Line Break within echo in a while loop

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜