Echo a string with no spaces using PHP - php

I have a foreach loop with list of links, and I'm trying to remove the spaces in the strings because I'll be using it as an href. So far, I successfully removed the spaces, but the code I wrote made a wide space before the string. Here's my foreach loop
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant): ?>
<li><span class="tenant"><?php echo $ppmtenant['label'] ?><a href="<?php echo $this->getURL() ?>?___store=
<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>"></a></span></li>
<?php endforeach; ?>
</ul>
And below is what I did to remove the spaces. I used preg_replace. Now, the spaces are gone, but it has a wide space before the string.
<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>
Live example here: http://powerplantv2.jehzlau.net/brands.
Screenshot here: http://i.imgur.com/wpQwxso.png
As you can see, if you hover on the images, there's an unwanted space after the ?. There should be no space so that my permalinks will work. If that space disappears, my problem will be solved. But I don't know how to make it disappear. I hope a php expert here can help me. :(

The problem is comming from the spaces in your html. To fix, simply move the variable declaration of $ppmtenantnospace above the li output, then echo it in at the correct place:
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant):
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);?>
<li><span class="tenant"><?php echo $ppmtenant['label'] ?></span></li>
<?php endforeach; ?>
</ul>

The whitespace is actually in your HTML code
You should open the php tag right after =
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant): ?>
<li><span class="tenant"><?php echo $ppmtenant['label'] ?><a href="<?php echo $this->getURL() ?>?___store=<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>"></a></span></li>
<?php endforeach; ?>
</ul>

Try opening the PHP tag before the newline in your Code
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant): ?>
<li><span class="tenant"><?php echo $ppmtenant['label'] ?><a href="<?php echo $this->getURL() ?>?___store=<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>"></a></span></li>
<?php endforeach; ?>
</ul>

have you tried to stick your <?php to your ?___store=? because the 4 spaces before the <?php are not ignored since they are not PHP code but HTML output

$spaceless_string = strtr($stringwithspaces, array(' ' => ''));

Related

Working with multiple breadcrumbs in a knowledge base

