trouble with looping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

trouble with looping

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: trouble with looping

Post 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 {
    // ...
}
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: trouble with looping

Post by NikBruce »

is that not what I've done, except I used sessions instead?
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: trouble with looping

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: trouble with looping

Post 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.
Post Reply