GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Nov-2005, 19:14
Zurke Zurke is offline
New Member
 
Join Date: Dec 2004
Posts: 12
Zurke is on a distinguished road

comparing two columns


Is it possible two compare two columns in a database?

I have never tried to compare two columns so I dont know if it is the way I have written it or if it is even possible.#counts total posts in a thread.

PHP Code:

$query="SELECT * FROM threads WHERE post_id = id";
   $result=mysqli_query($connection,$query);
    if(!$result){
     echo"<b>unable to select total posts @ this time</b>";
       exit();  
    } 
  while
   ($posts = mysqli_num_rows($result)){
    }
   if($posts == 0){$posts = "no replies";} 



"post_id" and "id" are the columns.

I am trying to retrieve the number of posts to a thread and place the result on a page next to the listed thread.

Am I making any sense? Can anyone help?
Last edited by LuciWiz : 14-Nov-2005 at 03:08. Reason: Please insert your Php code between [php] & [/php] tags
  #2  
Old 13-Nov-2005, 23:22
fallen_samurai fallen_samurai is offline
Awaiting Email Confirmation
 
Join Date: Oct 2005
Location: Wisconsin, USA
Posts: 29
fallen_samurai is on a distinguished road

Re: comparing two columns


I am building a forum system right now and it is possible to count posts in a certain thread, or threads in a topic for that matter. The way I did it was by creating separate tables for topics, threads, and posts. And in the threads table I used topic_id (which would equal the "id" field of the topic it belonged to), then in the posts table I made a thread_id (again associating the post to the thread table's "id").

For example (very simple tables):

PHP Code:

topics(id, name, description)
threads(id, topic_id, name, author, timestamp)
posts(id, thread_id, name, author, timestamp) 



With these tables you then join them in a SQL search statement:

PHP Code:

$query = "SELECT * FROM topics,threads where threads.topic_id = topics.id"; 



This is called a class-based query search, in which you use two or more tables and therefore have to associate the field you are looking for to a table by adding the table's name in front of it: TABLENAME.FIELD.

If any of this doesn't make sense, please let me know
Last edited by LuciWiz : 14-Nov-2005 at 03:09. Reason: Please insert your Php code between [php] & [/php] tags
  #3  
Old 14-Nov-2005, 18:58
Zurke Zurke is offline
New Member
 
Join Date: Dec 2004
Posts: 12
Zurke is on a distinguished road

Re: comparing two columns


fallen_samurai, I am doing the same thing you are. Only difference is I use one table. When someone makes a post I associate the row number of the thread to the post as a value. post_id gets its number from the id of the thread. At this point I am trying to match them up and count them.

But I cant seem to get it to work.
  #4  
Old 15-Nov-2005, 02:21
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: comparing two columns


If you really want some suggestions, you need to supply the table structure with some example data and what you expect to retrieve.
  #5  
Old 15-Nov-2005, 09:10
Zurke Zurke is offline
New Member
 
Join Date: Dec 2004
Posts: 12
Zurke is on a distinguished road

Re: comparing two columns


Quote:
Originally Posted by JdS
If you really want some suggestions, you need to supply the table structure with some example data and what you expect to retrieve.

Hi JdS, I can try and do that.

Table structure:
http://zurke.biz/table.php

Location:
http://zurke.biz/location.php

As you can see in the table structure the post_id has the same number as the thread_id's row 'id'. When a thread is created post_id is '0' and thread_id takes on a section number, that I can group together. When a post is created thread_id is '0' and Post_id takes on the row 'id' of the thread_id to match them up.

I want to count how many post_id's have a particular thread_id, place it into a variable for a while statement and display them as replies/posts to a particular thread. As shown on the "location" document I provided.

If this is not enough info just ask. I can get whatever you need. I have tried for some time to get it to work but I can only put them into an array. An array doesnt work because there could be 10, 20, 100 + posts, who knows?

I appreciate the help. Thanks.
  #6  
Old 16-Nov-2005, 10:45
Zurke Zurke is offline
New Member
 
Join Date: Dec 2004
Posts: 12
Zurke is on a distinguished road

Re: comparing two columns


Quote:
Originally Posted by Zurke
Hi JdS, I can try and do that.

Table structure:
http://zurke.biz/table.php

Location:
http://zurke.biz/location.php

As you can see in the table structure the post_id has the same number as the thread_id's row 'id'. When a thread is created post_id is '0' and thread_id takes on a section number, that I can group together. When a post is created thread_id is '0' and Post_id takes on the row 'id' of the thread_id to match them up.

I want to count how many post_id's have a particular thread_id, place it into a variable for a while statement and display them as replies/posts to a particular thread. As shown on the "location" document I provided.

If this is not enough info just ask. I can get whatever you need. I have tried for some time to get it to work but I can only put them into an array. An array doesnt work because there could be 10, 20, 100 + posts, who knows?

I appreciate the help. Thanks.

correction: I should have written thread_id as id. The correction is in bold.

As you can see in the table structure the post_id has the same number as the thread_id's row 'id'. When a thread is created post_id is '0' and thread_id takes on a section number, that I can group together. When a post is created thread_id is '0' and Post_id takes on the row 'id' of the thread_id to match them up.

I want to count how many post_id's have a particular id, place it into a variable for a while statement and display them as replies/posts to a particular thread. As shown on the "location" document I provided.

If this is not enough info just ask. I can get whatever you need. I have tried for some time to get it to work but I can only put them into an array. An array doesnt work because there could be 10, 20, 100 + posts, who knows?

Also, here is the actual code for the placement of the variable:

# This section is the product of the above query.
echo"<table border='1' background='' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='80' align='center'><tr>";
echo"<td background='' bgcolor='' width='400' align='left'>";view_post();
echo"</td><td background='' bgcolor='' width='150' height='80' align='center'><font size='3' color='ffffff' face''><b>$author &nbsp;</b></font></td>";
echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$date<br>$time &nbsp;</font></td>";
echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$total &nbsp;</font></td>";
echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$views &nbsp;</font></td>";
echo"</tr></table>";
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
speed of comparing char arrays andy_t_roo C Programming Language 3 30-Jun-2005 19:12
Comparing strings in C Tanag C Programming Language 2 28-Nov-2004 19:07
Sorting columns by selection dean MySQL / PHP Forum 1 03-Oct-2003 07:29
Opinions Wanted: do 2 columns or 3 look best for this site layout? jps0 Websites Reviewed Forum 10 20-Aug-2003 16:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:11.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.