简体   繁体   中英

PHP tables not printing out?

I'm calling a specific function, either getTaskList0 , getTaskList1 , or getTaskList2 , each the same as below, with the number after task referring to the getTaskList number (in TaskManagerDAO ):

function getTaskList0(){
  $lst=array();
  $con=$this->getDBConnection();
  $result = $con->query("SELECT name, score FROM task0 ORDER BY score DESC");
  $i = 0;
  $counter=0;
  while (($row =$result->fetch_row()) && ($counter<5)) {
      $rec = new Task($row[0],$row[1]);
      $lst[$i++]=$rec;
      $counter++;
  }
  return $lst;
}

Task:

 <?php
class Task{
    public $name;
    public $score;

    function __construct($name,$score) {        
        $this->name = $name;
        $this->score = $score;
    }

}

The function is called in here:

<?php
session_start();

if(!class_exists('Action')){
    include_once 'Action.php';
}
if(!class_exists('TaskManagerDAO')){
    include_once 'TaskManagerDAO.php';
}

class Action_DisplayList implements Action{
    public function execute($request){
        if(!isset($_SESSION['functCount']))
                $_SESSION['functCount']=0;
        $functCount=$_SESSION['functCount'];
        $functionName="getTaskList{$functCount}";
        echo $functionName;
        $dao = new TaskManagerDAO();
        $names = $dao->$functionName();
        $_SESSION['names'] = $names;
        $last = sizeof($names);
        $start = 0;
        $_SESSION['start'] = $start;
        $_SESSION['last'] =$last;
        header("Location: tasklist.php");
    }
}
?>

I'm trying to call the function by putting a variable at the end of the function name as a string and then calling the function. This works as I don't get any errors, but the list does not show up in the table, which is what this code is supposed to do:

<?php
  if(isset($_POST['Add']) && ($_POST['name']!=="") &&!empty($_POST['Add'])){
      include 'Action_Add.php';
      Action_Add::execute();
   }
   $functCount=0;
   $_SESSION['functCount']=0;
   $names = $_SESSION['names'];
   $start=$_SESSION['start'];
   $last = $_SESSION['last'];
   for($count = $start; $count < $last; $count++){
      $name=$names[$count];
      echo "<tr>";
      echo "<td>".($count+1)."</td>";
      echo "<td>".$name->name."</td>";
      echo "<td>".$name->score."</td>";
      echo "</tr>";
   }
   ?>

EDIT: I restarted my computer and everything and for the first run load of the page it worked perfectly. After that though, it stopped working though.

Use session_start(); before start using $_SESSION . So in your case, try something like

<?php

session_start();

if(isset($_POST['Add']) && ($_POST['name']!=="") &&!empty($_POST['Add'])){
//.... remaining code here

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM