I'm trying to echo regular text along with arrays. I'm basing the below code off of the answer found here, but it's not working: Echo arrays with regular text
<?php
$val1 = Yes;
if (($row->relation) == ($val1)) {
echo "<p><b>Applicant\'s Name:</b> {$row['relation_name']} | <b>Business:</b> {$row['relation_business']}</p>";
}
?>
You are doing so many thing wrong here
Example
V--------------------- Row Seems to be Object here
if (($row->relation) == ($val1)) {
echo "<p><b>Applicant\'s Name:</b> {$row['relation_name']}
^---------------------- Calling it as array here
After you clarify the above you can just use printf instead
If its array :
printf("<p><b>Applicant\'s Name:</b> %s|<b>Business:</b>%s</p>",$row['relation_name'],$row['relation_business']);
If its Object
printf("<p><b>Applicant\'s Name:</b>%s|<b>Business:</b>%s</p>",$row->relation_name,$row->relation_business);
You can use . notation which is clearly used for concatenation in php:
<?php
$val1 = Yes;
if (($row->relation) == ($val1)) {
echo "<p><b>Applicant\'s Name:</b>" . $row['relation_name'] . | . "<b>Business:</b>" . $row['relation_business'] . "</p>";
}
?>
or you can seperate HTML and PHP like this:
<?php
$val1 = Yes;
if (($row->relation) == ($val1)) {
?>
<p><b>Applicant\'s Name:</b><?php echo $row['relation_name'] ?>|<b>Business:</b><?php echo $row['relation_business'] ?></p>
<?
}
?>
Related
I want to echo out this code but there's already open and close php tag , so how do I sub the html and php in the echo and make the php function works.
This is the code that I want to echo it out.
<div class="tutor-zoom-join-button-wrap">
<?php echo $browser_text; ?>
<?php _e('Join in Zoom App', 'tutor-pro'); ?>
</div>
This is the function that I made(replace with code above with 123)
<?php
$var1 = 1;
if ($var1 = 1) {
echo "123 ";
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
This is what I try but not working
<?php
$var1 = 1;
if ($var1 = 1) {
echo
"<div class="tutor-zoom-join-button-wrap">"
"",.$browser_text."",
""._e('Join in Zoom App', 'tutor-pro')."",
"</div>",
"</div>" ;
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
I have modified your code. Please check and you also refer to some PHP tutorials to learn php.
<?php
$var1 = 1;
if ($var1 == 1) {
echo
"<div class='tutor-zoom-join-button-wrap'>
<a href=" .$browser_url. " target='_blank' class='tutor-btn tutor-button-block'>".$browser_text."</a>,
<a href=".$meeting_data['join_url']."target='_blank' class='tutor-btn bordered-btn tutor-button-block'>"._e('Join in Zoom App', 'tutor-pro')."</a>",
"</div>",
"</div>" ;
}
else {
echo "The course ID, password and join button will only be shown before 30min of course start";
}
?>
The problem is in the long string you want to echo. The double quote " starts and ends the string. You have started and ended the string several times when what you want is one long string. If you want to use a double quotes inside the string you have to precede it with a slash \". To concatenate strings you need to use a dot .. Double quotes also allow you to place variables inside the string that will be replaced. So your echo statement could be done like this:
echo
"<div class=\"tutor-zoom-join-button-wrap\">" .
"$browser_text" .
"" . _e('Join in Zoom App', 'tutor-pro') . "" .
"</div>" .
"</div>" ; // Note that this tag does not appear to be necessary based on the code you have shown
You can write it like this:
<?php
$var1 = 1;
if ($var1 = 1) {
echo `<div class="tutor-zoom-join-button-wrap">
`.$browser_text.`
`._e('Join in Zoom App', 'tutor-pro').`
</div>
</div>` ;
} else {
echo "The course ID, password and join button will only be shown before 30min of course start";
};
?>
Hope It will work for you!
So I have a variable in PHP that I would like to print along with an html tag but I am very new to PHP and I can't seem to get the syntax right
My goal is to output/print the following
<div class="post-block" style="background-image:url(Whatever image here);">
My code is below:
$feature_posts = the_sub_field('feature_image_post');
if ( get_sub_field('post-feature') == "" ) {
echo '<div class="post-block" style="background-image:url(' .$feature_posts");>';
echo '</div>';
}
You are missing the concatenation operator and the closing single quote after your variable:
$feature_posts = the_sub_field('feature_image_post');
if ( get_sub_field('post-feature') == "" ) {
echo '<div class="post-block" style="background-image:url(' . $feature_posts . ')">';
echo '</div>';
}
To keep the format more readable you can use double quotes to allow variables in your string. Just have to remember escaping the quotes within the markup:
$feature_posts = the_sub_field('feature_image_post');
if ( get_sub_field('post-feature') == "" ) {
echo "<div class=\"post-block\" style=\"background-image:url($feature_posts)\">";
echo '</div>';
}
You can make that.
<?php for($i = 0; $i <= 100; $i++){ ?>
<div class="post-block" style="background-image:url(<?php echo the_sub_field('feature_image_post') ?>);"></div>
<?php } ?>
This a sample how you can do.
I have a condition within echo statement like this, how to adjust it to make it working:
echo "<option value="http://localhost/myproject/index.php?if(empty($_GET['view'])){echo "view=main-content";}else{$view=basename($_GET['view']);echo "view=".$view;}"></option>";
Many thanks
I noticed that you are using two times view, threfore if you condition enters the else your final url would be something like: ?view=whateverview=whatever2 which is clearly wrong.
$view = (empty($_GET['view']) ? 'main-content' : basename($_GET['view']));
echo "<option value='http://localhost/myproject/index.php?view=" . $view . "'></option>";
If you would like to add more parameters to the URL you'll need to use &.
is this your expected output?
$d = "<option value='http://localhost/myproject/index.php?";
if(empty($_GET['view']))
{
$d .= "view=main-content";
}
else
{
$view=basename($_GET['view']);
$d .="view=".$view;
};
$d .="'>Testing</option>";
echo "<select>". $d."</select>";
You could pass it to a variable, and concatenate it with the rest of your echo statement.
if(empty($_GET['view'])){
$res = 'main-content';
} else {
$res = basename($_GET['view']);
}
echo '<option value="http://localhost/myproject/index.php?view=' . $res . '"></option>';
And since no one else posted it:
echo '<option value="http://localhost/myproject/index.php?view='
. (empty($_GET['view']) ? 'main-content' : basename($_GET['view'])
. '"></option>';
Its been a while since the last time I was coding, and I am a little rusty. I am trying to parse an xml file and having a problem with the condition. Spent hours trying to figure out where is the problem, but without success...
PHP CODE:
<div id="gallery">
<?php
$cat_img="images/cat.png";
$logo_path="logos/";
$file = simplexml_load_file('stores.xml');
$old_cat="";
foreach($file->store as $store){
if($store->cat != $old_cat){
echo "<img id='cat' src='".$cat_img."'><div id='cat_text'>".$store->cat."</div><br>";
$old_cat=$store->cat;
}
echo "<div id='store'><img id='store_img' src='".$logo_path.$store->logo."' alt='logo'><br>";
echo "<store_name>".$store->name."</store_name>";
echo "<phone>".$store->phone."</phone><phone>טל: </phone></div>";
}
?>
</div>
STORES.XML
<?xml version="1.0" encoding="UTF-8"?>
<stores>
<store><logo>renuar.jpg</logo>
<name>renuar</name>
<phone>052-6059962</phone>
<cat>clothing</cat></store>
<store><logo>yoop.jpg</logo>
<name>YOOP</name>
<phone>08-6601451</phone>
<cat>clothing</cat></store>
....
</stores>
The idea is to print every store, when category changes it should print the category divider and keep printing the stores. Somehow the condition is always true and I have no idea why...
p.s
If anyone has a better idea on how to divide the categories, a different method, I would be happy to hear.
Thanks!!!
$store->cat will be SimpleXMLElement object and two object will not be same even with same category name. You need to cast it to string for comparison
Like (string) $store->cat, try this:
<?php
$cat_img = "images/cat.png";
$logo_path = "logos/";
$file = simplexml_load_file('test.xml');
$old_cat = "";
foreach ($file->store as $store) {
if ((string) $store->cat != $old_cat) {
echo "<img id='cat' src='" . $cat_img . "'><div id='cat_text'>category: " . $store->cat . "</div><br>";
$old_cat = (string) $store->cat;
}
echo "<div id='store'><img id='store_img' src='" . $logo_path . $store->logo . "' alt='logo'><br>";
echo "<store_name>" . $store->name . "</store_name>";
echo "<phone>" . $store->phone . "</phone><phone>טל: </phone></div>";
}
?>
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>";
}
}
?>