Jump to content

Can i link an echoed row from a table to another row in another table?


Recommended Posts

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

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

 

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

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

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

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?

 

 

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...

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']}";
}

 

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']}";
}

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 :shy:

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.

 

 

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?

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

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

 

??

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

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!

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.