I have an issue where I'm showing boxes of some posts from my database in a foreach loop.
The length of the post title affects the design. For example if one post has a long title next to one with a short title, it will push the link i have below down. This makes it look uneven.
Therefor I'm trying to write a function that checks if the length is too short, it should insert a line break.
This is what I have so far.
function insert_line_break($text){
if (strlen($text) < 10 ) {
echo "<br>";
}
}
<?= insert_line_break($entry["title"]) ?>
However that seems to replace the title with a linebreak.
What am I missing?
You have missed to echo the text itself.
function insert_line_break($text){
if (strlen($text) < 10 ) {
return "<br>";
}
}
<?php echo $entry["title"] . insert_line_break($entry["title"]); ?>
Note:
I changed the function to return a value instead of echoing it to the output, so it will be much more reusable in the future.
And I expanded the short tag <? to <?php and made the = to verbose echo which is much clearer and readable way of coding.
And of course I closed the line with semicolon ; as it should be.
EDIT:
<?php echo $entry["title"] . insert_line_break($entry["title"]); ?>
echo prints the content of $entry["title"] and then ask function insert_line_break($entry["title"]) to decide whether it contains string shorter than 10 characters, if so it returns <br> that is echoed. And that's it.
Related
How can I convert content of a one line string into a code, that actually executes? Lets say that I intend to run the following code:
<?php
$sum=0;
for($i=1;$i<11;$i++){
$sum+=$i;
}
echo $sum;
?>
but part of the code is a content of a string $s:
<?php
$sum=0;
$s="for($i=1;$i<11;$i++){";
//what to write here to use the content of the string $s as a code
$sum+=$i;
}
echo $sum;
?>
EDIT: I am dealing with the hypothetical question, the two source codes are just an illustration.
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>';
}
?>
This code is supposed to help me in limiting the amount of characters seen in the timeline page. If the post is too long, (more than 400 characters), it should display a 'see more...' which should take the reader to a new page (posts.php?post=XXX). When I use this function, the second part after 'else' works fine. However, the first part seems to duplicate all the tags, and the spaces in this page appear twice bigger than the ones in posts.php. I can't trace the problem, someone please help me.
function display_content($long_text, $link, $page='posts', $page_ext = 'post'){
if(strlen($long_text) >= 400){
for($i=0; $i<=400; $i++){
echo nl2br($long_text[$i]);
}
echo "...<br /><a href='{$page}.php?{$page_ext}={$link}'>Read more...</a>";
} else echo nl2br($long_text);
}// I have taken care of all security issues.
Instead of parsing all characters of your text :
for($i=0; $i<=400; $i++){
echo nl2br($long_text[$i]);
}
Why don't you use :
echo nl2br(substr($long_text, 0, 400));
?
I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.
I would like to embed HTML inside a PHP if statement, if it's even possible, because I'm thinking the HTML would appear before the PHP if statement is executed.
I'm trying to access a table in a database. I created a pulldown menu in HTML that lists all the tables in the database and once I select the table from the pulldown, I hit the submit button.
I use the isset function to see if the submit button has been pressed and run a loop in PHP to display the contents of the table in the database. So at this point I have the complete table but I want to run some more queries on this table. Hence the reason I'm trying to execute more HTML inside the if statement. Ultimately, I'm trying to either update (1 or more contents in a row or multiple rows) or delete (1 or more rows) contents in the table. What I'm trying to do is create another pulldown that corresponded to a column in a table to make the table search easier and radio buttons that correspond to whether I'd like to update or delete contents in the table.
<?php if($condition) : ?>
This will only display if $condition is true
<?php endif; ?>
By request, here's elseif and else (which you can also find in the docs)
<?php if($condition) : ?>
This will only display if $condition is true
<?php elseif($anotherCondition) : ?>
more html
<?php else : ?>
even more html
<?php endif; ?>
It's that simple.
The HTML will only be displayed if the condition is satisfied.
Yes,
<?php if ( $my_name == "someguy" ) { ?>
HTML GOES HERE
<?php } ?>
Yes.
<?php if ($my_name == 'someguy') { ?>
HTML_GOES_HERE
<?php } ?>
Using PHP close/open tags is not very good solution because of 2 reasons: you can't print PHP variables in plain HTML and it make your code very hard to read (the next code block starts with an end bracket }, but the reader has no idea what was before).
Better is to use heredoc syntax. It is the same concept as in other languages (like bash).
<?php
if ($condition) {
echo <<< END_OF_TEXT
<b>lots of html</b> <i>$variable</i>
lots of text...
many lines possible, with any indentation, until the closing delimiter...
END_OF_TEXT;
}
?>
END_OF_TEXT is your delimiter (it can be basically any text like EOF, EOT). Everything between is considered string by PHP as if it were in double quotes, so you can print variables, but you don't have to escape any quotes, so it very convenient for printing html attributes.
Note that the closing delimiter must begin on the start of the line and semicolon must be placed right after it with no other chars (END_OF_TEXT;).
Heredoc with behaviour of string in single quotes (') is called nowdoc. No parsing is done inside of nowdoc. You use it in the same way as heredoc, just you put the opening delimiter in single quotes - echo <<< 'END_OF_TEXT'.
So if condition equals the value you want then the php document will run "include"
and include will add that document to the current window
for example:
`
<?php
$isARequest = true;
if ($isARequest){include('request.html');}/*So because $isARequest is true then it will include request.html but if its not a request then it will insert isNotARequest;*/
else if (!$isARequest) {include('isNotARequest.html')}
?>
`
<?php if ($my_name == 'aboutme') { ?>
HTML_GOES_HERE
<?php } ?>
I know this is an old post, but I really hate that there is only one answer here that suggests not mixing html and php. Instead of mixing content one should use template systems, or create a basic template system themselves.
In the php
<?php
$var1 = 'Alice'; $var2 = 'apples'; $var3 = 'lunch'; $var4 = 'Bob';
if ($var1 == 'Alice') {
$html = file_get_contents('/path/to/file.html'); //get the html template
$template_placeholders = array('##variable1##', '##variable2##', '##variable3##', '##variable4##'); // variable placeholders inside the template
$template_replace_variables = array($var1, $var2, $var3, $var4); // the variables to pass to the template
$html_output = str_replace($template_placeholders, $template_replace_variables, $html); // replace the placeholders with the actual variable values.
}
echo $html_output;
?>
In the html (/path/to/file.html)
<p>##variable1## ate ##variable2## for ##variable3## with ##variable4##.</p>
The output of this would be:
Alice ate apples for lunch with Bob.