0

嗨,网络编程大师。我有这个代码:

<?PHP

$db_user = 'user';
$db_pass = 'password';
$db_name = 'dbname';
$db_host = 'localhost';
if(mysql_connect($db_host,$db_user,$db_pass)) {
    mysql_select_db($db_name);
    mysql_query("CREATE TABLE IF NOT EXISTS smsads(id bigint unsigned primary key auto_increment, link varchar(255), fromnum varchar(60))");
    $res = mysql_query("SELECT * FROM smsads ORDER BY id DESC LIMIT 36");
    while($row = mysql_fetch_object($res)) {

        if ($res>=36){

        $http_link = $row->link;
        $root_link = strpos($http_link, '/');
            if ($root_link !== false) {
            $before = substr($http_link, 0, $root_link);
        }

        echo "<div id=\"banner\"><a href=\"http://{$before}\" alt=\"{$before}\" title=\"{$before}\" target=\"_blank\"><img src=\"http://{$http_link}\" /></a></div>";
    }
        else {
            echo $res . "<div id=\"banner\"></div>";
        }
    }
}
?>
</div>

如我们所见,获取的行数限制为 36。如果行小于 36,如何显示现有行并为每个行添加其他内容,直到 36?例如,这是一个像素广告脚本,我想可视化现有像素(例如,我有 20 个插入的像素),并将空框显示到其他空的地方(总共 36 个地方 - 20 个已放置的项目 = 16 个空位)直到总数达到 36 个盒子。

如您所见,我尝试使用“If”、“else”,但它始终只显示空框(因为我不知道如何告诉它显示 RESULTS + 空框)...

4

3 回答 3

2

将所有内容重写为 for 循环,这也使所有内容更易于理解:

for($cnt = 0; $cnt < 36; $cnt++){
    if($row = mysql_fetch_object($res)){
        // In here you should have the code for the boxes to be filled.
        $http_link = $row->link;
        $root_link = strpos($http_link, '/');
        if ($root_link !== false) {
            $before = substr($http_link, 0, $root_link);
        }
        echo "<div id=\"banner\"><a href=\"http://{$before}\" alt=\"{$before}\" title=\"{$before}\" target=\"_blank\"><img src=\"http://{$http_link}\" /></a></div>";
    }else{
        // In here you should have the code for the empty boxes
        echo "<div id=\"banner\"></div>";
    }
}

这将始终遍历循环 36 次,那些时候它会找到一行它会打印该行,否则它将打印空横幅标签。

于 2009-09-15T14:02:12.517 回答
0

使用mysql_num_rows

if (mysql_num_rows($res) >= 36) {
    // ...
}
于 2009-09-15T14:29:26.163 回答
0

如果 if 语句为真,您是否测试过执行的代码?

于 2009-09-15T15:11:27.420 回答