page_id in database

Hi all,

To make comments appear on different pages of your website (without getting mixed up), you’ll need to add a new column to the database, something like ‘page_id’.

After this you will need to assign each page a unique ID number by which you will distinguish it from the others (the best option is to use the same ID your CMS has assigned to the page).

When you insert a comment, you will have to add this ID as well. To show the comments for this particular page, just add a WHERE clause to the SELECT query in demo.php:

[php] $comments = array();
$result = mysql_query(“SELECT * FROM comments
WHERE page_id = “.$theIDofThePage.” ORDER BY id ASC”);[/php]

I created a database, now after days of searching and a lot of trouble i still not able to get it working…
So i will be glad to get some help.

Ok, i created a database…, it is working.
I put this in my webpage:

[php]<?php

// Error reporting:
error_reporting(E_ALL^E_NOTICE);

include “connect.php”; // conection details in here
include “comment.class.php”;

/*
/ Select all the comments and populate the $comments array with objects
*/

$comments = array();
$result = mysql_query(“SELECT * FROM comments
WHERE page_id = “.$theIDofThePage.” ORDER BY id ASC”);

while($row = mysql_fetch_assoc($result))
{
$comments[] = new Comment($row);
}

?> [/php]

I created the database like this:

[code]–
– Table structure for table comments

CREATE TABLE comments (
id int(10) unsigned NOT NULL auto_increment,
name varchar(128) collate utf8_unicode_ci NOT NULL default ‘’,
url varchar(255) collate utf8_unicode_ci NOT NULL default ‘’,
email varchar(255) collate utf8_unicode_ci NOT NULL default ‘’,
page_id varchar(50) collate utf8_unicode_ci NOT NULL default ‘’,
body text collate utf8_unicode_ci NOT NULL,
dt timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;[/code]

Now, my page_id is in the database, but where must i put the “.theIDofThePage.”?

Hope someone understand want i’m doing here…

when you insert something into the database and you want it for a specific page create a separate input give it a name of pageid you can even hide it if you want
[php]

<input type=hidden name=pageid value=(whatever page id you want it to have)>[/php]
now when you do your inserting into the database you would insert{$_POST[‘pageid’]} into the pageID field.
so for example lets say pageid 1, is what you want to show up on the home page, so we will add something to the home page
[php]

<? mysql_query("INSERT INTO comments (id, comment, page_id)VALUES('', '{$_POST['comment']}', '{$_POST['pageid']}')"); [/php]

now that code up there is completely unsecure you should validate everything but it will show you how to set everything up.
Now when you want to call the information to show on the home page you would do something like:
[php]
$a=mysql_query(“SELECT * FROM comments WHERE page_id=1”);
while($b=mysql_fetch_assoc($a)){
echo"{$a[‘comments’]}
";
}
[/php]

hope that leads you in the right direction :wink: again don’t forget to validate all the information before you send it to the database!

Sponsor our Newsletter | Privacy Policy | Terms of Service