Page 1 of 1

trouble with looping

Posted: Wed Dec 23, 2009 5:49 am
by NikBruce
Hi, I'm building a cms. I have a page where you can view your account details including what "projects" you are dealing with on the cms, using your email address.

This is the code that pulls and displays the names of the projects you are dealing with. It also creates a link to projectPage.php, which uses the pulled project name to decide which project's info to display:

Code: Select all

 
$result = mysql_query("SELECT proName FROM projectList WHERE accEmail = '$accEmail'");
 
while($row = mysql_fetch_array($result))
{
$proName = $row['proName'];
echo "| <a href = projectPage.php><b>".$proName."</b></a><br />";
$_SESSION['proName'] = $proName;
}
 
The different project names are displayed perfectly. The problem is, that

Code: Select all

$proName
is always going to equal the last project name that the loop pulls, meaning that no matter which link I click on, projectPage.php will always display that last projects info.

What should I be doing instead? :banghead:

Thanks

Re: trouble with looping

Posted: Wed Dec 23, 2009 6:02 am
by requinix
Pass the "proName" to the other PHP script.

Code: Select all

 
$result = mysql_query("SELECT proName FROM projectList WHERE accEmail = '$accEmail'");
 
while($row = mysql_fetch_array($result))
{
$proName = $row['proName'];
echo '| <a href = "projectPage.php?proName='.htmlentities($proName).'"><b>'.$proName."</b></a><br />";
}
Then projectPage.php uses $_GET["proName"].

Remember to make sure that the user can access that project too!

Code: Select all

$proName = mysql_real_escape_string($_GET["proName"]);
$result = mysql_query("SELECT * FROM projectList WHERE accEmail = '$accEmail' AND proName = '$proName'");
 
if (mysql_num_rows($result) == 0) {
    // no such project, or the project is not tied to this user
} else {
    // ...
}

Re: trouble with looping

Posted: Wed Dec 23, 2009 7:15 am
by NikBruce
is that not what I've done, except I used sessions instead?

Re: trouble with looping

Posted: Wed Dec 23, 2009 7:24 am
by NikBruce
sorry, I should have checked it before i questioned it, It works! thanks alot.

If you don't mind, could you give a brief explanation as to the get method works in this situation while the session method does not?

Thanks again

Re: trouble with looping

Posted: Wed Dec 23, 2009 5:04 pm
by requinix
With sessions you were using only one value and that was decided before the user clicked a link. You basically said "I assume you're going to click this particular link", so it wouldn't behave when they clicked a different one.
With the URL you were still using one value, but that one value depended on the link.