Jump to content

Displaying Records that link to row data


nclocl

Recommended Posts

I have a mysql table QUESTIONS with rows CREATED_BY, QUESTION and SUBJECT.

 

I have pages on a website that insert rows into this table depending on user input but now I would like to know how to display it.

 

I need a page with a list of questions (SELECT QUESTION FROM QUESTIONS) but they need to be hyperlinks that open a new page which displays that question and a text box to write an answer.

 

So my question is: How do I dynamically display the QUESTION records as hyperlinks?

 

When this is clicked I presume I can just post the question name and then on the next page, generate data based on this value from my table.

For reference, this is what I tried:

 

<?
                            session_start();
                            if (!session_is_registered(email)) {
                                header("location:login.html");
                            }



                            $con = mysql_connect("blah", "blah", blah");

                            if (!$con) {

                                die('Connection Failed' . mysql_error());
                            }

                            mysql_select_db("blah", $con);

                            $result = mysql_query("SELECT * FROM QUESTIONS")
or die(mysql_error());

while($essay_data=mysql_fetch_array($result)){

$question = $essay_data['QUESTION'];

echo "<a href=\"englishessays.php?id=$question>";
echo "$QUESTION </a>";
echo "<br><br>";
}

                            ?>

 

I should probably add an ID number for each question I guess.  It doesn't work anyway.

Just quickly, what's wrong with this?

 

while($essay_data=mysql_fetch_array($result)){

$question = $essay_data['QUESTION'];
$id = $essay_data['ID'];

echo "<a href=\"englishessays.php?id=$id">";
echo "$question </a>";
echo "<br><br>";
}}

 

It seems to be enclosing the 'echo' commands in the quotes.

 

The way I did it before worked but was adding </a>";

echo "<br><br>" as text onto the link

agh sorry for the triple post.  Here's my code:

 

$result = mysql_query("SELECT * FROM ESSAY_QUESTIONS WHERE SUBJECT = 'ENGLISH'")
or die(mysql_error());

while($essay_data=mysql_fetch_array($result)){

$question = $essay_data['QUESTION'];
$id = $essay_data['ID'];

echo "<a href=\"englishessays.php?id=$id>";
echo "$question </a>";
echo "<br><br>";
}

 

It seems to display the first question as a link but the link itself looks like it is the rest of the data in the results.  How do I echo a link for each row rather than one link with everything on it?

Watch your quotes, you you havn't ended the href:

echo "<a href=\"englishessays.php?id=$id>";

should read

echo "<a href=\"englishessays.php?id=$id\">";

also, you don't need to run as many echo's, 1 would do it:

echo "<a href=\"englishessays.php?id=$id\">$question</a><br><br>";

Archived

This topic is now archived and is closed to further replies.

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