Forum Moderators: coopster

Message Too Old, No Replies

OOP Programming Help

php, OOP

         

rodriguez1804

3:20 am on Apr 21, 2009 (gmt 0)

10+ Year Member



Hey all,

I am having trouble inserting an if function into the following code.

Here's the details: I created a class that has functions to generate the head, content and footer of the webpage. I then instantiate on another page, call the function, and then insert any further content I want added to the page. I am having trouble inserting an if statemtent though, can anybody tell me what I am doing wrong:


//This function allows me to add content to the body of the html page.
$webPage->addContent('
<div id="mainContentComments">
<div id="authorContainer">
<div id="authorPic">'.$myProfilePic2.'</div>
<div id="authorComment" align="left">
<div id="authorStats" class="smallTxt">'.$authorUsername.' - '.$postDate.'</div>
'.$authorComment.'</div>
</div>
</div>

'.$dbConn->retrieveUserReplies($postID).'
'.$dbConn->updateCounter($postID,$counter).'
'.$dbConn->registerPageURL().'

//THIS IS WHERE I WANT TO INSERT AN IF STATEMENT TO ONLY ALLOW REGISTERED USERS TO POST COMMENTS. AS IT IS, ANYBODY CAN GET TO THIS PAGE AND POST COMMENTS IF THEY KNOW THE URL.
//'.if($dbConn->isSessionSet() == (false)){do something!}.'

//PROBLEM IS I GET AN UNEXPECTED T_SYNTAX IF ERROR, IT SEEMS IT DOESN'T LIKE IF'S OR EVEN A SIMPLE ECHO HERE. WHAT'S GOING ON?

<div id="postArea">
<h3>Reply</h3>
<table>
<tr>
<form name="userInfo" method="post" action="replyToUserPost.php">
<td><textarea name="comment" cols="30" rows="10"/></textarea></td></tr>
<td><input type="hidden" name="date" value='.date("y-m-j,H:i:s a").' />
<td><input type="hidden" name="postID" value='.$postID.' /></td>
<tr><td><input class="submitBtn" type="submit" value="Reply" name="Reply"/>
</form></td></tr>
</table>
</div>

'); //END OF FUNCTION

//OUTSIDE OF THIS FUNCTION, IF'S, ECHO'S, OR ANYTHING ELSE WORK JUST FINE, BUT IT'S ONLY WHEN I TRY TO USE THE STATEMENTS WITHIN THE FUNCTION THAT IT GIVES ME AN ERROR?

lammert

2:21 pm on Apr 21, 2009 (gmt 0)

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



The parameter to the function expects a string, or a concatenation of strings and expressions which return a string. You could try the following:

Replace:
'.if($dbConn->isSessionSet() == (false)){do something!}.'

With:
'.($dbConn->isSessionSet() == (false)) ? do_something() : "".'

Then you create a function named do_something() which performs the code you wanted to execute at {do something!} and returns its result as a string, or an empty string if no result has to be inserted in the call to the overall function.

rodriguez1804

3:17 am on Apr 24, 2009 (gmt 0)

10+ Year Member



Thanks for the help lammert; However, I am having a heck of a time trying to get the conditional expression to evaluate correctly

Here's my isSessionSet function

function isSessionSet()
{
if(!isset($_SESSION['username'])){
return false;
}else{
return true;
}
}

then I do as you suggested, like so:


'.($dbConn->isSessionSet() == (true) ? "FALSE" : "TRUE" .'

Regardless of whether I am logged in or out, this always evaluates to either false or true, but not to the corresponding value. I know the isSessionSet function works correctly, so I seem to be screwing up somewhere in the conditional statement? Any suggestions, cause I 've beat my head on this for a couple of hours now ::::::Anybody?

rodriguez1804

4:14 am on Apr 24, 2009 (gmt 0)

10+ Year Member



man oh man, I feel special. It was a minor syntax error. Here's the solution for anybody in a similar situation. I ended up changing the function a little bit.

function


function userPostCheck()
{
$dbConn = new dbConnection();

if(isset($_SESSION['username'])){
return 1;
}else if(!isset($_SESSION['username'])){
return 0;
}else{
return 0;
}
}

Usage


'.$i = ($dbConn->userPostCheck() > 0) ? $dbConn->showPostArea() : $dbConn->blockPostArea().'