javascript - PHP echoing a table, appending it to a specific element in the DOM? -


i have code, checks database , returns rows php code containing 4 values (id, playera, playerb, turn, int).

i use array build table , append table specific location in dom, dont know how that.

i way (get rows via js ajax , use js build , append table), know how, dont want that.

is possible create table , append div using php/html/css ?

thanks

<?php if (isset($_session["userid"])){     $dbmanager = dbmanager::app();     $manager = new manager($_session["userid"]);     $gamelist = $manager->getgames();      if ($gamelist) {         debug::log("got active games: ".sizeof($gamelist);     }     else {         debug::log("no games");     }    } else {         debug::log("no user id"); }  ?>  <!doctype html> <html> <head>     <link rel='stylesheet' href='style.css'/>     <script src="jquery-2.1.1.min.js"></script>     <script src='script.js'></script>     <script src='ajax.js'></script> </head>     <body>         <input type="button" value="creategame" onclick="creategame()">         <divid="gamelistdiv">         <div><a href="logout.php">logout</a></div>     </body> </html> 

edit

 <?php     $table = "";      if ($gamelist) {         foreach ($gamelist $game){                    $table += "<tr>";             $table += "<td>";             $table += $game["name"];             $table += "</td>";             $table += "</tr>";         }          $table += "</table>";     } ?>      <body>         <input type="form" id="gamename" placeholder="enter game name here"></input>             <input type="button" value="creategame" onclick="creategame()"></input>         <div>             <span>active games</span>             <?php echo $table; ?>                </div>                       <div><a href="logout.php">logout</a></div>     </body> 

you need understand dom not yet exist - created browser, , browser builds based on output of combined php & html.

there many ways solve problem without resorting ajax calls etc.

<!doctype html> <html> <head>     <link rel='stylesheet' href='style.css'/>     <script src="jquery-2.1.1.min.js"></script>     <script src='script.js'></script>     <script src='ajax.js'></script> </head>     <body>         <input type="button" value="creategame" onclick="creategame()">         <div id="gamelistdiv">          <?php              if (isset($_session["userid"])){                 $dbmanager = dbmanager::app();                 $manager = new manager($_session["userid"]);                 $gamelist = $manager->getgames();                  if ($gamelist) {                     debug::log("got active games: ".sizeof($gamelist);                      echo '<table style="width:100%">';                      //assuming can iterate on $gamelist value                     foreach($gamelist &$game)                     {                         //here assume result returned object these properties - might case need $game['playera'] or $game->getdata('playera') - not sure database lib using.                         echo '<tr>                             <td>'.$game->playera.'</td>                             <td>'.$game->playerb.'</td>                             <td>'.$game->turn.'</td>                           </tr>';                     }                      echo '</table>';                 }                 else {                     debug::log("no games");                 }                }             else {                     debug::log("no user id");             }              ?>          </div>         <div><a href="logout.php">logout</a></div>     </body> </html> 

in example running php in-line html.

you if wanted keep of database logic @ top of page , not in-line html:

<?php     /* database logic here */     $variable = '<span>this variable contain old html came database logic</span>'; ?>  <html> <head>  </head> <body>     <php echo $variable; ?> </body> </html> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -