PHP: Only show first word of a custom taxonomy - php

This is my code for displaying a taxonomy (WordPress):
<?php
$locations = get_the_terms($post->ID , 'location');
foreach ($locations as $location) {
echo '<div class="term-location">';
echo $location->name . ' ';
echo '</div>';
}
?>
How can I modify the code to only display the first word of the taxonomy? I've tried it using explode but I can't get it to work (since my php knowledge is limited).
Thanks in advance!

You can indeed use explode so you were on the right path!
Explode returns an array of strings. So you first use explode, and then select the first item.
$locations = get_the_terms($post->ID , 'location');
foreach ($locations as $location) {
echo '<div class="term-location">';
echo explode(' ', $location->name)[0] . ' ';
echo '</div>';
}
You can get more info on explode here, and an example on getting the first word here.

Related

separate meta keys on implode

<?php echo strip_tags(stripslashes($row = implode(['meta_keys']))) ?>
all it shows is "meta_keys" as display, and not the info from the database.
I'm trying to get each key individually linked on an <a href=""> tag.
I know I need to array, but any ideas?
$meta_keys = explode(',', $row['meta_keys']);
foreach ($meta_keys as $key) {
echo strip_tags($key) . ' ';
}

Get id value with PHP Simple HTML DOM

I have this scenario:
<div class="listing_title">
<strong>
<a href="http://www.mywebsite.com/dectails23291.html" id="url_id_1977">
Listing Title
</a>
</strong>
</div>
To get Listing Title, I have implemented this code:
$page = "http://www.mywebsite.com/listings.html";
$html = new simple_html_dom();
$html->load_file($pagina);
foreach($html->find('.listing_title') as $element)
echo $element->first_child()->plaintext . '<br>';
OUTPUT IS:
Listing Title
Now I need get id value
url_id_1977
preferably only "1977", clean of "url_id_", but I dont know do. Thanks in advance!!
Add this inside of your foreach loop:
echo end(explode('_', $element->find('a', 0)->id));
To get rid of the warning you could assign the id to a variable:
$id = explode('_', $element->find('a', 0)->id);
echo $id[2];
Or, if your anchor's id always starts with url_id_, just use str_replace():
echo str_replace('url_id_', '', $element->find('a', 0)->id);
try this
foreach($html->find('*[class=listing_title] a') as $element)
echo $element->id. '<br>';

while loop inside a while loop? Separating data from PHP

