Forum Moderators: coopster

Message Too Old, No Replies

Using variables between pages

newbie help

         

johnglass

9:50 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



Hi,

I'm trying to request a field from a MySQL database, then use that as part of the link, and then use the variable from that page for the linked one. Confused? Me too :)...

Specifically, I want to do something like this:

while ($row = mysql_fetch_assoc($arresult))
{
$data1 = $row["archive_title"];
$data2 = $row["page_name"];
echo?><?="$data2"?> - <a href='arview.php'><?=$data1?></a><br><?php;
}

Thus, it searches the database, returning each row and echoing it, till it runs out (in this case, its searching the database, returning the info, and using the info to create a link).

All my pages basically use the same content window (an included file), and the content itself is decided by a variable included in page. So I need the "destination" page (arview.php) to include the same $data2 variable. The arview.php file has this line:

$arviewresult = mysql_query("SELECT * FROM $datatable1 WHERE page_name=$data2", $dbconn)
or die ("Unable to run query");

Obviously in its present form, this doesn't work, because $data2 isn't defined anymore.

There's prolly several ways to do this.. I'm thinking one might be including the variable in the page name somehow... another might be to use the $post or $global function (both of which I don't really understand yet, and I'm not sure if they are applicable to this case).

Sorry if that's not too clear, let me know if I need to elaborate on something.

Thanx :)

johnglass

killswitch

11:23 pm on Apr 5, 2004 (gmt 0)

10+ Year Member



Okay probably the easiest way to do this will be using GET to grab the variable from the url string.

The way you will want to formulate the link something like this:

echo "$data2 - <a href='arview.php?var=".$data1."'>link text</a>";

Note the bolded var element in the string. You can use whatever you want here, it will be what is used on the arview.php page to access the value.

[arview.php]

$data2 = $_GET['var']; // Use the same var name here to grab the information you need from the url string.

I think you will need to encase your variables in the mysql query with single quotes for the query to process. You may want to do some error checking on the GET variable since users can modify it themselves. Another option would be to use $_SESSION varibles assuming the user has cookies enabled.

johnglass

2:10 am on Apr 6, 2004 (gmt 0)

10+ Year Member



Awesome, thank you so much, took me a while to implement and test it, but it seems to work perfectly :)