I am working with Heroic Knowledge Base, and I am having trouble getting only one breadcrumb to show up (their suggested CSS code is not working). Unfortunately, I am not a PHP expert, and I am not sure how to edit the code so that it only shows one of the lines that it is pulling (we don't care which one) I have added the PHP from their breadcrumbs file. Any assistance would be greatly appreciated.
<?php
/**
* Breadcrumbs template
*/
?>
<?php if(hkb_show_knowledgebase_breadcrumbs()): ?>
<!-- .hkb-breadcrumbs -->
<?php $breadcrumbs_paths = ht_kb_get_ancestors(); ?>
<?php foreach ($breadcrumbs_paths as $index => $paths): ?>
<ol class="hkb-breadcrumbs" itemscope
itemtype="http://schema.org/BreadcrumbList">
<?php $last_item_index = count($paths)-1; ?>
<?php foreach ($paths as $key => $component): ?>
<li itemprop="itemListElement" itemscope
itemtype="http://schema.org/ListItem">
<?php if($key==$last_item_index): ?>
<span itemprop="item">
<span itemprop="name"><?php echo
$component['label']; ?></span>
</span>
<?php else: ?>
<a itemprop="item" href="<?php echo $component['link']; ?>">
<span itemprop="name"><?php echo $component['label']; ?></span>
</a>
<?php endif; ?>
<meta itemprop="position" content="<?php echo $key+1; ?>" />
</li>
<?php endforeach; ?>
</ol>
<?php endforeach; ?>
I was able to solve this issue using the following code, however I would like to make it so that it is only effective when there is more than one.. the code below worked on articles with two breadcrumbs, but when there is only one it hid them all together.
ol.hkb-breadcrumbs {
display: table-column-group;
}

Display MySQL request in PHP

I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help
At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.
When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>

Remove new lines after echoing HTML from PHP

I have a Wordpress function that checks if a user has filled in some fields on his or her profile. If it is filled in, echo its contents, if not: don't. Here is a stripped down version of the fun
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
Website
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
Twitter
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
Instagram
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
LinkedIn
</li>
<?php endif; // End check for linkedin ?>
</ul>
This works well. However, for layout purposes I am using inline on these elements and I can't use floats. As you may know, this causes "gaps" in between the elements because they are all echoed on new lines.
I don't want these gaps. I know I can solve it by an arbitrary negative margin, but I don't want to use it because I am using a responsive, percentage based layout. The solution would be to "fill the gaps" in HTML by glueing all HTML together without new lines. As an example:
Will create gaps when displayed inline:
<ul>
<li>Twitter</li>
<li>Instagram</li>
<li>LinkedIn</li>
</ul>
Won't have gaps:
<ul>
<li>Twitter</li><li>Instagram</li><li>LinkedIn</li>
</ul>
I am looking for a PHP solution (no CSS) that can get rid of all the new lines inside the ul, but I don't know where to start. Or, more precisely, I don't know where to put the function that removes the new lines. How can I wrap all that HTML, and then replace the new lines by ... nothing?
Have you tried something like:
$string = str_replace("\n", "", $string);
or:
$string = str_replace("\n\r", "", $string);
(where $string is the html you want to get on one line)
<ul class="author-meta">
<?php
if (get_the_author_meta('url', $theID)) {
$output = '<li class="url">Website</li>';
}
if ( get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter">Twitter</li>';
}
if ( get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram">Instagram</li>';
}
if ( get_the_author_meta('linkedin', $theID)) {
$output .= '<li class="linkedin">LinkedIn</li>';
}
echo $output;
?>
</ul>
Edit by Bram Vanroy: make sure to use get_the_author_meta instead of the_author_meta when using the function in PHP.
You can do something like this:
ob_start();
//echo whatever stuff you need
echo str_replace(PHP_EOL, NULL, ob_get_clean());
Using PHP_EOL instead of \n or \r\n will catch line breaks regardless of platform.
You can use buffer for that :
<?php
function remove_white_spaces($buffer){
return trim(preg_replace('/\s\s+/', ' ', $buffer));
}
ob_start('remove_white_spaces');
?>
<ul>
<?php if (get_the_author_meta('url',$theID)) : ?>
<li class="url">
Website
</li>
<?php endif; // End check for url ?>
<?php if ( get_the_author_meta('twitter',$theID)) : ?>
<li class="twitter">
Twitter
</li>
<?php endif; // End check for twitter ?>
<?php if ( get_the_author_meta('instagram',$theID)) : ?>
<li class="instagram">
Instagram
</li>
<?php endif; // End check for instagram ?>
<?php if ( get_the_author_meta('linkedin',$theID)) : ?>
<li class="linkedin">
LinkedIn
</li>
<?php endif; // End check for linkedin ?>
</ul>
<?php ob_end_flush(); ?>
Catch all the output on a buffer, run a function to replace new lines with blanks, and release the buffer.
Just set your ul font-size to 0 and apply the size to your li
font-size: 15px; display: inline-block;
As per comment by #APAD1 and feedback by #CBRoe, I decided to go with the comment method. It works flawlessly, though the HTML output isn't pretty (empty comments, byebye semantics).
<ul class="author-meta">
<?php if (get_the_author_meta('url',$theID)) : ?><li class="url">
Website
</li><?php endif; // End check for url ?><!--
--><?php if ( get_the_author_meta('twitter',$theID)) : ?><li class="twitter">
Twitter
</li><?php endif; // End check for twitter ?><!--
--><?php if ( get_the_author_meta('instagram',$theID)) : ?><li class="instagram">
Instagram
</li><?php endif; // End check for instagram ?><!--
--><?php if ( get_the_author_meta('linkedin',$theID)) : ?><li class="linkedin">
LinkedIn
</li><?php endif; // End check for linkedin ?>
</ul>
Note that the comments should be outside the if clauses, because if they are inside they might not be echoed corretly, e.g. missing a opening or closing tag.
Here you go:
<ul><?php
$output = '';
if (get_the_author_meta('url', $theID)) {
$output .= '<li class="url">Website</li>';
} elseif (get_the_author_meta('twitter', $theID)) {
$output .= '<li class="twitter">Twitter</li>';
} elseif (get_the_author_meta('instagram', $theID)) {
$output .= '<li class="instagram">Instagram</li>';
} elseif (get_the_author_meta('linkedin',$theID)) {
$output .= '<li class="linkedin">LinkedIn</li>';
}
echo $output;
?></ul>
To make an easy to use version, you can store all your websites data in an array:
$site=Array(
"url"=>"Website",
"twitter"=> "Twitter",
"instagram"=> "Instagram",
"linkedin"=>"LinkedIn"
);
And then generating the whole list. I used a simple sample case where all domains are .com. You will have to improve the array format if you want more information to be stored.
<ul><?php
foreach($site as $k=>$v)
if (get_the_author_meta($k,$theID))
echo '<li class="'.$k.'"><a href="'
.(($k=="url")?"":"http://$k.com/")
.the_author_meta($k,$theID).'">'.$v.'</a></li>';
?></ul>
Don't forget to remove your empty title attribute in your a tags.
Note that there are others CSS alternatives: using display:table on your ul and display:table-cell on your li, or using display: flex and so on.

Loop with a Loop including an explode()

I have an associative array returning a SQL query that pulls questions from my database. The tags field is stored as a delimited text VarChar. I want to loop through all of my database entries while using explode to create an array and then loop through the tag array to create a UL with the tags as well unfortunately it has not worked very well. This is what I have so far:
<?php foreach($questionRow as $questionShow) { ?>
<?php echo ($questionShow['netvotes']) ;?>
<?php echo ($questionShow['views']) ;?>
<?php echo ($questionShow['q_answer_count']) ;?>
<?php echo ($questionShow['title']) ;?>
Tags:
<ul style="display: inline">
<?php
$tagname = explode(",",$questionShow['tags']);
foreach ($tagname as $tagList) { ?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo($tagList) ;?></li>
<?php }; ?>
</ul>
<?php }; ?>
The results were very strange
I also tried by using the below to run my outer loop
<?php while($questionShow =$questionResult>fetch_assoc() ) { ?>
That caused the main recordset results to not actually loop. Hopefully I have explained this properly and someone could help. Thank you in advance!
well this could be very simple:
<?php
$query = mysql_query("SELECT * FROM `questions` LIMIT 30"); // replace this with your query
while($data=mysql_fetch_array($query)){
echo $data['netvotes'];
echo $data['views'];
echo $data['q_answer_count'];
echo $data['title']) ;?>
?>
Tags:
<ul style="display: inline">
<?php
foreach(explode(",",$data['tags']) as $tag){
?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo $tag; ?></li>
<?php
}
?>
</ul>
<?php
}
?>

hidden div not working correctly in for loop using a modal box

I have a mysql select and it spits out all the results in a <ul> that I have setup in a for loop. This works great. However, I wanted to have a "click more" type link at the bottom of each result that opens in a modal box when clicked. I also got this working just fine. What doesn't work, however, is the data inside what is displayed inside the modal box. It displays the data correctly but it is from the first result no matter what result you click. I thought I just didn't have my for loop setup correctly but it works when I change the id of the div the content is in for the modal box (but then the modal box doesn't work obviously).
Is there some reason why hiding a div would mess a for loop up as far as which row to display?
Below is the for loop
for($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);
?>
<div id='basic-modal'>
<ul style="background-color:#F5F5F5; padding:2px;">
<li class="program" style="font-size:18px;"><strong><?php echo trim ($row["program"]); ?></strong></li>
<li><strong>Sponsored by:</strong> <i><?php echo trim ($row["organization"]); ?></i></li>
<li><strong>Discipline:</strong> <?php echo trim ($row["discipline"]); ?></li>
<li><strong>Mission:</strong> <?php echo trim ($row["mission"]); ?></li>
<li><strong>Description:</strong> <?php echo trim ($row["content"]); ?></li>
<li><strong>Grade(s):</strong> <?php echo trim ($row["grade"]); ?></li>
<li><strong>Cost:</strong> <?php echo trim ($row["cost"]); ?></li>
<li> </li>
<li>Contact Info</li>
</ul>
<br />
<br />
</div>
<!-- pop up contact info -->
<div id="basic-modal-content">
<ul style="font-size:12px">
<li class="program" style="font-size:18px;"><strong><?php echo trim ($row["program"]); ?></strong></li>
<li><strong>Sponsored by:</strong> <i><?php echo trim ($row["organization"]); ?></i></li>
<li><strong>Contact Person:</strong> <?php echo trim ($row["contact"]); ?></li>
<li><strong>Contact Email:</strong> <?php echo trim ($row["email"]); ?></li>
<li><strong>Contact Phone:</strong> <?php echo trim ($row["phone"]); ?></li>
<li><strong>Contact Hours:</strong> <?php echo trim ($row["contactHours"]); ?></li>
</ul>
</div>
<!-- preload the images -->
<div style='display:none'>
<img src='images/x.png' alt='' />
</div>
<?php
}
?>
I'm using Eric Martin's SimpleModal if that makes a difference.
Thanks for the help.
You have same id of each div it is wrong

Categories