while($row = mysql_fetch_array($result))
//Template for each card in search result
{
echo '<div class="sleeve">';
echo '<div class="card ', $row['name'], '">';
echo '<div class="front face">';
echo '<img src="/', $row['cardset'], '/', $row['name'], $row['altart'], '.jpg"', ' alt="', $row['name'], '" />';
echo '</div>';
echo '<div class="back face">';
echo '<a id="name">', $row['name'], '</a><br/>';
echo '<form name="info" action="">';
echo '<select name="set">';
//while???
echo '<option value="Zendikar">', $row['sets'],'</option>';
echo '</select>';
echo 'Foil:<input type="checkbox" name="foil" value="true"/><br/>';
echo '<select name="condition">';
echo '<option value="Near Mint">Mint</option>';
echo '<option value="Played">Played</option>';
echo '<option value="Damaged">Damaged</option>';
echo '</select>';
echo 'Trade:<input type="checkbox" name="trade" value="true"/ <br/>';
echo '</form>';
echo '<b>Rulings:</b> <br/>';
echo $row['rulings'];
echo '</div>';
echo '</div>';
echo '</div>';
}
//while??? It might be hard to see in that mess of echoes, but there's a section that I have no idea how to deal with. PHP is grabbing rows of info, and populating them nicely. It fills the screen with little boxes of info.
In this section, that I don't know how to deal with, the data in one of the rows contains multiple things (thing1, thing2, thing3) separated by a (, ) each time. I need each of those things in a new thing1
So I feel like there would be another while loop inside each card?
You're on the right track with a loop. Try something like this, which explodes the string into an array, based on the comma delimiter, with explode():
echo '<select name="set">';
foreach( explode( ',', $row['sets']) as $item)
echo '<option>', $item, '</option>';
echo '</select>';
You probably need a foreach statement there after exploding the String into an array:
instead of the //while line and the following one:
foreach (explode(',', $row['sets']) as $value)
echo '<option value="', $value, '">', $value,'</option>';
I guess you may actually have another value for each row (one to be displayed, the other one is the actual value you want to set), but then the String would look much more like "(key1=value1, key2=value2)" and then you need a little more work, but you get the idea.
Hope this helps.
Yes, you would need to first explode that row into an array
$list_of_things = explode(",", $row['whatever']);
and then use a while, or a foreach:
$thing_options = '';
foreach($list_of_things as $thing)
$thing_options .= "<option>$thing</option>";
You might also find the here document syntax useful:
print <<<TEMPLATE
<div class="sleeve">
<div class="card {$row['name']}">
<div class="front face">
<img src="/{$row['cardset']}/{$row['name']}{$row['altart']}.jpg"
alt="{$row['name']}" />
</div>
<div class="back face">
<a id="name">{$row['name']}</a>
<br/>
<form name="info" action="">
<select name="set">
{$thing_options}
<option value="Zendikar">{$row['sets']}</option>
</select>
...
TEMPLATE;
While all of the answers telling you to explode() the array are correct, I can't help but think that having a db column filled with comma separated values are a symptom that your database is not normalized. You should check out the following link for an introduction to db normalization: http://mikehillyer.com/articles/an-introduction-to-database-normalization/
I'd also recommend not echoing out HTML. Ideally, your PHP scripts should follow this pattern:
All PHP processing up front, including database queries and form handling. Results should be stored in variables.
|
|
|
V
Almost pure HTML template/view, with just enough display logic (if/else, loops, echo) to actually display the results you stored in the variables from step 1.
You'll find that debugging/editing is a lot simpler if you let PHP be the brains and HTML be the beauty. Just like how markup and styles should be separate, the same goes for scripting and display.
You can use PHP's explode-method to split the comma separated string into its values, and then handle that array in a foreach loop:
$raw = "one,two,three";
$values = explode("," $raw);
foreach($values as $value) {
echo $value;
}

Drupal: Views - Print taxonomy term description in header

I am using the following code to print the taxonomy term in the views page header.
<?php
$view = views_get_current_view();
$term_name = array_pop($view->args);
$term_name = str_replace('-', ' ', $term_name);
$possible_terms = taxonomy_get_term_by_name($term_name);
$term = $possible_terms[0];
print '<div class="term-desc">';
print filter_xss_admin($term->description);
print '</div>';
?>
The issue I'm having is that it works with all of the terms that have more than one word, but on the terms with only one word for the term name, it won't print the description.
Try
$term = array_pop($possible_terms)
instead of
$possible_terms[0].
You can also try doing a
foreach($possible_terms as $key=>$term){
$desc = $term->description
}
If that doesn't help do a
var_dump($possible_terms);
to see your data structure.

How to rewrite this php function into HTML-emeddable function <?php ?>

Could someone convert this line of code to be readable by HTML?
echo '<h3>'. $r['title'] .'</h3>';
into something like this:
<?php echo...blah blah blah ?> /* To display the title in HTML */
I am sure I am not doing it right, that's why it's still not working :(.
Edit: There seems to be a confusion here. I am not going to modify the original php function. What I need to do is call it to my HTML page, to display the Title of the page
function r($text, $level = 3)
{
$tag = 'h' . $level . '>';
return '<' . $tag . $text . '</' . $tag;
}
Thanks for the downvote. The given question is totally unclear and constantly edited.
Ah you mean?
<php echo "<h3>$r['title']</h3>"; ?>
could be an answer to this unclear question
Save the result into a variable.
<?php $title = '<h3>'. $r['title'] .'</h3>';?>
<?php echo $title; ?>
Not exactly sure what you're asking, but you can't use PHP code within an HTML page.
The line
<?php echo '<h3>'. $r['title'] .'</h3>'; ?>
Within a PHP file, will print out the contents of $r['title'], within <h3> tags.
There is no function involved; $r is an associative array variable and title is a key to a particular value.

Categories