Number for each post

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Number for each post

Post by gumphfy »

Hi I'm currently coding a comment system for me news script (not very hard at all...). The only things I seem to have trouble with are the small things, which turn out to be somewhat complicated...i.e. I want to have the number for each comment posted. Note all comments to every news, are stored in one table, separated by newsID (nid)...
Now...I'm using this:

Code: Select all

$id = $_GET['id'];
$result = mysql_query("select * from nws_cmt where nid like '$id' order by 'id' DESC");
$num = mysql_num_rows( $result );
echo $num;
Which, works as it should, but what I really want is for every comment to have a number of their own like; 1 2 3 4 ... etc. and starting from 1 in every news entery.

...Maybe I'm explaining this very poorly, but I don't have the sufficient terminology.

Now I've been reading a lot at php.net, and searching google quite a few times, but I can't seem to find what I'm looking for..I know it's possible to do, since many forum systems have that function (VBulletin, Invision Board etc.) But I cant get to look at their code without actually having to pay for it....

Alright, enough babble...I'll make it simple:
row number, for each comment, what should I do?

Oh did I mention my PHP knowledge is limited? (I Have no idea why the PHP tag is f'ing up...sry)
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post by phpCCore Brad »

Does each comment have a unique ID or do they all share the same unique ID as the orginal news post. In other words is the table structure such that there is something like this

id - title - body -newsID

or

title-body-newsID


Either way it can be done, but it is more clean if each comment has a unique integer to identify it.
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Post by gumphfy »

Each comment does have their own unique ID.
If it helps, here the table structure:

Code: Select all

CREATE TABLE `nws_cmt` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `nid` int(11) unsigned NOT NULL,
  `date` varchar(255) default NULL,
  `name` varchar(255) default NULL,
  `email` varchar(255) default NULL,
  `title`varchar(255) default NULL,
  `txt` text,
  KEY `id` (`id`)
) ENGINE=MyISAM;
Hope that'll help you, to help me ^^
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Post by gumphfy »

bump
...sry I really need this...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Use an array, iterate through the array and number each iteration...

Code: Select all

<?php
$comment_array[] = mysql_fetch_array($comment_query);

for ($i = 0; $i < count($comment_array); $i++)
{
    $counter = $i + 1;
    echo $counter . ' - ' . $comment_array[$i]['field_name1'] . ' is field 1, ' . $comment_array[$i]['field_name2'] . ' is field 2, etc...';
}
?>
Doing it this way uses a numbering system from the array not the DB, which is what you want I believe. Each number would be sequential starting at 1. Of course, you can change this to suit your need, but the overall concept is what you are looking for I think.
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Post by gumphfy »

It's almost what I'm looking for. But there's just one problem about it. the first post is numbered ok by #1, but the next post is numbered by #12, then #123...etc.

This is my entire code for the comment system:

Code: Select all

<table border="0" width="530px" cellpadding="0" cellspacing="0">
<?php
$id = $_GET['id'];
$result = mysql_query("select * from nws_cmt where nid like '$id' order by 'id' DESC");
$numn = mysql_num_rows( $result );
while($row = mysql_fetch_array($result))

{
?>
<tr><td class="lbar" width="10px"></td>
<td class="mbar" width="447px"><font class="bartxt">Topic: <? echo $row['title']; ?></font></td>
<td class="rbar" width="73px"><font class="bartxt">#
<? 
$comment_array[] = $row; 

for ($i = 0; $i < count($comment_array); $i++) 
{ 
    $counter = $i + 1; 
    echo $counter; 
}
?>
</font></td>
</tr><tr>
<td class="line" colspan="3">
<font class="text">&nbsp;Author: <a href="mailto:<? echo $row['email']; ?>"><? echo $row['name']; ?></a><br>&nbsp;Date: <? echo $row['date']; ?>
</font>
</td></tr>
<tr><td class="border" colspan="3">
<font class="meow">&nbsp;Comment:<br>&nbsp;<? echo $row['txt']; ?></font>
</td></tr>
<tr><td colspan="3"><br></td></tr>
<?
}
?>
</table>
I have excluded the

Code: Select all

$comment_array[$i]['field_name1'] . ' is field 1, ' . $comment_array[$i]['field_name2'] . ' is field 2, etc...';
part, since I didn't see any need for it ... But even with it in place it would still act the same way, as without.
Oh and btw, I'm running PHP5, if that means anything...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The for loop needs to be outside the while loop. Use the while loop to set $comment_array[] = $row. Close the while loop then do a for loop. I know it seems like a but much, but I do this kind of thing to reuse arrays throughout my code. This way I can select once and use as much as needed.

If you are using this result set once only, you can do away with the whole assignment of the array and for loop. To get what you want, add a $counter = 1 just before your while loop. Then after the code in the while loop is executed but before you close the while loop, add in a $counter++;. Something along the lines of...

Code: Select all

<?php
$counter = 1;
while($row = mysql_fetch_array($result))
{
?>
    <tr>
        <td class="lbar" width="10px"></td>
        <td class="mbar" width="447px"><font class="bartxt">Topic: <? echo $row['title']; ?></font></td>
        <td class="rbar" width="73px"><font class="bartxt">#<?php echo $counter; ?></font></td>
    </tr>
    <tr>
        <td class="line" colspan="3"><font class="text">&nbsp;Author: <a href="mailto:<? echo $row['email']; ?>"><? echo $row['name']; ?></a><br>&nbsp;Date: <? echo $row['date']; ?></font>
        </td>
    </tr>
    <tr>
        <td class="border" colspan="3"><font class="meow">&nbsp;Comment:<br>&nbsp;<? echo $row['txt']; ?></font>
        </td>
    </tr>
    <tr>
        <td colspan="3"><br></td>
    </tr>
<?php
$counter++;
} 
?>
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Post by gumphfy »

Ahhh, I see...thx pal, worked like a charm...Isn't it great with these "Ah" moments, it's like feeling stupid and enlightened at the same time :p
Anyway, thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There are entire threads in here about super stupid moments. And there are just as many threads about our finally reaching that point of "Holy crap, it was THAT simple!". Good luck with the rest of your project.
Post Reply