Forum Moderators: coopster
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?
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.
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?
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().'