0

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

2
  • Not part of the question but you should be using mysqli or PDO extensions to connect to your database and retrieve data instead of mysql. Commented Sep 20, 2012 at 14:06
  • i am still trying to look for tutorials for mysqli Commented Sep 21, 2012 at 10:02

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.