English 中文(简体)
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!

最佳回答

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:

See

问题回答

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> 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;




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签