Reputation: 2976
Hey guys I have the following sample. I have navigation which is loaded from an mySQL databse with an PHP file called getNavi.php:
$query="SELECT * FROM projects WHERE category=\"$category\"";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
while ($row = mysql_fetch_object($result)) {
$title=$row->title;
$id=$row->id;
echo "<div class=\"sublink\" data-subsite=\"$id\" data-category=\"$category\" data-title=\"$title\" ><a href=\"#\">$title<br />";
}
These links are loaded correctly and are handled via AJAX:
$(document).ready(function(){
$('.link').click(function(){
var subsite = $(this).data('subsite');
$('#naviLeftContent').load('php/getNavi.php?category='+subsite);
});
$(document).on("click", ".sublink", function(){
var subsite = $(this).data('subsite');
var category = $(this).data('category');
var title = $(this).data('title');
var info = category + "/" + title;
var lower = info.toLowerCase();
var nospaces = lower.split(' ').join('');
$('#titleContent').text(title);
$('#imageContent').load('php/getImages.php?info='+nospaces);
$('#textContent').load('php/getText.php?id='+subsite);
$('#infosContent').load('php/getInfos.php?id='+subsite);
});
});
And this part is where it starts to get buggy, because the content of my divs is sometimes only partially loaded or not at all. The Text is not cut, but mostly the title and the infos are not loaded. And sometimes it occures that the infos toggle while i repeat click on the same link all the time.
The problem is that in all PHP files I load I open and close a SQL connection. Now as a Java programmer I thought it would be nice to create a class for my connection with member functions, which I could call. Of course I thought of an singleton object, but I don't know how to call the methods when I would create a PHP file like this:
<?php
class DBConnection {
private static $instance;
private function __construct() {
$user="root";
$password="";
$database="klb";
mysql_connect("localhost",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
}
public function __destruct() {
mysql_close();
}
public static function getInstance() {
if(!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function getNaviForCategory($category) {
$query="SELECT * FROM projects WHERE category=\"$category\"";
$result=mysql_query($query);
$num=mysql_numrows($result);
while ($row = mysql_fetch_object($result)) {
$title=$row->title;
$id=$row->id;
echo "<div class=\"sublink\" data-subsite=\"$id\" data-category=\"$category\" data-title=\"$title\" ><a href=\"#\">$title<br />";
}
}
public function getInfosForProject($id) {
$query="SELECT * FROM projects WHERE id=\"$id\"";
$result=mysql_query($query);
$num=mysql_numrows($result);
while ($row = mysql_fetch_object($result)) {
$infos=$row->info;
echo $infos;
}
}
private function createTableProjects(){
$query="CREATE TABLE projects (id int(6) NOT NULL auto_increment,category varchar(30) NOT NULL,title varchar(30) NOT NULL,
info varchar(200) NOT NULL,text varchar(8000) NOT NULL,PRIMARY KEY (id),UNIQUE id (id))";
mysql_query($query);
}
}
?>
I now need some replacement for this lines of code:
$('#infosContent').load('php/getInfos.php?id='+subsite);
EDIT :
I solved this problem with the following code:
<?php
require_once( 'dbconnection.php');
$category = $_GET["category"];
DBConnection::getInstance()->getNaviForCategory($category);
?>
But it seems that my on click listener for the sublink divs (which is mentioned above) not always pass the right variables to the php code.
$('#titleContent').text(title);
This line of code for example only works if I choose another category.
Upvotes: 0
Views: 645
Reputation: 12101
may be in getNavi.php instead old code
$category = intval($_GET['category']);
DBConnection::getInstance()->getNaviForCategory($category);
Upvotes: 1