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.
I am trying to include a piece of text from a config file into a php file that generates a random number of tags from a seperate list of tags.
Code to generate the tags :
<?php
include '../config/filenames.php';
$keywords = explode(",", file_get_contents("taglist.php"));
shuffle($keywords);
$keys = array();
foreach (array_slice($keywords, 0, 20) as $word) {
$word = trim($word);
$keys[] = $word;
}
echo join(',', $keys);
?>
This is the taglist :
<a href='<?php echo $filename_a?>'>test</a>,
<a href='<?php echo $filename_a?>'>test2</a>,
<a href='<?php echo $filename_a?>'>test3</a>,
<a href='<?php echo $filename_a?>'>test4</a>
For some reason it is not including the $filename_a. It looks like the result of the random data is not parsing the php, but simply printing the piece of php code rather then the result of it.
Can someone help ?
I have the following piece of HTML in a PHP app with an array including Group and Items. First I want to sort them by Groups, then within Groups have Items separated by comma (Item1, Item2, Item3), without ending comma.
<dl>
<?php $groupname = '' ?>
<?php foreach ($product['product_filters'] as $product_filter) { ?>
<?php if ($groupname != $product_filter['group']) { ?>
<?php $groupname = $product_filter['group']; ?>
<?php echo '<dd>' . $product_filter['group'] . '</dd>'; ?>
<?php } ?>
<dt>
<?php echo $product_filter['name']; ?>
</dt>
<?php } ?>
</dl>
I want to have a the following result, but I don't know how to manage it and which loop should I use:
Group 1
G1_Item_1, G1_Item_2
Group 2
G2_Item_1, G2_Item_2, G2_Item_3
You could use two loops: one to restructure your data into groups, and one just for outputting it in the desired format. Note that you used <dt> and <dd> in the opposite sense: the groups are the titles, so use <dt> for them.
Also, your code becomes much more readable if you don't open and close the php tag on every line. Try to make code blocks that don't have such interruption: it will make it so more readable.
Here is the suggested code:
<?php
// Create a new structure ($groups): one entry per group, keyed by group name
// with as value the array of names:
foreach ($product['product_filters'] as $product_filter) {
$groups[$product_filter['group']][] = $product_filter['name'];
}
// Sort it by group name (the key)
ksort($groups);
// Print the new structure, using implode to comma-separate the names
foreach ($groups as $group => $names) {
echo "<dt>$group</dt><dd>" . implode(', ', $names) . "</dd>";
}
?>
See it run on eval.in.
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($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;
}