I've created a simple query to create a list of dates and descriptions from my calendar database.
The idea is that I have a PHP webpage that show a text list that I can quickly copy and paste into an email or text message.
My problem is that, although the text shows up correctly on the web page, when I paste the info into a text editor (Word, email, whatever) I'm getting tabs between each column.
How can I format the text in PHP so that it pastes correctly?
This is my code:-
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li>
<?php echo htmlentities($row_AvDates['Month']);?>
<?php echo "-";?>
<?php echo htmlentities ($row_AvDates['the_days']);?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
This gives me output that looks correct, but pastes like this...
(Month[tab]"-"[tab] Dates)
How do I loose the tabs?
You're closing php and letting the HTML output be rendered. See tab chars below:
[tab]<li><?php echo htmlentities($row_AvDates['Month']);?>
[tab]<?php echo "-";?>
[tab]<?php echo htmlentities ($row_AvDates['the_days']);?></li>
Solution:
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities ($row_AvDates['the_days']); ?>
Darren.
Try to use this code, it should solve your problem:
<?php if (mysql_num_rows($AvDates) > 0) : ?>
<ul>
<?php while ($row_AvDates = mysql_fetch_assoc($AvDates)) : ?>
<li><?php echo htmlentities($row_AvDates['Month'])."-".htmlentities ($row_AvDates['the_days']);?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hope it helps.
you can have the whole output in one single like this
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities($row_AvDates['the_days']);?>
instead of separate php tags
If you want to separate code across multiple lines, not all code in one line:
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li><?php
echo htmlentities($row_AvDates['Month']);
echo "-";
echo htmlentities($row_AvDates['the_days']);
?></li>
<?php
}
?>
</ul>
<?php
}
?>
Related
IN PHP I noticed that if we have code like below:
<?php if ( function('parameter')):?>
<?php //do something here ?>
<?php endif; ?>
why can't we write this code like:
<?php if ( function('parameter'))
//do something here
endif; ?>
I am new to PHP, Thanks a lot!!
The PHP code has to be inside <?php ?> and the HTML markup needs to be outside. You can also print out the HTML markup with echo.
Here is an example (much cleaner in my opinion, than example 2). The HTML markup is inside a PHP string. The return value of the_field(), a string, is then concated with .:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
echo '<p class="btn">Request a Quote</p>';
}
if(get_field('rfq_pdf_url')) {
echo '<p class="btn">Download PDF</p>';
}
?>
And here is another valid example (2). You can end the PHP part with ?> and output regular HTML markup and then start the PHP part again with <?php:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) { ?>
<p class="btn"><a href="
<?php the_field('quote_url'); ?>
">Request a Quote</a></p>
<?php }
if(get_field('rfq_pdf_url')) { ?>
<p class="btn"><a href="
<?php the_field('rfq_pdf_url');?>
">Download PDF</a></p>
<?php }
?>
It would however be redundant to start with <?php on every line and end it then again with ?>.
Another possibility would be:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
?>
<p class="btn"><a href='<?php echo the_field('quote_url'); ?>'>Request a Quote</a></p>
<?php
}
if(get_field('rfq_pdf_url')) {
?>
<p class="btn">Download PDF</p>
<?php
}
?>
The context
I have the following code:
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
It renders:
<ul>
</ul>
When there are $users, everything works well. However, when there aren't, the <ul> is rendered with blank spaces in it, making it not attributable with the CSS selector :empty.
I managed to solve the problem by doing this:
<ul><?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?></ul>
It renders:
<ul></ul>
Without the like breaks in the code, the list is now empty (even empty of blank spaces), but I'm afraid to leave it that way and some other programmer fix the indentation and break it.
My question
Is there a way to add line breaks to the code without adding blank spaces or breaks to the rendered html element?
My question is: Is there a way to add line breaks to the code without adding blank spaces or breaks to the rendered html element?
You could use HTML comments
<ul><!--
<?php foreach($users as $user){ ?>
--><li><!-- …
This is a technique also used to fight the space between inline block elements, if you want to keep the HTML code readable.
Or you could of course use output buffering, and then replace those spaces before you return the output to the client (rather complex, if you want to do it right.)
And probably the most simple solution of all to your actual problem:
Why output the ul element at all, if there are not going to be any list items?
Surround the whole block with an if that checks how many elements the array contains – and just don’t output any of that stuff if there aren’t any.
Do a test before :
<?php if (isset($users) && count($users) > 0){ ?>
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
<?php } ?>
With that way, <ul> won't be created if there's no users, so no empty spaces.
Don't tab your php statements:
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
PHP will remove everything starting at the <?php and ending at the ?> including the newline character. This is leaving the tab you have before your opening php tag.
You can break into a new line using:
echo "\n" //this will break into a new line
But pay atention that you must use double quotes. Single quotes won't work:
echo '\n' //this will output \n
The full solution will be
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } echo "\n" ?></ul>
I tested and it works. But if you are worried about future development and wan't to make things clear, I would use
if(isset($users)){?>
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
<?php }else{ //comment explaining that this prevents CSS selector ?>
<ul></ul>
<?php } ?>
I would go with this notation :
<ul><?php
foreach($users as $user){
?><li><?php echo $user->name ?></li><?php
}
?></ul>
It's still pretty readable, and HTML output contains no spaces that aren't needed, whether $users is empty or not!
I have created a multi option attribute so that I show an image for each option but i can not get it to work.
I have used this code from another post on here to get a list of the options to show.
The code I used is:
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
So this now shows a list of the options, one of on top of each other.
As I said I would like an image to be shown for each option.
I thought the best way would be to have divs and assign an image to each div.
I was hoping that I could get the output to be:
<div class="option1"></div>
<div class="option2"></div>
<div class="option3"></div>
<div class="option3"></div>
instead of the output that the code above has:
<ul>
<li>option1</li>
<li>option2</li>
<li>option3</li>
<li>option4</li>
</ul>
Change your code to
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<div class="<?php
$_comma = ",";
$_list = "\"></div><div class=\"";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</div>
<?php endif; ?>
I'm having trouble writing an if else statement. Considering that I've been doing so for years in ColdFusion, this make me feel very stupid.
Here's the task.
I need to pull first name, last name, email, co-chair status from a database and return the results. However not everyone has an email, so I need a statement that will include a mailto link for those that have emails, and exclude a mailto link for those that don't.
Here's the code I'm using that includes the mailto link for all.
What adjustments do I need to make?
thanks,
david
<?php do { ?>
<?php echo $row_GetMembers['BACmember_First']; ?> <?php echo $row_GetMembers['BACmember_Last']; ?>
<?php /*START_PHP_SIRFCIT*/ if ($row_GetMembers['BACmember_CoChair']=="Yes"){ ?>
<strong> Co-Chair</strong>
<?php } /*END_PHP_SIRFCIT*/ ?><br />
<?php } while ($row_GetMembers = mysql_fetch_assoc($GetMembers)); ?>
This is the line of code you want to optionally display as a link (split for readability):
<a href="mailto:<?php echo $row_GetMembers['BACmember_Email']; ?>">
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
</a>
You'll want something like this instead:
<?php if (!empty($row_GetMembers['BACmember_Email'])): ?>
<a href='mailto:<?php echo $row_GetMembers['BACmember_Email']?>>
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
</a>
<?php else: ?>
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
<?php endif; ?>
If the email field isn't empty (the ! negates the result, so we're checking if it isn't empty), it prints the first and last name inside an anchor tag. If it is empty, it prints the name without it.
A more elegant solution may be to provide the email address as a separate field or link, as opposed to having a list of some names that are links and some that aren't.
I'm using the following section of code to display some icons on our Magento store with the idea being if there is nothing added in the icons section it shouldn't display, for some reason this isn't working...it is displaying a division as if something is there but there is actually nothing.
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons') !== null):
?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->geticons()), 'icons') ?>
</div>
<?php endif; ?>
It needs to show Icons if they are coded in the attribute field and then hide the division if there is nothing added.
I've worked out that the code is returning a value of string(0) what do I need to change in my coding to get the desired effect?
Here's the thing you need, and you don't need call the same functionality twice to get the empty result. Define your variable and check if it is empty (null, undefined or false) or not
<?php $icons = $_helper->productAttribute($_product,($_product->getIcons()), 'icons');?>
<?php if(!empty($icons)):?>
<div class="product-icons">
<?php echo $icons;?>
</div>
<?php endif;?>
this could be even better solution as it wont call the helper unless there are icons defined but you first have to try it out on your codebase.
<?php if($_product->getIcons()):?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->getIcons()), 'icons') ?>
</div>
<?php endif; ?>
and please check if it isn't a typo and it really is:
$_product->geticons()
or should it be
$_product->getIcons()
Something like:
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons'))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>
would be better!
try
<?php
if(!empty($_helper->productAttribute($_product,($_product->geticons()), 'icons')))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>