Patrick Narcelles
Patrick Narcelles

Reputation: 167

displaying array values in a table

i want to display values from an array in a table, i tried searching and none of it works, here is the snippet

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

any suggestion on how can i display each array value in a different cell, every cell will contain different values from the array

Upvotes: 0

Views: 180

Answers (4)

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

    $sql=mysql_query("select  * from tblname");
    while($row=mysql_fetch_array($sql))
        {
            echo "<td>".$row["id"];
            echo "<td>".$row["name"];
        }

Upvotes: 1

SimonPeacock
SimonPeacock

Reputation: 11

loop through the $STF array with a for loop echoing out the result.

for ($STF as $r) 
{ 
  echo '<td>' .$r["whatever data you want to display here"] .'</td>';
}

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use like this

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

}

don't use $array[] for assigning, it take whatever value pass to it.

Upvotes: 1

Undrium
Undrium

Reputation: 2678

Remove the first:

$row = mysql_fetch_array($result);

Then add a:

var_dump($STF);

after each while loop in your functions. This will help you understand how it works better and hopefully the variable is populated with lots of goodies!

Upvotes: 1

Related Questions