PHP显示由3个CSS列中的两个连接变量组成的mysql查询结果

时间:2012-09-24 18:02:07

标签: php mysql css

标题显示了我需要完成的内容,但是我得到的是因为页面构建是3列,每行的结果相同。每一行都不同,但每一行的每一列都重复相同的事情,而不是第一,第二和第三列的不同内容。

我在下面的代码中遗漏了一些东西来使这项工作,任何帮助将不胜感激。

谢谢。

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$results = mysql_query("SELECT title,link FROM syndicate ORDER BY popularity DESC LIMIT 75");
while ($row = mysql_fetch_array($results)){

$title = $row['title'];
$link = $row['link'];

if ($divtest = 1){
//insert in div col1;
echo '<div class="col1">';
///<!-- Column 1 start -->
echo '<a href="' . $link . '" target="_blank">' . $title . '</a><br><br>'; /// add all links here
///<!-- Column 1 end -->
echo '</div>';

$divtest = 2;
}

if ($divtest = 2){
//insert in div col2;
echo '<div class="col2">';
///<!-- Column 2 start -->
echo '<a href="' . $link . '" target="_blank">' . $title . '</a><br><br>'; /// add all links here
///<!-- Column 2 end -->
echo '</div>';

$divtest = 3;
}

if ($divtest = 3){
//else {
//insert in div col3;
echo '<div class="col3">';
///<!-- Column 3 start -->
echo '<a href="' . $link . '" target="_blank">' . $title . '</a><br><br>'; /// add all links here
///<!-- Column 3 end -->
echo '</div>';

$divtest = 1;
}

}

2 个答案:

答案 0 :(得分:1)

您错误地在=语句中使用了分配运算符if,而您可能意味着==运算符。

更改以下行:

if ($divtest = 1){
..
if ($divtest = 2){
..
if ($divtest = 3){

正确的:

if ($divtest == 1){
..
if ($divtest == 2){
..
if ($divtest == 3){

答案 1 :(得分:0)

因为

$divtest = 2; 
$divtest = 3; 
$divtest = 1;

在每个if语句的最后一个中,每三个如果在while的一个循环中运行。

我认为你应该设置$ divtest值而不是用这些

来改变条件
if ($divtest == 1){}

if ($divtest == 2){}

if ($divtest == 3){}