I would like to insert a PHP tag within a PHP tag in the example below:
<?php $values = get_field('sold');
if($values)
{ foreach($values as $value) {echo '<div id="sold">'; echo $value; echo '</div>'; } }
else {
echo '<div id="inquire">Inquire about this item...</div>';
}
?>
I would like to imprint the_title(); as the subject of the email.
Can anyone offer any insight?
If the title is the return value of a php function you can just call the function and concatenate it to the rest of the echo.
For example
echo '<div id="inquire">Inquire about this item...</div>'
Related
I am trying to get values from a form but on Submit request. Bellow is just the PHP code as I know that the HTML Form is correct as it worked perfectly when I did not place the isset() function into the PHP.
<?php
if (isset($_POST["submit"]))
{
$fname = $_POST['firstname'];
$emailstr = $_POST['email'];
$postaddrstr =$_POST['postaddr'];
$favsportstr =$_POST['favsport'];
$emailliststr =$_POST['emaillist'];
}
?>
I believe the error lies somewhere in the below part as i am getting an undefined variable message for $val and an Array to string conversion at the foreach loop.
<section id="output">
<?php
if (isset($_POST["submit"]))
{
echo "<h2>The following information was received from the form:</h2>";
echo "<p><strong>First Name:</strong> $fname </p>";
echo "<p><strong>Email = </strong> $emailstr </p>";
echo "<p><strong>Post Address = </strong> $postaddrstr </p>";
echo "<p><strong>Your Favourit Sport:</strong>
foreach($favsportstr as $val) {
$val
}";
echo "<p><strong>Email list = </strong> $emailliststr </p>";
}
?>
</section>
You can't write loops inside echo change following line:
echo "<p><strong>Your Favourit Sport:</strong>
foreach($favsportstr as $val) {
$val
}";
to
echo "<p><strong>Your Favourit Sport:</strong> ";
foreach($favsportstr as $val) {
echo $val;
}
This is where your error comes from:
echo "<p><strong>Your Favourit Sport:</strong>
foreach($favsportstr as $val) {
$val
}";
Just change it to this and it will be fine:
echo "<p><strong>Your Favourit Sport:</strong>";
foreach($favsportstr as $val) {
echo $val;
}
The reason is you can't put a loop inside an echo statement. Put echo inside the loop not the loop inside echo. 'echo' statement is just for printing the output. It wont support any processing it will just print everything inside it. It can only output the variable values when they are placed between double quotes. Like this : echo "$var";
Edit |
This is a simple url...
The issue is I have to echo it in a while loop.
Which means I cant use php tags.
MY cancatenation sucks... I tried it for hours (noob) Please help
The issue is I have to echo it in a while loop. Which means I cant use php tags.
It doesn't mean that.
<?php
while ($condition) {
?>
Edit |
<?php
}
?>
(But if you have a list of links, then use list markup (ul/ol/li) not | characters).
try something like this
echo 'Edit'
It's pretty simple:
<?php
While(condition){
?>
Edit
<?php
}
?>
I would suggest to "prepare" the whole element in php. Something like this:
foreach ($arr as $key) {
print 'Edit';
}
<?php
while ($condition) {
echo 'Edit';
}
?>
You can also use heredoc syntax.
Sidenote, I would collect them all and echo it out once in the end
$text = '';
while ($foo) {
$text .= 'Edit';
}
echo $text;
// or heredoc
$text = '';
while ($foo) {
$text .= <<<_HTML
Edit
_HTML;
}
echo $text;
// or array
while ($foo) {
$data[] = 'Edit';
}
if(!empty($data)){
echo '<p>'.implode('</p><p>',$data).'</p>';
}
if it is a template file in php then always use this type of syntax
code:
<?php
$i=0;
while($i<10) : ?>
<div><?php echo $i;?></div>
<?php
$i++;
endwhile;?>
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
Hello i'm having this variable which is string.
$this->product_output_html = <<< HTML
Some HTML Code
HTML;
I want int he class test to add a php for loop like this one
if ($admin->errors) { foreach ($admin->errors as $error) {
echo ''.$error.''; } }
i have tried to add but is not working. i added '' after the class="test"> and before the of the test but still is not working. what i'm doing wrong?
thanks a lot
try something like
$this->product_output_html = 'Start of html code as a string';
if ($admin->errors) {
foreach ($admin->errors as $error) {
$this->product_output_html .= '<br />'.$error;
}
}
$this->product_output_html .= '<br />End of html code as a string';
echo $this->product_output_html;
replace
echo ".$error.";
with
echo $error;
For a multiple choice quiz application i would like to show the dummy answers with the correct answer. But with the correct answer being in a different position at each different question.
This is what i've tried but it doesn't seem to be working:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$dummy_num = 1;
foreach ($query->result() as $row)
{
$rand_number = rand(1, 3);
if ($dummy_num == $rand_number)
{
$dummy_num = $rand_number + 2;
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
//Now echo the real answer
echo '<h4>Answer '.$rand_number.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>'; //Get id's for each.echo $row->id;
}
else
{
echo '<h4>Answer '.$dummy_num.'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
$dummy_num++;
}
}
echo '</div>';
echo ' <hr/>';
}
?>
You should use shuffle function.
In your case it will be:
if ($question->type == 1)
{
echo "<div id='dummy_answers'>";
//Show Dummy
echo '<h3>Dummy Answers</h3>';
//Get Dummy Answers
$query = $this->test_model->getDummyAnswers($question->id);
$answers=$query->result();
shuffle($answers);
foreach ($answers as $nr=>$row)
{
echo '<h4>Answer '.($nr+1).'</h4>';
echo '<p>';
echo $row->option;
echo '</p>';
}
echo '</div>';
echo ' <hr/>';
}
?>
Put the answers in an array the use shuffle
$random_array = shuffle($answers);
All you need to do is put the keys for your answers in an array and call shuffle(). Something like this:
$keys = array_keys($answers);
shuffle($keys);
for ($key in $keys) {
echo $answers[$key];
}
I would suggest putting all the answers into an array and using the shuffle() function to randomize them. Once they're shuffled, just iterate the array with a loop and build the markup.
You could put the results into an array (1 correct and 3 incorrect), then shuffle, then output them?
$answers = array();
array_push($answers, "answer1");
array_push($answers, "answer2");
array_push($answers, "answer3");
array_push($answers, "answer4");
shuffle($answers);
foreach ($answers as $answer) {
echo $answer;
}