Forum Moderators: coopster

Message Too Old, No Replies

Passing a $ from a previous page

         

Sapper

5:28 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



Hi im very new to this and just using a book for refference.

Ok here is the scene... I have created a database called horses with a table called Tracks also have a fieldname called Tracks which holds horse track names ie Aintree, Newbury etc etc.

I am trying to pass an individual trackname as a $ to the next page so that i don't have to make 59 seperate pages one to display each tracks details.

Essentially i have made a table which I can use as a template on another page and intened to just insert the following in the row of the table required.

<?php
/* This program places the info into a table */
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("Horses");
$sql = "select * from Tracks where Tracks ='$Tracks'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
printf("<left><font face=arial color=\"blue\"><font size=\"2\">%s</font></font>\n",
$row["Tracks"]);
}
?>

That code should work fine and i have already inserted the $Tracks in the connection line.

However my problem lies in passing the $Tracks from the previous page the idea is to have a list on the page with the 59 courses as links when you click on the link it sends you to the page above and passes the name of the link that you clicked on as $Tracks.

This is the current setup of the page but is setup just to open the seperate file instead of passing the $Tracks how can i change it so it passes on the $Tracks instead of openeing it's own page?

If this doesnt make any sense at all then I can see no easier way of explaining it...

<?php
/* This program lists Tracks on the database */
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("Horses");
$sql = "select * from Tracks";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print '<img src="gfx/redbullet.gif" />';
printf("<left><font face=arial color=\"#000066\"></font><font size=\"-4\"><a href=\" %scoursedata.php\">%s</a>\n",
$row["Tracks"], $row["Tracks"]);
}
?>

Any ideas would be apreciated

Thanks

henry0

5:51 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Saper,
Welcome to WebmasterWorld!

You mentioned
“Where tracks=$tracks
I assume that $tracks is passed via a form
If not how do you set a value for it?

If it passes through a form then
We will use a SESSION
On the VERY TOP of each concerned page add
<? session_start();
// get the Tracks value
$tracks=$_POST[‘tracks’];
// then we use the value now posted of tracks
// to create a SESSION
$_SESSION[‘tracks’]=$tracks
?>

USING IT
<? Session_start();
// to use $tracks on another page
$tracks=$_SESSION[‘tracks’];
// rest of script
?>

Sapper

6:19 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



Hi thanks for your swift reply....

No it's not being passed from a form which is the whole problem but surely if it can print the name to the screen then there must be some way of turning into $Tracks and passing it to the next page.

The following line picks the name from the database and adds it to coursedata.php to create the filename Aintreecoursedata.php as a link so why cant it pass the name Aintree onto the next page?

The template page being coursedata.php instead of having 1 page for each course like i am having to do at present.

printf("<left><font face=arial color=\"#000066\"></font><font size=\"-4\"><a href=\" %scoursedata.php\">%s</a>\n",
$row["Tracks"], $row["Tracks"]);

ergophobe

6:22 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



However it gets there, Henry0's solution should work, but instead of

$tracks=$_POST[‘tracks’];

just assign the $tracks variable straight to the session. It doesn't have to be post data. It can come from the DB or anywhere

Sapper

6:41 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



Hi thanks just tried it and it's still not working added echo ("$Tracks"); on the page and its not showing anything so its not passing any iafo still..

henry0

9:04 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try to echo $tracks in a few spots to follow if it passes or if stops somewhere

do something like
echo "line24.$tracks";
echo "line36.$tracks";

so you know what's ref to what
if it does not pass then it will read for example
line36."the rest will be empty"

Next: Instead of passing a var try to pass a direct value so you will know if there is a glitch in the script
$tracks="test";
and figure if the session works
by echoing that value to your next page.

also after session_start();
add
error_reporting (E_ALL);
and report what it reads

Sapper

9:46 am on Oct 18, 2005 (gmt 0)

10+ Year Member



Hi sorry for the delay with replying but i had to go to work.

Ok I am back now and will try again, I normally have more sucess with things after leaving them for a few hours....

Thanks for your help it is much appreciated...

Sapper

10:14 am on Oct 18, 2005 (gmt 0)

10+ Year Member



ok here is the error message im getting
Notice: Use of undefined constant ‘Tracks’

