PHP/ Get current URL + conditional - php

I am not very good at php but I want to detect if a user is in the home of the website such as;
<?php
if (current path = http://website.com/ || website.com)
{?>
<div>We are in home</div>
<?php } else { ?>
<div>do something</div>
<?php } ?>
Someone help me please thanks

$pageName = $_SERVER['PHP_SELF'];
if($pageName == 'http://website.com'){
echo '<div>We are in home</div>';
} else { do something };

Related

Header in the top

At that time I need two view pages in one page. So that in the session i do something like this:
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
?>
///My HTML CODE///
<?
}
else {
?>
//Diffetent View HeRe//
<?
}
require_once('../libraries/config/configPDO.php');
?>
But now I just need to make it become one page and redirect it if they not logged in.
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
?>
///My HTML CODE///
<?
}
else {header("Location: login.php");
?>
<?
}
require_once('../libraries/config/configPDO.php');
?>
But as you see, I put the header redirect at the bottom of the page.
How to make it simple, so that I set header location in the top of the page.
Try with -
session_start();
if(empty($_SESSION['login_id'])){
header("Location: login.php");
exit;
} else {
//HTML
}
try this,
<?php
ob_start();
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
?>
///My HTML CODE///
<?
require_once('../libraries/config/configPDO.php');
}
else {
header("Location: login.php");
}
?>
reverse the if to
if(!isset($_SESSION['login_id']) || empty($_SESSION['login_id'])){
and swap the cases

How do i take a value from URL and don't display a div from template with certain value

I want to leave out a div from my template.html that is displayed when someone clicks on the sidebar or navigation bar. When a category is navigation_bar I want it to leave out something like a Facebook comment box. Because it is only needed on the sidebar pages.
an URL example index.php?catagory=navigation_bar&subject=contact
I was thinking about something like this:
<php?
if (isset($_GET['catagory']))
{
$catagory = $_GET['categorie'];
if $catagory = navigatie_bar
{
//here something that says: then don't display <div class="fb-comments'></div>
}
}
?>
can someone help me out ?
is this what you are looking for-
<?php
if (isset($_GET['catagory']))
{
$catagory = $_GET['categorie'];
if $catagory = navigatie_bar
{
//here something that says: then don't display <div class="fb-comments'></div>
}
}
?>
<input type="hidden" value="<?php echo $category?>" id="tohide"/>
<div id="comment">comments....</div>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready({
var id=$("tohide").val();
$("#"+id).hide();
});
</script>
Your tried seems correct just bit correction,
//This condition says show fb comment on all pages but navigation_bar
if(isset($_GET['catagory']) && $_GET['catagory'] != "navigatie_bar")
{
echo " <div class='fb-comments'></div>";
//Your code goes here
}

How to get name of selected option and sent it to other php script?

This is my jQuery code
<script>
$(document).ready(function() {
$('#countries').change(function(){
$('#countriesForm').submit();
});
});
</script>
and my php code looks like this
<form action="<?php echo $this->createUrl($this->id."/".$this->action->id); ?>" id="countriesForm" method='POST'>
<?php echo CHtml::dropDownList('countries', $select = array(),
CHtml::listData($countries, 'code', 'name'));
?>
<form>
<div class='description'></div>
Can someone pls help me how to get echo/print name or code of the selected country into the div .description.
And how to send the selected code to other php script.
It can be like
<div class='description'><?php
if( isset($_POST['countries']) ) {
echo CHtml::encode($_POST['countries']);
}
?></div>
<?php
$controller = Yii::app()->controller->id;
$action = Yii::app()->controller->action->id;
?>
Using this, you get the controller and action performed.
<li <?php
if ($controller=="site"&&$action=="index"){
echo 'class="active"'; } ?> >
<?php echo CHtml::link('Home',array('site/index')); ?>
</li>

display hyperlink in php

I am new to php. Here is a code to check if the user is logged in using session and then allowing the user.
VALIDATION
<?php
session_start();
$uname = $_POST['uname'];
$pass = $_POST['pass'];
if($uname == "admin" && $pass == "admin")
{
$_SESSION['uname'] = $uname;
$_SESSION['auth'] = 1;
echo "Welcome Mr ".$uname.". You are now logged in ";
echo "<br>";
echo "<a href='TakeMeHome.html'>Click here to access the application </a>";
}
else
{
echo "Invalid username or password";
}
?>
Page
<?php
session_start();
if($_SESSION['auth'] != 1)
{
echo "You are not logged in! ";
echo "<a href = \"TakeMeHome.html\">";
echo "Access Application";
echo "</a>";
exit();
}
?>
<html>
You are now logged in
</html>
But the link tag is displaying
"; echo "Access Application"; echo ""; exit(); } ?>
along with the html data. No verification is done. I know there are many better ways to validate user is logged in or not. But i am learning sessions and hence i am using sessions.
Can you please tell me where i am going wrong?
regards.
use single quote in your echo codes like this:
<html>
<head>
</head>
<body>
<?php
echo "<a href='pageToGoTo.html' title='Page to go to' class='whatEver'>Anchor text</a>";
?>
</body>
</html>
What is told already, html should be put in the body...
I have no idea why #Andy has suggested you put your PHP in your head tags - it isn't javascript. You have 2 ways you can format your PHP and HTML, the first is to put all your PHP above your opening html tag, like so
<?php
session_start();
if($_SESSION['auth'] != 1) {
$message = 'You are not logged in! Access Application';
} else {
$message = 'You are logged in!';
}
?>
<html>
<head>
</head>
<body>
<?php echo $message; ?>
</body>
</html>
Or, place it in the body of your page, like so:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php
if($_SESSION['auth'] != 1) {
echo 'You are not logged in! Access Application';
} else {
echo 'You are logged in!';
}
?>
</body>
</html>
If you are still not getting the desired results then use var_dump($_SESSION); to print out your session array and make sure it holds the correct information.

Show Hide div if, if statement is true

My code works to a point. What I want is that when this if statement is false, the <div> doesn't show
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0) {
$fvisit = mysql_fetch_array($result3);
}
else {
}
?>
You can use css or js for hiding a div. In else statement you can write it as:
else{
?>
<style type="text/css">#divId{
display:none;
}</style>
<?php
}
Or in jQuery
else{
?>
<script type="text/javascript">$('#divId').hide()</script>
<?php
}
Or in javascript
else{
?>
<script type="text/javascript">document.getElementById('divId').style.display = 'none';</script>
<?php
}
This does not need jquery, you could set a variable inside the if and use it in html or pass it thru your template system if any
<?php
$showDivFlag=false
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
if ($numrows > 0){
$fvisit = mysql_fetch_array($result3);
$showDivFlag=true;
}else {
}
?>
later in html
<div id="results" <?php if ($showDivFlag===false){?>style="display:none"<?php } ?>>
A fresh look at this(possibly)
in your php:
else{
$hidemydiv = "hide";
}
And then later in your html code:
<div class='<?php echo $hidemydiv ?>' > maybe show or hide this</div>
in this way your php remains quite clean
Use show/hide method as below
$("div").show();//To Show
$("div").hide();//To Hide
<?php
$divStyle=''; // show div
// add condition
if($variable == '1'){
$divStyle='style="display:none;"'; //hide div
}
print'<div '.$divStyle.'>Div to hide</div>';
?>
Probably the easiest to hide a div and show a div in PHP based on a variables and the operator.
<?php
$query3 = mysql_query($query3);
$numrows = mysql_num_rows($query3);
?>
<html>
<?php if($numrows > null){ ?>
no meow :-(
<?php } ?>
<?php if($numrows < null){ ?>
lots of meow
<?php } ?>
</html>
Here is my original code before adding your requirements:
<?php
$address = 'meow';
?>
<?php if($address == null){ ?>
no meow :-(
<?php } ?>
<?php if($address != null){ ?>
lots of meow
<?php } ?>
from php you can invoke jquery like this but my 2nd method is much cleaner and better for php
if($switchView) :?>
<script>$('.container').hide();</script>
<script>$('.confirm').show();</script>
<?php endif;
another way is to initiate your class and dynamically invoke the condition like this
$registerForm ='block';
then in your html use this
<div class="col" style="display: <?= $registerForm?>">
now you can play with the view with if and else easily without having a messed code example
if($condition) registerForm = 'none';
Make sure you use 'block' to show and 'none' to hide. This is far the easiest way with php
If you wrap the whole block within the <?php ?> tags in the initial 'if' statement and don't specify an 'else', this condition is not satisfied, returns false so therefore won't display the <div>

Categories