数据库中的循环总数不相同
Looping total not same in database
您好,我有一个脚本 循环 php,每个循环 4 data/row 然后显示广告。
但我有问题.. 因为在数据库中我有数据 123 行。
但在 php 中显示只有 92 row/data :'(
这是我的脚本
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
else
{
echo $school->name_school"<br/>";
}
$i++;
}
?>
当我 运行 在浏览器中显示广告但学校只显示“92 row/data”
如果我删除脚本
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
然后脚本是这样的
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
echo $school->name_school"<br/>";
$i++;
}
?>
此数据显示所有 123 row/data
帮帮我,我想要这样
学校 1
学校 2
学校 3
学校 4
#广告
学校 5
学校 6
学校 7
学校 8
#广告
帮帮我,谢谢
试试这个:
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
echo $school->name_school . "<br/>";
$i++;
}
?>
您想每次都回显学校名称
另外为什么你没有收到错误:
echo $school->name_school"<br/>";
您使用的 if/else 结构使得您不会每隔四个条目就回显一次。无论是否 $i%4==0
,你都希望每次都回显 $school->name_school
。你想这样做:
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
// This line will now display EACH time, not just when $i%4 != 0
echo $school->name_school . '<br/>';
$i++;
}
?>
希望对您有所帮助!
您好,我有一个脚本 循环 php,每个循环 4 data/row 然后显示广告。
但我有问题.. 因为在数据库中我有数据 123 行。 但在 php 中显示只有 92 row/data :'(
这是我的脚本
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
else
{
echo $school->name_school"<br/>";
}
$i++;
}
?>
当我 运行 在浏览器中显示广告但学校只显示“92 row/data”
如果我删除脚本
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
然后脚本是这样的
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
echo $school->name_school"<br/>";
$i++;
}
?>
此数据显示所有 123 row/data
帮帮我,我想要这样
学校 1
学校 2
学校 3
学校 4
#广告
学校 5
学校 6
学校 7
学校 8
#广告
帮帮我,谢谢
试试这个:
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
echo $school->name_school . "<br/>";
$i++;
}
?>
您想每次都回显学校名称
另外为什么你没有收到错误:
echo $school->name_school"<br/>";
您使用的 if/else 结构使得您不会每隔四个条目就回显一次。无论是否 $i%4==0
,你都希望每次都回显 $school->name_school
。你想这样做:
<?php
include "connection.php";
$i=0;
$data_school=mysql_query("select * from school");
while ($school=mysql_fetch_object($data_school))
{
if($i%4==0)
{
echo "<br/><a href='#'><img src='ads.jpg' alt=''></a><br/>";
}
// This line will now display EACH time, not just when $i%4 != 0
echo $school->name_school . '<br/>';
$i++;
}
?>
希望对您有所帮助!