Show div depending on user logged in - php

$getquery = mysql_query("SELECT * FROM it_task ORDER BY task_id DESC");
while ($rows = mysql_fetch_array($getquery))
{
$id= $rows['task_id'];
$date=$rows['date'];
$project=$rows['project'];
$topic=$rows['topic'];
$instby=$rows['instby'];
$inst=$rows['inst'];
$dline=$rows['dline'];
$ocome=$rows['ocome'];
$comm=$rows['comm'];
$fin=$rows['fin'];
$dellink="Delete";
$editlink="Edit";
$admin = "MJH";
if(($instby == $username)||($instby == $admin))
{
echo "<div id=\"editcont\">$editlink $dellink</div>";
}
else if($inst == $username)
{
echo "<div id=\"editcont\">$editlink <font face=\"Trebuchet MS, Arial, Helvetica, sans-serif\" size=\"2\">Delete</font></div>"
}
else
{
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
.in my code above what i want to do is to show a containing links depending on who is logged in.. however, when the admin logs in he will be able to see the containing $editlink and $dellink.
.I can't seem to find where exactly have i gone wrong.. please help me with this guys! TIA! More Power!
.this is what my code looks like.

if(($instby == $username)||($username == $admin))

$admin = "MJH";
if(($instby == $username)||($instby == $admin)) {
if($inst == $username) {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcont\">$editlink <font>Delete</font></div>";
}
else {
echo "<div id=\"editcont\">$editlink $dellink</div>";
echo "<div id=\"editcontdisabled\">Edit Delete</div>";
}
}
Thats how i percieved you wanted it, but really, it is very hard to understand whats going on here. Are $inst and $instby meant to be different variables? Also, out of curiousity, why have you double bracketed your if(); statements?
Hope I deciphered it correctly.
PS. I don't think echo();ing html is good practice.

Related

If statement within echo?

I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;

more button need to show after some count

if(count($fetchExperiences) > 0)
{
$i=0;
foreach($fetchExperiences as $fetchExperience)
{
$sqlExperiencedat = $db->createCommand(" code ");
$fetchExperiencesdat = $sqlExperiencedat->queryAll();
$tdyyy=date('YYYY-mm-dd');
if(strtotime($tdyyy)<strtotime($fetchExperience['experience_date']))
{
foreach($fetchExperiencesdat as $fetchExperiencesdatt)
{
if(strtotime($tdyyy)<strtotime($fetchExperiencesdatt['avail_date']))
{
//echo $fetchExperiencesdatt['avail_date'];
$tth=1;
}
}
if($tth==1){
//fetchExperiencesdatt code
}
}
}
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
}
}
My question is that I want more button after $fetchexperiences count is greater than 8. It is fine. But in between $fetchexperiences loop there is a loop for $fetchExperiencesdat. by the loop if there is no experince with in availdate it is not shown. so the probelm is more button is display not after 8 because of some $fetchExperiencesdat elements should be hidden. So please give me any solution for this.
Thanking you.
I'm not sure I understand where your count experiences are coming from. However, the problem your code has is that fetchCountExperiences is not given a value.
if(count($fetchCountExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}
Looks like it should be
if(count($fetchExperiences) > 8){
<a class="more_experience" style="clear:both;"><span onclick="getMoreExperiences1('<? php echo count($userResults);?>','<?php echo $_SESSION['user_id']; ?>','8')">More Experiences</span></a>
}

Giving a CSS Class to Echoing Strings

I've a PHP file which has numerous echo statements for various checks like this;
if ($power != "1")
{
echo "Please contact administrator for assistance.";
exit;
}
if (!$uid)
{
echo "You do not have permissions to change your status.";
exit;
}
if (!$mybb->input['custom'])
{
echo "You've not added any status to change";
exit;
}
I want to give a similar CSS class to each echo statement. I tried this;
if ($power != "1")
{
echo "<div class='class_name'>Please contact administrator for assistance.</div>";
exit;
}
and it works, but my php file has dozens of echo's and I don't want to edit each and every echo statement. Is there any easy way to achieve this?
You could define a function to handle outputting the message. You'll have to update the existing code but in the future, you'll be able to change the CSS class name or HTML structure easily be modifying the function.
class Response
{
public static function output($message, $className = 'class_name')
{
echo "<div class='" . htmlspecialchars($className) . "'>" . $message. "</div>";
exit;
}
}
Usage:
if ($power != "1")
{
Response::output("Please contact administrator for assistance.");
}
Override the class name:
Response::output("Please contact administrator for assistance.", "other_class");
If you're having issues/errors in above answers then here is my answer, I hope it helps;
Add the following code just above the <?php of your PHP file;
<style type="text/css">
.error{
background: #FFC6C6;
color: #000;
font-size: 13px;
font-family: Tahoma;
border: 1px solid #F58686;
padding: 3px 5px;
}
</style>
Next change each echo statement to something like this;
echo "<div class='error'>Write error code here.</div>";
exit;
You can easily find and replace the echo statements if you're using Notepad++
It should work. Also its somewhat similar to MrCode's answer however I think my answer is easily understandable and may be easy to implement.
There's no there way, unless you define once css class and put all your echo statements inside it.
<div class = "name">
<?php
echo ...
...
...
?>
</div>
Try to change each echo to a fixed variable name:
if ($power != "1")
{
$msg = "Please contact administrator for assistance.";
}
if (!$uid)
{
$msg = "You do not have permissions to change your status.";
}
if (!$mybb->input['custom'])
{
$msg = "You've not added any status to change";
}
Then write a function for giving style to the out put:
function stylizing($msg, $class="")
{
if($class != "")
$msg = "<div class='{$class}'>{$msg}</div>";
echo $msg;
}
Then you can use stylizing($msg, "class_name"); where you want to print out the result.
Do you mean something like this?
$message = '';
if ($power != "1") $message .= "<div class='one'>Please contact administrator for assistance.</div>";
elseif (!$uid) $message .= "<div class='two'>You do not have permissions to change your status.</div>";
elseif (!$mybb->input['custom']) $message .= "<div class='three'>You've not added any status to change.</div>";
echo $message;

PHP & MySQL - Compare IPs that are stored

I am trying to create a system to store the last logged on IP, and to compare it to the current IP, then do some functions later on down the road, but currently, I cannot compare them without going to the last else statement. Here's the code.
<?php
$userToPull = $session->userinfo['username'];
$query = "SELECT * FROM users WHERE username='$userToPull'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
$userToShow = $row[25];
$IPtoVerify = $row[26];
}
$lastActivity = RelativeTime($userToShow);
$currIP = $_SERVER['REMOTE_ADDR'];
/*
Shows Partner Stuff
}elseif(!$session->isAdmin()){
echo "<div style='text-align:right;' id='homebox'";
echo "<b>Partner Total:</b> ".$database->getNumMembers()."<br>";
echo $database->num_active_users." partners logged in and ";
echo $database->num_active_guests." guests viewing the site.<br><br>";
echo "</div>";
*/
if(!$IPtoVerify == $currIP){
echo "<div style='text-align:right; background-color: #FAAAB3' id='homebox_partner'";
echo "<b>You are logged on from an unrecognized location.</b><br>";
echo "You will be sent a notification for security purposes.";
echo "<br>This location will automatically be remembered.";
echo "</div><br>";
}elseif($IPtoVerify == $currIP){
echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>You are logged on from a recognized location.</b><br>";
echo "Your IP is ".$_SERVER['REMOTE_ADDR'];
echo "<br>Your last login was approximately ".$lastActivity;
echo "</div><br>";
}else{
echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>An error has occurred.</b><br>";
echo "</div><br>";
}
?>
The only thing not working is the if(!$IPtoVerify == $currIP){ if statement.
The IP is stored in the normal fashion, and echo's like: 100.100.100.100. (normal fashion)
Maybe I am not comparing them right, but it has worked for me in the past.
This code doesn't do what you think:
if (!$IPtoVerify == $currIP) {
PHP interprets it as:
if ((!$IPtoVerify) == $currIP) {
You will want to write it as:
if ($IPtoVerify != $currIP) {
Try ($IPtoVerify !== $currIP) instead of (!$IPtoVerify == $currIP)
!$IPtoVerify == $currIP
means
0==$currIP,
because it first validates
`!$IPtoVerify`
which always returns 0 unless $IPtoVerify is 1.
Add additional brackets like
if(!($IPtoVerify == $currIP))...
to solve the issue.

Styling an If Else Statement

I know that it's not possible to style any given PHP but I do know that it is possible to style the output HTML that the PHP provides you with. I was wondering if there was any method available for me to use that would enable me to style the output from my IF ELSE statement.
if ($result != false) {
print "Your entry has successfully been entered into the blog.";
}
Just use HTML in your printed string. Nothing fancy needed.
if ($result != false) {
print "<p class=\"success\">Your <strong>entry</strong> has successfully been entered into the blog. <img src=\"smileyface.png\" alt=\"Smiley face\" /></p>";
}
You mean this?
if ($result != false) {
print "<span style='color:#ff0000'>Your entry has successfully been entered into the blog.</span>";
}
What I like to do, just to make life simple, is to switch between HTML and PHP in my page. For example,
<?php if ($fooSuccess = true) {?>
<p><span style="color: green;">Success!</span></p>
<?php } else {?>
<p><span style="color: red;">Error!</span></p>
<?php } ?>
You need to print HTML then.
if ($result != false) {
print "<b>Your entry has successfully been entered into the blog.</b>";
}
Or even
if ($result != false) {
print "<div class='Style1'>Your entry has successfully been entered into the blog.</div>";
}

Categories