kleb Posted December 16, 2011 Share Posted December 16, 2011 I am new to php.i want to be able to link echoed out rows from a table to another row in another table so that when users clicked the first row i echoed out it will take them to that SPECIFIC row i linked it to in the second table..JUST LIKE FACEBOOK...Please do i make sense or is there another way to do it. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/ Share on other sites More sharing options...
Deoctor Posted December 16, 2011 Share Posted December 16, 2011 Could not able to understand what you are exactly saying. I could not able to figure out the relation between your question and the facebook. Can you elaborate Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298488 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 Ok. This is what i mean: i want users to post Essays on my site just like a forum. what i did was i stored the topics of those Essays in a table i named TOPICS and the body of the Essay to another table i called CONTENT, i have selected all the topics and echoed it out on a page so that intrested users can click on them and read the whole Essay but my problem is how i can link those TOPICS to their specific CONTENTS. THanks Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298496 Share on other sites More sharing options...
Drongo_III Posted December 16, 2011 Share Posted December 16, 2011 The two tables should share a key. So the ID for the topics should match the ID for the Contents. Do you want the user to click through to a new page to view the topic's content? Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298497 Share on other sites More sharing options...
Deoctor Posted December 16, 2011 Share Posted December 16, 2011 so as drong told if you have a link between these two tables then pass the value of the id of the topic as the input to the content page to show the content by using the $_GET['string'] the string could be any thing. Ok. This is what i mean: i want users to post Essays on my site just like a forum. what i did was i stored the topics of those Essays in a table i named TOPICS and the body of the Essay to another table i called CONTENT, i have selected all the topics and echoed it out on a page so that intrested users can click on them and read the whole Essay but my problem is how i can link those TOPICS to their specific CONTENTS. THanks Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298499 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 Please can you help me by giving me the codes am still confused Chaitu and Drong Thanks Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298502 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 I have a problem with this codes, i dont want when users click on a topic the whole body or content in the joined table to be echoed.i only want a topic for a content since they were both created once by the user. This are my codes: index.php <tr class="row-a"> <td class="first">05.31.2006</td> <td> <?php include"header.php"; $sql="SELECT *FROM topics "; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)){ echo"<a href='topic_content.php?'id='postID'>{$row['topics_subject']}</a>"."</br>"; } ?> </td> topics_content.php <?php include"header.php"; $sql="SELECT* FROM post JOIN topics ON postID=topics.topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo "{$row['post_content']}"; } ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298547 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 I have a problem with this codes, i dont want when users click on a topic the whole body or content in the joined table to be echoed.i only want a topic for a content since they were both created once by the user. This are my codes: index.php <tr class="row-a"> <td class="first">05.31.2006</td> <td> <?php include"header.php"; $sql="SELECT *FROM topics "; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)){ echo"<a href='topic_content.php?'id='postID'>{$row['topics_subject']}</a>"."</br>"; } ?> </td> topics_content.php <?php include"header.php"; $sql="SELECT* FROM post JOIN topics ON postID=topics.topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo "{$row['post_content']}"; } ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298548 Share on other sites More sharing options...
Drongo_III Posted December 16, 2011 Share Posted December 16, 2011 Hi Kleb Since you're echoing the topics links with a query string you could do something like this in you contents.php to pull that ID. $contentID = $_GET['postID']; //access GET array to get the query string then use this to query the Contents table Then use that $contentID to search for the specific content record in the database. If you have two separate tables for the Topic and Content then there should be a key (like an ID value) that is matched in both cases. So if ID 1 is Animals - in your Topics table Then ID 1 will be Content on Animals - in the Content table Do you get that? Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298553 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 i dont get you Drongo please throw more light am Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298556 Share on other sites More sharing options...
Drongo_III Posted December 16, 2011 Share Posted December 16, 2011 Ok. so you have two tables. Table 1 contains rows with - ID and Topic Name Table 2 contains rows with - ID and Content - the ID here should match with it's counterpart in the Topic table. Your topics page generates a list of links all of which have a query string (i.e. ?id=postID). The user clicks that link and gets directed to the contents.php page. Because the links have the query string you pull that from the GET array - i.e. $id = $_GET('id') You now know the topic the person has clicked on and you have the ID of the topic which should match the ID of the Content tables -therefore you know the content you want. $result = mysql_query("SELECT * FROM Contents WHERE id=$id") while($row = mysql_fetch_array($result)) { echo $row['Contents'] } Somethng like that... This is assuming that you have two tables which is what I thought was the case from the start... Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298560 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 help it gave me an error Notice: Undefined index: postID in C:\wamp\www\topic_content.php on line 3 <?php include"header.php"; $id=$_GET['postID']; $sql="SELECT* FROM post JOIN topics ON postID=topics.topicsID WHERE topicid='$id'"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo "{$row['post_content']}"; } Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298575 Share on other sites More sharing options...
Drongo_III Posted December 16, 2011 Share Posted December 16, 2011 sorry change postID to just ID. So $id=$_GET['id']; Why are you still trying to join the tables? help it gave me an error Notice: Undefined index: postID in C:\wamp\www\topic_content.php on line 3 <?php include"header.php"; $id=$_GET['postID']; $sql="SELECT* FROM post JOIN topics ON postID=topics.topicsID WHERE topicid='$id'"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo "{$row['post_content']}"; } Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298589 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 I thought i need to join the tables for it to work Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298596 Share on other sites More sharing options...
Drongo_III Posted December 16, 2011 Share Posted December 16, 2011 hehe sorry i might be confusing you becuse i am assuming you have two tables. Try it your way and see how it goes Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298599 Share on other sites More sharing options...
kleb Posted December 16, 2011 Author Share Posted December 16, 2011 when i changed the id it said that Id column doesnot exist so i changed it back to POSTID and it didnt show the error but its still telling me that $id is undefined..what if i had one table only and i put both the TOPICS and the CONTENT in one table?? or can you help me write the code in your own way thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298612 Share on other sites More sharing options...
Drongo_III Posted December 16, 2011 Share Posted December 16, 2011 Well if you just have three columns in your table and you're not planning on extending this too much you could put it all in one and have ID, Topic and Content. Then you'd just have to do one single sql query and you won't have to worry about joining the tables etc. It depends how much you're planning to scale this really. Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1298633 Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 but will i be able to link the a TOPIC from the TOPIC column to the CONTENT in the CONTENT column? Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1299263 Share on other sites More sharing options...
Drongo_III Posted December 19, 2011 Share Posted December 19, 2011 Well they'd all be in the same row so yes. So you'd have ID Topic Content Keep in mind this is an extremely simplistic example and you may benefit from having multiple tables and structuring it more clevely if you plan to scale this to hundreds of recrods. But if you just have a handful and it's finite then it should be ok to do this. but will i be able to link the a TOPIC from the TOPIC column to the CONTENT in the CONTENT column? Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1299310 Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 Please dont get bored with the way am bordering you..now if i want to do the link up so that the userID will be tied to the TopicID and the contentID and when i echo lets say all the topics,when a visitor clicks on any of the topics it will take him/her to the exact content linked to it..just like when i click on a topic on PHP FREAKS thanks Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1299324 Share on other sites More sharing options...
Drongo_III Posted December 19, 2011 Share Posted December 19, 2011 Hi Kleb. No worries. How does the user ID come into this? Do you want to echo out some info about the user who created the topic? If so does that mean you have two tables: - One for users -One for topic and content ?? Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1299352 Share on other sites More sharing options...
kleb Posted December 19, 2011 Author Share Posted December 19, 2011 sorry i am not echoing out the userID am just trying to say that in order for me to echo out the topic posted by a specific user..Yes, i hv two tables. one for the USERS and another that contains the TOPICS and the CONTENT Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1299363 Share on other sites More sharing options...
Drongo_III Posted December 20, 2011 Share Posted December 20, 2011 Hi Kleb Your tables should be setup so that both the USERS table and the CONTENT table share a common id. So in your USERs table 'Bill' might be ID 1. Bill has created a topic and content that is stored in table 2. So this would have three columns like so: id 1, Topic, Content Then you run a statement along the lines of the following (which is a very simplistic example). In the example 'Content' refers to your content table and Users refers to your users table. The query looks for a common id in both tables and joines the data when an id match is found so it's all accessible as one row. $result = mysql_query("SELECT * FROM Content, Users WHERE (content.id= user.id) "); while($row = mysql_fetch_array($result)) { echo $row['id'] . " " . $row['name'] . " " . $row['topic'] . " " . $row['content']; echo "<br />"; } Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/253295-can-i-link-an-echoed-row-from-a-table-to-another-row-in-another-table/#findComment-1299658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.