0

hello I'm wiritng a simple dynamic website in which the content of a div.container is loaded dynamically by jquery and since I also needed to update the url without reloading the page I use this script:

function ChangeUrl(page, url) {
  if (typeof (history.pushState) != "undefined") {
    var obj = { Page: page, Url: url };
    history.pushState(obj, obj.Page, obj.Url);
  }
}

$('ul.menu li a').on('click', function(){
    var page = $(this).attr('href');
    var pageUrl = page.split("/");
    pageUrl = pageUrl[1];

    $('.container').load(page + '.php', function(){
      //fadeout old content
      //fadein new content
    });

    ChangeUrl('Page1', '?page=' + page);

    return false;
})

this script also allows me to reload (cmd+r // ctrl+r) the page with the exact div content. Now the problem is that I wrote a specific page in which inside the div.content I display an sql table:

<div class="section-inner main-content page">
 <?php
   $pdo = new PDO('mysql:host=localhost;port=8889;dbname=Table','user','pass');
   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $smtm = $pdo->query("SELECT item1, item2, item3,item4 FROM Table");
   echo '<table border="1">'."\n";
   while ($row = $smtm->fetch(PDO::FETCH_ASSOC)) {
     echo "<tr><td>";
     echo($row['item1']);
     echo("</td><td>");
     echo($row['item2']);
     echo("</td><td>");
     echo($row['item3']);
     echo("</td><td>");
     echo($row['item4']);
     echo("</td></tr>\n");
   }
   echo "</table>\n";
 ?>
</div>

but when I reload this page from the browser the table is not displayed. any suggestion?

ddgg
  • 145
  • 1
  • 10
  • You're saying it is not displayed when you reload but is it displayed the firs time? Where is your PHP code located? What does it return ? Did you call your database 'Table' ? `dbname=Table` – La masse Oct 03 '17 at 10:58
  • yes yes the first time it's displaying with no problem...so I guess all the variable are correct – ddgg Oct 03 '17 at 12:32

3 Answers3

0

in php you can close php tags write html or js codes and continue again by opening the php tags. Like;

$row = mysql_fetch_array($query, MYSQL_ASSOC);    
while($mysql_num_rows($query)){
    ?>
    <table>
    <td><? echo $row[col1]; ?></td>
    <td><? echo $row[col2]; ?></td>
    <?
    }

and go on....

No you can display your html code easly with loading this php file dynamicly into yor div.

$('div').load('url.....');
Güney Saramalı
  • 791
  • 1
  • 10
  • 19
  • I corrected my code, because the problem is not actually the table formatting but the fact that it doesn't reload if I refresh the page... – ddgg Oct 03 '17 at 12:31
  • if you dynamicly load it you can set "$(document).ready(function()" of js with somevariables to check if matches and load it with $(...).reload(url) function. – Güney Saramalı Oct 03 '17 at 12:53
0

If you want to reload the previously loaded page, when refreshing your main page, just keep it in a cookie

  function ChangeUrl(page, url) {
  if (typeof (history.pushState) != "undefined") {
    var obj = { Page: page, Url: url };
    history.pushState(obj, obj.Page, obj.Url);
  }
}

$('ul.menu li a').on('click', function(){
    var page = $(this).attr('href');
    // Save your cookie here
    document.cookie = "lastpage=" + page;
    var pageUrl = page.split("/");
    pageUrl = pageUrl[1];

    $('.container').load(page + '.php', function(){
      //fadeout old content
      //fadein new content
    });

    ChangeUrl('Page1', '?page=' + page);

    return false;
})
// When page is loaded
$(document).ready(function() {
    var lastpage = getCookie("lastpage");
    if (lastpage) {
        // Do the load stuff
    }
});

Hope this helps !

You will find the getCookie and setCookie functions there : https://www.w3schools.com/js/js_cookies.asp

La masse
  • 1,190
  • 1
  • 10
  • 24
  • I'm looking into cookies, which I've never used before, but in the meantime: why I don't need to use cookies to reload other non-database-related contents? they loads perfectly fine... – ddgg Oct 04 '17 at 18:49
0

Even thought the cookie suggestion worked perfectly, I figured out the simplest solution to my problem (which I found here)

I now initialise the pdo connection including pdo.php:

require_once (__DIR__.'/pdo.php');

in which I copied:

$pdo = new PDO('mysql:host=localhost;port=8889;dbname=Table','user','pass');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

specifying __DIR__ to be sure that the relative path is solved when the page is loaded.

hope this helps

ddgg
  • 145
  • 1
  • 10