Sapper

11:12 am on Oct 18, 2005 (gmt 0)

10+ Year Member



Sorry copied same page this is the Aintreecoursedata.php code

<? Session_start();
error_reporting (E_ALL);
// to use $tracks on another page
$Tracks=$_SESSION["Tracks"];
// rest of script
?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Track Information</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<table width="680" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr>
<td colspan="3"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><strong>Track:
</strong></font><font size="2" face="Arial" color="#3333FF"><b><font size="2" face="Arial" color="#3333FF"><b><font face="Arial" size="3"><b><font color="#000000" size="4">
<?php
/* This program gets members info from the database */
$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("Horses");
$sql = "select * from Tracks WHERE Tracks = '$Tracks'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
printf("<left><font face=arial color=\"blue\"><font size=\"2\">%s</font></font>\n",
$row["Tracks"]);
}
?>

</font></b></font></b></font></b></font><font color="#000000" size="2" face="Arial, Helvetica, sans-serif"><strong>
</strong></font></td>
</tr>
<tr>
<td width="150" height="130" rowspan="4" valign="top"> <table width="150" height="130" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="150" height="130"><font size="2" face="Arial" color="#3333FF"><b><font size="2" face="Arial" color="#3333FF"><b><font face="Arial" size="3"><b><font color="#000000" size="4">
</font></b></font></b></font></b></font></td>
</tr>
</table></td>
<td width="150" height="130" rowspan="4" valign="top"><table width="150" height="130" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="20">
<div align="center"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Address:</strong></font></div></td>
</tr>
<tr>
<td width="150" height="110"><font size="2" face="Arial" color="#3333FF"><b><font size="2" face="Arial" color="#3333FF"><b><font face="Arial" size="3"><b><font color="#000000" size="4">
</font></b></font></b></font></b></font></td>
</tr>
</table></td>
<td height="69" valign="top">&nbsp;</td>
</tr>
<tr>
<td height="20" valign="bottom"><strong><font size="2" face="Arial, Helvetica, sans-serif">Telephone:</font><font size="2" face="Arial" color="#3333FF"><b><font size="2" face="Arial" color="#3333FF"><b><font face="Arial" size="3"><b><font color="#000000" size="4">
</font></b></font></b></font></b></font></strong></td>
</tr>
<tr>
<td height="20" valign="bottom"><strong><font size="2" face="Arial, Helvetica, sans-serif">Email:</font><font size="2" face="Arial" color="#3333FF"><b><font size="2" face="Arial" color="#3333FF"><b><font face="Arial" size="3"><b><font color="#000000" size="4">
</font></b></font></b></font></b></font></strong></td>
</tr>
<tr>
<td height="21" valign="bottom"><strong><font size="2" face="Arial, Helvetica, sans-serif">Website:</font><font size="2" face="Arial" color="#3333FF"><b><font size="2" face="Arial" color="#3333FF"><b><font face="Arial" size="3"><b><font color="#000000" size="4">
</font></b></font></b></font></b></font></strong></td>
</tr>
</table>
</body>
</html>

henry0

11:26 am on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<? Session_start();
error_reporting (E_ALL);
// to use $tracks on another page
$Tracks=$_SESSION["Tracks"];

replace ["tracks"] with ['tracks']
and avoid capital letters

also do this
$tracks=$_SESSION['tracks'];
echo"VerifySession.$tracks";

Sapper

11:46 am on Oct 18, 2005 (gmt 0)

10+ Year Member



It still doesn't work and I have to use a capital T as the fieldname started with one unless you want me to change it in the database?

henry0

11:51 am on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sapper
so the $tracks session does not pass when you echo it as per my last post suggestion

OK
you sre missing a " ; "
here:$Tracks=$_POST[‘Tracks’];
// then we use the value now posted of tracks
// to create a SESSION
$_SESSION[‘Tracks’]=$Tracks
it should read

$Tracks=$_POST[‘Tracks’];
// then we use the value now posted of tracks
// to create a SESSION
$_SESSION[‘Tracks’]=$Tracks;

Sapper

11:56 am on Oct 18, 2005 (gmt 0)

10+ Year Member



Ok cant cahnge the table name from a capital T but ive changed the fieldname to tracks.

