PHP conditionals/variables failing.
Posted: 14 June 2005 05:47 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  77
Joined  05-14-2004

I’m trying to put little “EDIT” links by posts.  I want them there if either 1) The post’s author is logged in, or 2) The user is an admin.

I have to use PHP to do this, and because of EE’s flaky handling of {member_id} and {group_id}, I have to futz about with PHP variables.

So check out my code:

{if logged_in}

   <?php
     $member
= "{member_id}";
     
$group = "{group_id}";
   
?>

{
/if}


{exp
:weblog:entries weblog="blognomic" limit="30" status="not draft"}

// Stuff, stuff, stuff

{if logged_in}

     <?php

        $author
= "{author_id}";

        echo
$member;  // debug
        
echo $author;  // debug
        
echo $group;  // debug

        // -------------------------------------------
        // IMPORTANT CONDITIONAL FOLLOWS
        // -------------------------------------------

         
if (($member == $author) || ($group == '1') || ($group == '6')) {

             
echo 'The edit link goes here.'

         
}

    ?>

{
/if}

A synopsis:  I set $member and $group BEFORE the {exp:weblog:entries}.  So these values pertain to the logged in user, not the weblog entries.

The lines marked //debug just output those variables’ values; at this point, the variables work PROPERLY.  No problems.

The IMPORTANT CONDITIONAL is where things screw up.  I can’t seem to compare any of those variables against anything.

This works:

if ($member) {
echo $member
}

This also works:

if(1==1) {
echo $member
}

Those both work fine.  It’s when I try to compare one of those variables that it fails, and it does so by not returning anything whatsoever.  Even this:

echo $member;  // outputs 1

if ($member == 1) {   // FAILS.  Outputs nothing.
echo $member;
}

Doesn’t work at all.

Can anyone tell me what’s going on here?

SFT

Profile
 
 
Posted: 14 June 2005 06:45 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12395
Joined  04-29-2002

I can replicate the problem. Doesn’t matter if parsing is set to input or output.

Part of the problem I can see if I do the following:

<?
      $member
= "{member_id}";
      
$mtest = "1";

           
$zz = strlen($member);
           echo
"Member Length: $zz<br />\n";
           
$yy = strlen($mtest);
           echo
"MTest: $yy<br />\n";
?>

The results come back as:


Member Length: 11
MTest: 1

So that’s why comparing a string of 11 characters to 1 character isn’t working.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 14 June 2005 07:39 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  77
Joined  05-14-2004

Uhh.  But what happens if you just go

echo $member;

?  Does it output the number you expect?  Or just the string {member_id}?  Because in my case, it outputs the number I expect.

Profile
 
 
Posted: 14 June 2005 08:03 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12395
Joined  04-29-2002

Uhh.  But what happens if you just go

echo $member;

?  Does it output the number you expect?  Or just the string {member_id}?  Because in my case, it outputs the number I expect.

It looks like it spits out 1. But it’s not really the case.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 14 June 2005 09:06 AM   [ Ignore ]   [ # 4 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  32895
Joined  05-14-2004

As I understand it, you can use the stand alone edit form plugin to do this - it has a builtin tag, or so I was told.  That may be your best bet, even if not using the form itself. =)

 Signature 
Profile
MSG
 
 
Posted: 14 June 2005 09:10 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  77
Joined  05-14-2004

Guh.  I don’t understand that, but I know whatever it is, it makes me hate computers.

And I mean, if I log in as a different user in a different group, all the numbers change accordingly!  So how can it somehow be storing both the “{member_id}” string and the proper value?  It’s not an array…!?

Very very confused,
SFT <.<

Profile
 
 
Posted: 14 June 2005 09:14 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  77
Joined  05-14-2004

LisaJill,

Ah thanks, I’ll investigate that.  : )

SFT

Profile
 
 
Posted: 14 June 2005 09:24 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  77
Joined  05-14-2004

After quick investigation,

I might be missing something, but I’m not sure that addresses my problem.  I don’t see anything in the documentation that indicates it handles links to the edit form at all.  And the problem is regulating who sees the links, not forming them.

But I AM just coming off an all-nighter so I’m very probably dense.  <.<

SFT

Profile
 
 
Posted: 14 June 2005 09:41 AM   [ Ignore ]   [ # 8 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23536
Joined  05-20-2002

I recall a thread dealing with this (I think)- but can’t find it to save my life.  I think there was a trick with member_id using isset to make it explicitly an integer (or the other way around).  Might give that a try.  Wish I could find the blasted thread….

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 14 June 2005 09:50 AM   [ Ignore ]   [ # 9 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  9868
Joined  06-19-2002

Instead of trying to set PHP variables using EE variable values, try just going straight PHP.  So, something like this:

<?php

  $member
= $SESS->userdata['group_id'];
  
$group = $SESS->userdata['group_id'];

  if ((
$member == {author_id}) || ($group == 1) || ($group == 6)) {

    
echo 'The edit link goes here.';

  
}

?>

 Signature 

Chris Curtis
chriscurtis.org

Profile
 
 
Posted: 14 June 2005 10:04 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  77
Joined  05-14-2004

Chris,

Beautiful.  (For archaeologists stumbling across this thread, it needed a ‘global $SESS;’ line wink  )

Thanks a bunch!  : )

SFT

Profile
 
 
Posted: 14 June 2005 10:11 AM   [ Ignore ]   [ # 11 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23536
Joined  05-20-2002

Chris already got you squared away, but for my own sanity, here’s the post on issues with member_id.  The workaround was:

$id = “{member_id}”;
$id = settype($id,“int”);

via MS.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 14 June 2005 10:51 AM   [ Ignore ]   [ # 12 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12395
Joined  04-29-2002

EEWiki: Using Session Variables Via PHP

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64978 Total Logged-in Users: 22
Total Topics: 82016 Total Anonymous Users: 21
Total Replies: 440814 Total Guests: 189
Total Posts: 522830    
Members ( View Memberlist )