0

我想在表格中显示数组中的值,我尝试搜索,但都不起作用,这是代码段

$host="localhost";
$username="root";
$password="";
$db_name="sampledb";
$tbl_name="tblsched";
$cd1 = date("Y-m-d");
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT * FROM tblteacher WHERE teacherName='$uname'";
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);

$teacherid  = $row['teacherID'];

function mymainfunc()
{
global $uname;
global $teacherid;
$bgcolor="#ADDFFF";
$tdali="center";
dateto();
datefrom();
$arrayto[] = array();
$arrayfrom[] = array();
$arrayto[] = dateto();
$arrayfrom[] = datefrom();
$x=0;
while($x<count($arrayto[]))
{
echo "<tr>";
echo "<td align=$tdali bgcolor=$bgcolor>$arrayfrom[$x] - $arrayto[$x]</td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>"; 
echo "</tr>";

$x++;
}
}
function dateto()
{
global $uname;
global $cd1;
global $teacherid;
$sql="SELECT SchedTimeTo FROM tblsched WHERE SchedTeacherID=$teacherid and SchedDateFrom<='$cd1' and SchedDateTo>='$cd1' ";
$result=mysql_query($sql);

if($result === FALSE) {
die(mysql_error());
}

$count=mysql_num_rows($result);
$row = mysql_fetch_array($result);

$STF = array();
while($row=mysql_fetch_array($result))
  {
  $STF[] = $row;
}

return $STF;
}

function datefrom()
{
global $uname;
global $cd1;
global $teacherid;
$sql="SELECT SchedTimeFrom FROM tblsched WHERE SchedTeacherID=$teacherid and SchedDateFrom<='$cd1' and SchedDateTo>='$cd1' ";
$result=mysql_query($sql);

if($result === FALSE) {
die(mysql_error());
}

$row = mysql_fetch_array($result);

$STF = array();
while($row=mysql_fetch_array($result))
  {
  $STF[] = $row;
  }

return $STF;
}

关于如何在不同的单元格中显示每个数组值的任何建议,每个单元格将包含来自数组的不同值

4

4 回答 4

1

删除第一个:

$row = mysql_fetch_array($result);

然后添加一个:

var_dump($STF);

在函数中的每个 while 循环之后。这将帮助您了解它如何更好地工作,并希望该变量充满了很多好东西!

于 2012-09-20T12:58:19.873 回答
1

像这样使用

for($x=0;$x<count($arrayto);$x++)
{
echo "<tr>";
echo "<td align=$tdali bgcolor=$bgcolor>$arrayfrom[$x] - $arrayto[$x]</td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>";
echo "<td align=$tdali> </td>"; 
echo "</tr>";

}

不要使用 $array[] 进行分配,它需要传递给它的任何值。

于 2012-09-20T12:59:03.703 回答
1
    $sql=mysql_query("select  * from tblname");
    while($row=mysql_fetch_array($sql))
        {
            echo "<td>".$row["id"];
            echo "<td>".$row["name"];
        }
于 2012-09-20T13:09:34.190 回答
0

循环遍历 $STF 数组,并使用 for 循环回显结果。

for ($STF as $r) 
{ 
  echo '<td>' .$r["whatever data you want to display here"] .'</td>';
}
于 2012-09-20T13:01:31.997 回答