I am trying to check to make sure a custom-field is not blank before echoing the custom-field.
This is what I have
<?php
$key = 'one_line_summary';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<blockquote><?php echo get_post_meta($post->ID, one_line_summary, true); ?></blockquote>';
}
?>
But it out puts the "get_post_meta($post->ID, one_line_summary, true);" literally rather than the contents of the variable one_line_summary.
I am a beginner but I feel like I need to either use nested echo's somehow or change the second echo all together?
Thanks in advance.
You have nested <?php ?> inside an existing set of PHP tags, which is not allowed. Remove those, and concatenate in the function call to get_post_meta(). What happened here is that the inner <?php ?> tags were output as strings to the browser, but not rendered onscreen (since the browser treated them as unknown HTML tags).
echo '<blockquote>' . get_post_meta($post->ID, one_line_summary, true) . '</blockquote>';
As a note, these kinds of issues are considerably easier to spot with proper code indentation as was done when your post was edited above.
Related
I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>
UPDATE:
Thanks for all the suggestions (what did I do to get -3? I don't understand that)
I found that the data field was set as VARCHAR and truncated the JSON off at 255 characters, making it invalid. I changed that, and the JSON is now valid according to JSONlist.com
But it still refuses to chow, so at the risk of getting more demerits, here is the current code, is there anything I am doing wrong?
JSON:
[{"name":"images/front_8mhztpaj.jpg","usrName":"GEDC0041.JPG","size":805229,"type":"image/jpeg","thumbnail":"images/front_8mhztpaj.jpg","thumbnail_type":"image/jpeg","thumbnail_size":10490,"searchStr":"front_8mhztpaj.jpg,!:sStrEnd"}]
New Code:
<?php
$json = $DETAILS->getColumnVal("IMAGE");
//
json_decode($json, true);
?>
<?php if ($DETAILS->getColumnVal("IMAGE") != "") { ?>
<?php $imgsrc = "input/".$myImage = $myArray[0]['name']; ?>
<img src="<?php echo $imgsrc ?>" class="img-responsive center-block" alt="<?php echo $DETAILS->getColumnVal("TITLE"); ?>"> <?php echo $imgsrc; ?>
<?php } ?>
</div>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
The query is still working correctly, when I ask it to echo "IMAGE" it shows the json string I have posted here, but in the page where the image data should be it just shows "input/" and no text afterwards.
What am I doing wrong?
The JSON you provided is misformated, there's missing closing tags. Seems the backslashes are mandatory, you can remove them if you have no need to escape your paths. (#alex-t)
Also, a good practice is to avoid PHP opening/closing tags in the middle of your code. That'll make it less messy.
<?php
$json = $DETAILS->getColumnVal("IMAGE");
// Stripslashes() is mandatory
$myArray = json_decode(stripslashes($json), true);
// Security check in case json_decode returns false.
if ($myArray) {
$myImage = $myArray[0]["name"];
$myTitle = $DETAILS->getColumnVal("TITLE");
// Into a double quoted string, you can 'inject' php vars.
// just put it like $myVar or wrap it with {$myVar}
echo "<img src=\"input/{$myImage}\" class=\"img-responsive center-block\" alt=\"{$myTitle}\">";
}
SOLVED
The main issue I had (thanks to you guys) was that the data field containing the JSON was set as a VARCHAR, and truncated to 255 characters.
The secondary issue I had was a php error
json_decode($json, true);
should have read:
$myArray = json_decode($json, true);
Thanks to all who helped
You need to remove the slashes from the JSON string first:
$myArray = json_decode(stripslashes($json), true);
I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>
Here's the problem, I am trying to echo a statement or an array after dynamically generated HTML, and unfortunately the thing that i want to echo goes above the HTML, is there any way to echo it after that dynamic HTML or work around?
Code:
Link 1
Link 2
if(isset($_GET["id"]) && $_GET["id"] == "do_something") {
$html = "dynamic html generate";
echo $html;
//after this im using foreach
foreach($array as $item) { echo $item . "<br />"; }
}
As I click one of these two , dynamically generated HTML shows up. Now for example I have an array:
$array = array("error1", "error2");
All the generated PHP goes above the dynamic HTML :/.
How should i fix it so that i can echo all of this array below the dynamic HTML?
Thanks
Use buffering with ob_start
ob_start();
// dynamic html code generate
$dynamic_html = ob_get_clean();
echo $dynamic_html;
// your code
echo $dynamic_html;
Sounds like you missed some closing tags (most likely </table>) in the dynamic html. Thats why the later generated echo gets displayed at the top.
Example (Note the missing closing table):
<?php
echo "<table><tr><td>TableText</td></tr>";
echo "I should be bellow the table, but going to the top.";
?>
will produce:
I should be bellow the table, but going to the top.
TableText
Besides personal preference, does it make any difference?
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strNum', true);
'' != $meta and print "$meta";
?>
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strName', true);
'' != $meta and print "$meta";
?>
as opposed to this
<?php
$meta = get_post_meta(get_the_ID(), 'rw_strNum', true);
'' != $meta and print "$meta";
$meta = get_post_meta(get_the_ID(), 'rw_strName', true);
'' != $meta and print "$meta";
?>
The first version will output an extra newline character into the generated output, since there's one between the ?> and the <?php:
?>
<?php
That is the only difference; there isn't any noticeable performance impact between the two.
Everything outside <?php ?> is treated as output. This means, that
?>
<?php
may output something. "May" because the newline after ?> is part of the tag and therefore not returned. But with something like
?>
<?php
there are two whitespace echoed. The problem is, that you cannot set any headers anymore, after something is returned to the browser.
Like knittl said, it will not make any visible difference. The only difference is it will make your HTML easier to read when you mix PHP and HTML together, for example when doing templates.
The php parser goes through the whole file regardless and I doubt encountering a closing tag and then an opening tag has any impact on parsing/speed of parsing.
Yes it makes a difference. Look at this:
<?php
$uselessvar = 1;
?>
<?php
header('Location: /'); // This will not work
?>
<?php
$uselessvar = 1;
header('Location: /'); // This will work
?>
In the first example, there is a new line between the first closing tag ?> and the second opening tag <?php. This new line is treated as an output and it's send to the client. header function could not work if any output is sent to the client before its called. That's why the first example will not work where the second will.
In a more general way, it's better to use the closing tag ?> only where you need it, to avoid such errors.
For example, you don't have to put a closing tag ?> at the end of a php file. Sometimes we see a file terminated by a closing tag then a new empty line. This new empty line has the same effect as above, and can crash/alter any script.