Forum Moderators: coopster

Message Too Old, No Replies

Embed PHP variable in link

How can I get a variable included in a link?

         

s9901470

9:18 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



Hi

In the code below, I'd like an existing variable called id to apear instead of XXXXX, but I can't seem to find the right way of doing it.

if ( $answer == $dbanswer){echo '<P>The answer you provided was correct, so you can continue authorized. Please <a href="page2.php?id=XXXXX">CLICK HERE<A> to continue.</P>';}

If I put id=$id, the link followed just says $id at the end rather than pasting the variable's value itself.

Any suggestions?

proper_bo

9:21 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



if ( $answer == $dbanswer)
{
echo ("<P>The answer you provided was correct, so you can continue authorized. Please <a href=\"page2.php?id=" . $id . "\">CLICK HERE<A> to continue.</P>");
}

You will see I have changed to " instead of '. I am just used to it that way.

s9901470

9:48 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



That works great! Many thanks.

Now I have another problem. On the next page, I want to generate a list of questions specific to that id. In the code at the bottom, I expected to be able to add 'AND' but I get an error message

This doesn't work:

for($n = 0; $n <= $entries; $n++) {
$sql = "SELECT * FROM roam WHERE title = $n AND id= $id ";

This works:

for($n = 0; $n <= $entries; $n++) {
$sql = "SELECT * FROM roam WHERE title = $n";

Any suggestions?

jatar_k

10:25 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't know what the error message is but a shot in the dark is to add quotes around the value of title

$sql = "SELECT * FROM roam WHERE title='$n' AND id=$id ";

s9901470

6:32 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



That gives me a blank screen so there might be something wrong. Here is some more code, in case the problem is elsewhere. I want to make a list of all the questions in a quiz from the database, but only those specific to $id. Any suggestions?

for($n = 0; $n <= $entries; $n++) {
$sql = "SELECT * FROM quiz WHERE title = $n";
$result = mysql_query($sql);
while ($info = mysql_fetch_array($result)) {
foreach( $info AS $key => $val ) {
$$key = stripslashes( $val );
}
$options .= "<option value=\"$questions\">$questions</option>";

}
}
return $options;
}

jshpro2

2:18 am on Feb 24, 2005 (gmt 0)

10+ Year Member



Instead of using double quotes you could always have done:
echo ('link='.$var.'&whatever.....');
As you can see the . combines the strings.