Show url only if value is present - php

Goal:
Instead of showing simple YES or NO.
If value is found in record, show hyperlink with that value or else show text "No"
How to modify below code for this purpose:
<?php echo $row_RecordsetContacts['propertyFile'] ? '<strong>Yes</strong></br>' : 'No</br>'; ?>
View
</td>

<?php
$file = $row_RecordsetContacts['propertyFile']; # for readability only
if ($file)
{
?>View<?php
}
else
{
?>No<?php
}
I also suggest to avoid mixing echo and HTML markup. In 99% cases it makes the code better for understanding.

Try the following code:
<?php
$prop = $row_RecordsetContacts['propertyFile'];
if(empty($prop)) {
echo "No";
} else {
echo "<a href='propfiles/$prop'>View</a>";
}
?>

Related

GET request variable error in PHP

I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.

Passing a PHP Value from a link

I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>

PHP function that decides which HTML code to use?

I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1

PHP if/else statement

How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/

Need help in display members name when logged in using PHP?

for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.
Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)
Make sure you're also calling session_start() before trying to access the variables.
Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>
This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?&gtl
Is it?
Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..

Categories