What I have in my Mage site is:
echo "<div class='block-content'><ul>";
foreach($listeditems as $optionId => $value) {
echo "<li class='item'>" . $value . "</li>";
}
echo "</ul></div>";
But I want it to echo the matching URL instead of a list of search result items.
Related
i am using the following legacy code to pull post type GENRE and display the records on a wordpress site template file.
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0 ));
foreach ( $genre_ids as $genre_id ) {
$categories[] = get_the_title( $genre_id );
}
if (!empty($categories)){
foreach ($categories as $i=>$cat) {
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
} else {
print "<li>";
print "no categories";
print "</li>";
}
the script works now to say if there is any post type GENRE then run the query. but i want to edit it to check and show the GENRE only if there is a GENRE. right now the query is showing all categories even if there is no data. any help would be greatly appreciated.
Please try this code::
<?php
$genre_ids = get_posts('fields=ids&posts_per_page=-1&post_status=publish&post_type=genre&order=asc&orderby=title');
$choices = array(array('text' => 'Select Genre From List', 'value' => 0));
foreach ($genre_ids as $genre_id)
{
$categories[] = get_the_title($genre_id);
}
if (!empty($categories))
{
foreach ($categories as $i => $cat)
{
if (!empty($cat))
{
print "<li>";
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
print "</li>";
}
}
}
else
{
print "<li>";
print "no categories";
print "</li>";
}
?>
Try to replace this line:
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . $cat . "</a>";
With this line
print "<a href='" . get_site_url() . "/band-search-page/?cat=$cat'>" . array_values(array_filter($cat) . "</a>";
Just add array_filter($cat) to remove empty array elements.
Don't know if I understand you correctly, but I'm also working on a site where I only want to show the category name that has posts stored in it. I just checked it by using if(category_count > 0).
Well, you might want to set the category and call it using get_categories(), loop through it then you can do category->count to check for the count.
hope this might help... someone.. :)
I want generate a link using two array: the first one contains addresses; the second one contains text.
I want have:
- text3
- text3
- text3
to do so I tried like this but I can't generate texts.
<ul>
<?php
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
$nome=array_values($nomimenu);
foreach($vocimenu as $voce) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$voce.'">';
echo $nome;
if($page!=$voce) echo "</a>";
echo "</li>";
}
?>
</ul>
You can use one array
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$links=array("address1"=>"text1","address2"=>"text2","address3"=>"text3");
foreach($links as $href=>$text){
if($page!=$voce){
echo ''.$text.'';
}else{
echo $text;
}
}
This should work:
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
//since you're using two arrays, foreach is not the way to go
//you need a counter so you can get elements from each array
for ($i=0;$i<count($vocimenu);$i++) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$vocimenu[$i].'">';
echo $nomimenu[$i];
if($page!=$voce) echo "</a>";
echo "</li>";
}
An alternate option is to do it like this, but that could make some of your other code less flexible:
$array = array("address1"=>"value1","address2"=>"value2",...);
foreach($array as $address=>$value){
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$address.'">';
echo $value;
if($page!=$voce) echo "</a>";
echo "</li>";
}
It would be much easier if you create an associative array:
$menu = array(
"fmp_trama" => "Full Metal Panic!",
"fumoffu_trama" => "Full Metal Panic? Fumoffu",
"fmp_tsr" => "Full Metal Panic! TSR"
);
echo '<ul>';
foreach ($menu as $key => $value) {
echo "<li>";
if($page != $key) {
echo sprintf('%s', $key, $value);
}
else {
echo sprintf('<span>%s</span>', $value);
}
echo "</li>";
}
echo '</ul>';
You can build the array like this: (if you are bound to the 2 array structure)
$menu = array_combine($vocimenu, $nomimenu);
Needed Navigation Html
Home
Pages
About
Services
Products
Contact
FAQs
Sitemap
Privacy Policy
Column Layouts
1 Column
2 Column (Left Sidebar)
2 Column (Right Sidebar)
3 Column
4 Column
I want to use php arrays and foreach loops to output the needed html.
The php code I have thus far is:
<?php
$data = array("navigation");
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
$data['navigation']['Pages']['About'] = base_url('pages/about');
echo '<ul>';
foreach($data as $nav) {
foreach($nav as $subNavKey => $subNavHref) {
echo "<li><a href='$subNavHref'>$subNavKey</a>";
}
}
echo '</ul>';
?>
I was thinking I would need three foreach loops nested but php warnings/errors are generated when the third loop is reached on lines such as:
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
I'm not quite sure how to test for 3rd level depths such as:
$data['navigation']['Pages']['About'] = base_url('pages/about');
Also, outputting the needed li and ul tags in the proper positions has given me trouble aswell.
Use recursion
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
$data['navigation']['Pages']['About'] = base_url('pages/about');
$data['navigation']['Pages']['About']['Team'] = base_url('pages/team');
$data['navigation']['Pages']['About']['Team']['Nate'] = base_url('pages/nate');
echo "<ul>"
print_list($data);
echo "</ul>"
function print_list($menu) {
foreach($menu as $key=>$item) {
echo "<li>";
if(is_array($item)) {
echo "<ul>";
print_list($item);
echo "</ul>";
} else {
echo "<a href='{$val}'>$key</a>";
}
echo "</li>";
}
}
<?php
function nav($data) {
$html = '<ul>';
foreach ($data as $k => $v) {
if (is_array($v)) {
$html .= "<li>$k" . nav($v) . "</li>";
}
else {
$html .= "<li><a href='$k'>$v</a>";
}
}
$html .= '</ul>';
return $html;
}
echo nav($data);
A recursive function can get the job done:
$items = array(
"Home",
"Pages" => array(
"About",
"Services",
"Products",
"Contact",
"FAQs",
"Sitemap",
"Privacy Policy",
"Column Layouts" => array(
"1 Column",
"2 Column (Left Sidebar)",
"2 Column (Right Sidebar)",
"3 Column",
"4 Column"
)
)
);
function getMenu($array) {
foreach($array as $key => $value) {
if(is_array($value)) {
echo "<li>" . $key . "</li>";
echo "<ul>";
getMenu($value);
echo "</ul>";
} else {
echo "<li>" . $value . "</li>";
}
}
}
echo "<ul>";
getMenu($items);
echo "</ul>";
Output:
You should use a recursive function, for example (Working Demo):
function makeMenu($array)
{
$menu = '';
foreach($array as $key => $value) {
if(is_array($value)) {
$menu .= '<li>' . $key . '<ul>' . makeMenu($value) . '</ul></li>';
}
else {
$menu .= "<li><a href='". $value ."'>" . $value ."</a></li>";
}
}
return $menu;
}
Then call it like:
$data = array(
"Home",
"Pages" => array("About", "Services"),
"Column Layouts" => array("1 Column", "2 Column (Left Sidebar)")
);
echo '<ul>' . makeMenu($data) . '</ul>';
We have a client who wants to output cinema listings on their website. This particular cinema has provided us with a link to retrieve the information from. This link is simply outputted in plain text, with the elements seperated by ~|~
I am using explode and strpos to divide the listings into divs however it still isn't good enough for what I am wanting to achieve. I just can't get my head around what I can do to format this correctly. This is my code:
<?php
$theArray = explode("~|~", file_get_contents("THE URL"));
echo "<ul id=\"cinemaListings\">";
foreach($theArray as $item){
$findcinemaid = '~77';
$findcinemaname = 'Cinema Name';
$findx = 'X';
$findu = 'U';
$findgen = 'Gen';
$findlink = '.ie';
if(strpos($item, $findcinemaname) !== false){
echo "<div class='cinemaName'>" . $item . "</div>";
}elseif(strpos($item, $findgen) !== false){
echo "<div class='cinemaGen'>" . $item . "</div>";
}elseif(strpos($item, $findcinemaid) !== false){
echo "<li>";
}elseif($item == $findx){
echo "<div class='cinemaX'>" . $item . "</div>";
}elseif(strlen($item) == 8){
echo "<div class='cinemaCode'>" . $item . "</div>";
}elseif(strlen($item) == 3){
echo "<div class='cinemaListId'>" . $item . "</div>";
}elseif(strlen($item) == 2){
echo "<div class='cinemaListId'>" . $item . "</div>";
}elseif(strlen($item) == 1){
echo "<div class='cinemaListId'>" . $item . "</div>";
}elseif(DateTime::createFromFormat('H:i', $item) !== FALSE){;
echo "<div class='cinemaTime'>" . $item . "</div>";
}elseif(strpos($item, $findlink) !== false){
echo "<div class='cinemaLink'>" . $item . "</div>";
}elseif(DateTime::createFromFormat('d/m/Y', $item) !== FALSE){;
echo "<div class='cinemaDate'>" . $item . "</div>";
}else {
echo "<div>" . $item . "</div>";
}
}
echo "</ul>";
?>
This script however is outputting (with divs and classes of course, just don't want to add them all):
<li>The Smurfs 2 2D 30/08/2013 11:10</li>
<li>The Smurfs 2 2D 30/08/2013 13:20</li>
<li>The Smurfs 2 2D 31/08/2013 11:10</li>
<li>The Smurfs 2 2D 31/08/2013 13:20</li>
<li>Elysium 04/09/2013 15:45</li>
<li>Elysium 04/09/2013 21:00</li>
<li>Elysium 05/09/2013 15:45</li>
Is there any possible way to format it as follows:
<li>
The Smurfs 2 2D
30/08/2013 (today's date)
11:10 13:20
</li>
<li>
Elysium
04/09/2013 (today's date)
15:45 21:00
</li>
So just one film name, todays date and the times for that day (with classes ofcourse). Then onto the next film... etc.
I'm stuck and would appreciate any help! Thanks in advance.
Are you talking about the source layout or the front end html layout? If it's the source layout, have you tried replacing this:
echo "<div class='cinemaName'>" . $item . "</div>";
With this(?):
echo "<div class='cinemaName'>" . preg_replace( ' ', "\r\n", $item ) . "</div>";
Since it looks like there are 3 spaces between each segment of the $item, you can replace them with return shorthand \r\n (or just \n for some systems).
If it's the front end layout, just use the same method, but instead of \r\n, change it to <br/>.
I have an array coming from a .csv file. These are coming from a real estate program. In the second column I have the words For Sale and in the third column the words For Rent that are indicated only on the rows that are concerned. Otherwise the cell is empty. I want to display a list rows only For Sale for example. Of course then if the user clicks on a link in one of the rows, the appropriate page will be displayed.
I can't seem to target the text in the column, and I can't permit that the words For Sale be used throughout the entire array because they could appear in another column (description for example).
I have tried this, but to no avail.
/* The array is $arrCSV */
foreach($arrCSV as $book) {
if($book[1] === For Sale) {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
I also tried this:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div>';
}
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
Do you mean:
foreach($arrCSV as $key => $book) {
if($book['1'] == 'For Sale') {
echo '<div></div>';
}else{
echo '<div>';
echo $book[0]. '<br>';
echo $book[1]. '<br>';
echo $book[2]. '<br>';
echo $book[3]. '<br>';
echo $book[6]. '<br><br><br>';
echo '</div>';
}
}
I found a solution here:
PHP: Taking Array (CSV) And Intelligently Returning Information
$searchCity = 'For Sale'; //or whatever you are looking for
$file = file_get_contents('annonces.csv');
$results = array();
$lines = explode("\n",$file);
//use any line delim as the 1st param,
//im deciding on \n but idk how your file is encoded
foreach($lines as $line){
//split the line
$col = explode(";",$line);
//and you know city is the 3rd element
if(trim($col[1]) == $searchCity){
$results[] = $col;
}
}
And then:
foreach($results as $Rockband)
{
echo "<tr>";
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo "</tr>";
}
As you can see I'm learning. It turns out that by formulating a question, a series of similar posts are displayed, which is much quicker and easier than using google. Thanks.