1

I am trying to return a $user_id variable from file to if(isset(...)) statement. I will perform a different if statement but just trying to echo out the $user_id variable to test that I am setting the page.

<?php 
include 'core/init.php';
include 'init.image.php';
protect_page();
include 'includes/overall/overall_header.php';

if(isset($_GET['username']) === true && empty($_GET['username']) === false){
$username       = $_GET['username'];


if(user_exists($username) === true){
$user_id        = user_id_from_username($username);
$profile_data   = user_data($user_id, 'first_name','last_name','email', 'username');
?>

    <h1><?php echo $profile_data['first_name']; ?>'s Yor Page</h1>

<div id="navWrapper">
    <ul>
        <li>
            <a href="#"><img src="uploads/profile/blank_profile.gif" width="150" height="150" id="blank_profile"></a>
        </li>

        <nav>
            <ul>
                <li>
                    <a href="<?php echo $profile_data['username'];?>?action=albums">Albums</a>
                </li>
                <li>
                    <a href="<?php echo $profile_data['username'];?>?action=music">Music</a>
                </li>
            </ul>
        </nav>
    </ul>
</div>

<?php
if(isset($_GET['action']) && $_GET['action']=='albums'){

$albums = get_profile_albums($user_id);

if(empty($albums)){
echo 'No Albums';
}else{

foreach($albums as $album){
if (empty($album['image'])) {
    $album['image'] = 'uploads/profile/blank_profile.gif';
}
?>
 <p><?php echo $album['name'],' (', $album['count'], ')'?> <br />
  <a href="<?php echo $profile_data['username'];?>?action=album_id=<?php echo $album['id'];?>">
    <img src="uploads/thumbs/<?php echo $album['id'];?>/<?php echo $album['image'];?>" />
  </a><br />
  <?php echo $album['description'];?>...<br />
</p>
<?php
}
}

}
if(isset($_GET['action']) && $_GET['action']=='album_id=$album['id']'){
echo $user_id;
}



if(isset($_GET['action']) && $_GET['action']=='music'){
echo'<h1>Music</h1>';
}

}else{
    echo 'Sorry, that user doesn\'t exist';
}
}else{
header('Location: index.php');
exit();
}

include 'includes/overall/overall_footer.php';
?>
5
  • 2
    There's no need to add === boolean. Commented Aug 2, 2012 at 0:12
  • You're doing it right... what is the problem/error you are getting? Commented Aug 2, 2012 at 0:13
  • I am not sure what you are asking? I did notice this though: isset($_GET['username']) === true - as isset returns a boolean you don't need to use a === and if you want to shorten your statement, you could write it as if(isset($_GET['username'])) which will only evaluate if returns a true anyhow. Commented Aug 2, 2012 at 0:15
  • 6
    There is no need to call both isset() and empty() on the same variable. empty() implicitly calls isset() so simply !empty() will suffice. Commented Aug 2, 2012 at 0:15
  • Ok i removed the true and still same results which is no results echoed, but if I move that variable outside the if statement and echo it, it displays the variable. Commented Aug 2, 2012 at 0:37

2 Answers 2

3

Single quotes do not parse the string but double quotes do. For example:

$album_id = 1;
echo 'album_id=$album_id ';
echo "album_id=$album_id";

Will result in album_id=$album_id album_id=1

Thus, $_GET['action']=='album_id=$album_id' is checking if $_GET['action'] is equal to 'album_id=$album_id' and not what $album_id is evaluated to.

Your check should be more along these lines:

if (/* ... */ && $_GET['action']=="album_id=$album_id") {

Edit: Suggestion.

You should check $_GET['action'] once and store $action. From there create an if/else if/else statement checking each possible type of action (music, albums, album, etc). If a type of action requires extra user supplied data, include that in the respective if block. For example:

 $action = $_GET['action'];
 // URL: /script.php?action=albums
 if ($action == 'albums') {
     // ...
 }
 // URL: /script.php?action=album&album_id=12
 else if ($action == 'album') {
     if (!empty($_GET['album_id'])) {
          $album_id = $_GET['album_id'];
          echo "User with id $user_id is trying to access album with id $album_id";
     }
     // ...
 }
 // ...

No more $_GET['action']=="album_id=$album_id"

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure what you mean but I need to get the action of the link being clicked, if you look up further in my code where the link this isset is referring to is I am passing a function in the link so how do I check with $_GET action to return that link dynamicly>
I made an edit with a suggestion. Instead of having action=album_id=121 you check if action is album and then you get the album_id. I hope that is what you were getting at.
No that is not working I edited my original code the variable is $album['id'] not $album_id.
0

You are passing the variable correctly.

What I'm more concerned with in the following line is:

if(isset($_GET['action']) && $_GET['action']=='album_id=$album_id'){

'album_id=$album_id' doesnt look right and is probably why your if is not firing... It looks like you're trying to use the variable $album_id wrapped in single quotes and that will not substitute the value and rather keep $album_id in tact. I'm not sure if this is what you intended but if not its likely the source of your problem.

9 Comments

ok I thought maybe as well, how do I pass that variable $album_id then?
I mean how do I concat $album_id?
i like to wrap all my vars inside strings with the {} because its more official and it looks prettier in my text editor, but you dont need them...
I apologize I posted wrong variable for $album_id it is suppose to be $album['id'].
Anything you are the closest to figuring this out for me?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.