here is the following error message that im still getting....

Notice: Undefined index: tracks in /horses/Aintreecoursedata.php on line 4

Notice: Undefined index: tracks in /horses/Aintreecoursedata.php on line 5
VerifySession.

Sapper

12:04 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Sorry had to change the database field back to a T too many other files are using it like that..

Anyway here is the error message im now getting..

and no it's not passing anything as your Verify Session is coming back blank...

Notice: Undefined index: Tracks in /home/jshyeuwt/public_html/horses/Aintreecoursedata.php on line 4
VerifySession.

henry0

12:26 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<? session_start();
// get the Tracks value
$Tracks=$_POST[‘Tracks’];
// then we use the value now posted of tracks
// to create a SESSION
$_SESSION[‘Tracks’]=$Tracks

try this from the very first page

<? session_start();
// get the Tracks value
$Tracks=11111;
// then we use the value now posted of tracks
// to create a SESSION
$_SESSION[‘Tracks’]=$Tracks;
// we set a value to tracks.

second page

<? Session_start();
//error_reporting (E_ALL);
// to use $tracks on another page
$Tracks=$_SESSION["Tracks"];
echo"TestSession.$tracks";
// should read: TestSession1111
// rest of script

Note we have commented error reporting

Sapper

12:30 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



If I can't get it working then the whole thing is doomed it's ok to make 59 seperate track pages but i'm not making 5000 horse ones lol...

Sapper

12:38 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Yes thats passing 11111 to the page

henry0

1:12 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well at least we know that the value passes through
now re-establish the regular session and do this

we are testing your db with 2 values id and horses
if no id and horses exist then change by whatever is in your table, remember to do the changes in the 2 locations that call for those field names

mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("Horses");
$sql = "select id, horses from Tracks WHERE Tracks = '$Tracks'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
printf("id: %s horses: %s", $row[0], $row[1]);

}

what is the HTML for?
each td seems to be empty?

Sapper

1:51 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Nope its not passing anything still......

The html is so that i can insert the php code into each part of the table seperately.

By just getting one to work copy and pasting it then changing the field name that it refers too......

There must be a simple way of doing this the session we are trying is not working as it has no information to pass unless we manually give it one.

Sapper

1:59 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Ok Im off out for an hour....

Will enter hour 16 of trying to do this when i return lol

Thanks for your persistance it will pay off in the end....

henry0

2:24 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Last thing I can think of:
Post how you supply the $tracks value to your first script

Sapper

2:52 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



This is where the problem lies...

It is getting the info from the database then displaying it and creating a link to %scoursedata.php which i made to originally go to a page corresponding to the track name....

This line can be changed as i no longer wish to do it like that i want all output for all tracks going to a page called coursedata which then will just need the $Tracks info passed to display the relevant info.

But essentially it has already created a variable as it already puts it in the filename to create the link so we just need to capture that and send it to the next page.

But until you click on the link then it wont know which track you want so it can't pass the info on.....

printf("<left><font face=arial color=\"#000066\"></font><font size=\"-2\"><a href=\" %scoursedata.php\">%s</a>\n",
$row["Tracks"], $row["Tracks"]);

henry0

4:04 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that I start to understand what's wrong
Your user clicks on a link which link is ONLY A LINK, as a direct result no value is generated.

First create a form to select a track
then pass that value to the query that builds the link then pass it to the next page.

<edit> added modif </edit>

Sapper

5:16 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Yes i think i will give up on doing it for the Tracks I will just make individual pages as for the 5000 horses i will use a search form for them.

Just thought there may be a way around it but unfortunately it doesnt look like there is...

Thanks for your help anyway....

ergophobe

8:08 pm on Oct 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sapper, The main thing is that I don't see anywhere in any code that you have posted showing where you assign values to the $_SESSION. Have you integrated the code from Henry0's first post?


echo "line24.$tracks";

Little tip. This will change when you edit, so it's easier to use

echo "Tracks = $tracks (" . __FILE__ . " :: " . __LINE__ . ")";


Notice: Use of undefined constant ‘Tracks’

That typically means you left out a $ before a variable name or you have an unquoted array index as in $row[Tracks]. I didn't see either in your code though.

Similarly for undefined index.