Forum Moderators: coopster

Message Too Old, No Replies

Multiple similar php requests on a page

Best way to do it?

         

abbeyvet

1:24 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bear with me here - I have only very rudimentary PHP skills.

Basically am customising a CMS to produce a blog-like output. There is no way with this CMS to integrate comments, but that's ok, I have set up a separate comments DB, which stores comments based on the URL of the post page (permalink) which works fine.

I have it all pretty much sorted, except for pulling the number of comments on a given post from a comments table to display with headlines in archives etc.

I am doing it with individual posts as follows:

$sqlquery = "SELECT * FROM $table WHERE href = '/example/example.php'";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);

$i = 0;

if ($number < 1) {
print "No Comments";
}
else {
if ($number > $i) {
print "Comments ($number)";
$i++;
}
}
?>

This is easy, as I can get the CMS to print the href value to the page.

But am not sure how to do it when there are multiple headline on a page and thus multiple href entries to get from the table. Somehow I don't think including that whole thing beneath each headline is necessary or efficient - or is it?

Thanks

mcibor

1:36 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think better solution for you would be:

$sqlquery = "SELECT COUNT(*) AS count, href FROM $table GROUP BY href";
$result = mysql_query($sqlquery);

while($row = mysql_fetch_assoc($result))
{

then in $row['count'] you will have the number of comments for each $row['href']

When $row[$href] is not set (!isset($row['/example/example.php']) then number of comments is 0.

Hope this helps
Michal

abbeyvet

2:30 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks and I can see that would work IF the posts and headlines were being generated from the same database - but they are not. The comments and posts are stored COMPLETELY independently. I don't see how it would work in this situation.

Maybe I didn't explain it well.

The posts and headlines are managed by a CMS which outputs actual physical pages, that is to say they are not retrieved from a database as and when requested by the user, they exist on the server. There is no MySql database involved, data is stored in flat text.

I want it this way - the choice is totally deliberate. The output pages have PHP extensions and it is easy to add PHP functionality to the various templates that generate them.

The form for submitting comments (which is included on individual post pages) grabs the url of the page it is on, and stores this along with each comment and its associated user info etc in a single row in a table of a databse.

I can retrieve the information I want easily when there is just a single post on a page, my difficulty is figuring out how to do it when it is a page with a variable number of headlines, which are not generated by the comments DB.

Obviously I can identify the url of the post to which each headling links - and it is this information which I need to use to retrieve the number of relevant comments from the DB and display them with the headline.

Sorry to go on so lengthily - it's hard to explain :(

joelgreen

2:52 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



You can select from different databases in one query. Just preppend database name to the table name.

joelgreen

3:25 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Can't you hook the php code you posted into each headline? So it would print headline, then run your code to print comments for the headline.

abbeyvet

5:36 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can select from different databases in one query.

But only the comments are in a database.

Can't you hook the php code you posted into each headline?

Yes, but I was sort of hoping there was a way that didn't require so many queries.

The logic I am thinking might work goes like this, but I have no idea where to start to turn this into PHP - I just need a pointer in the right direction and I may be able to figure it out.

1. get the the number of comments in the table for each unique url in the column 'href'.

2. Identify each unique url in the table and (somehow) make each a variable

3. Assign the appropriate number of comments as a value to the relevant variable

So, I would end up with something like:

$page-one = 0
$another-page = 2
$third-page = 6
$this-page = 3
etc

Then after each headline I could just print the appropriate value and it could all be done with a single DB connection.

The page names have no particular logic - they are manually entered, usually as two words separated by a hyphen. So they aren't numeric - eg post-1, post-2, post-3 etc. Plus they are stored with their .php extension, which I would have to strip away.

Maybe I won't bother - it's starting to make my head burst. But thanks for your help so far.