PHP output multiple variable values if they exist - php

I have to output a list of multiple values. These values are not part of an array. I am doing it like this:
$value_1 = get_field('value_1');
$value_2 = get_field('value_2');
$value_3 = some_other_function('value_3');
$value_4 = another_function('value_4', 'one_more_param');
echo '<ul>';
if ($value_1) :
echo '<li>' . $value_1 . '</li>';
endif;
if ($value_2) :
echo '<li>' . $value_3 . '</li>';
endif;
if ($value_3) :
echo '<li>' . $value_3 . '</li>';
endif;
if ($value_4) :
echo '<li>' . $value_4 . '</li>';
endif;
echo '</ul>';
I have about 30 values. Is there a quicker, cleaner way to output this?

Collect your values to array and iterate over this array:
$values = [
get_field('value_1'),
get_field('value_2'),
some_other_function('value_3'),
another_function('value_4', 'one_more_param'),
];
echo '<ul>';
foreach ($values as $value) {
if ($value) {
echo '<li>' . $value . '</li>';
}
}
echo '</ul>';

Create a function to check if it needs display the item, then pass the value each time...
function displayListItem( $value ) {
if ( $value ) {
echo '<li>'.$value.'</li>';
}
}
echo '<ul>';
displayListItem(get_field('value_1'));
displayListItem(get_field('value_2'));
displayListItem(some_other_function('value_3'));
displayListItem(another_function('value_4', 'one_more_param'));
echo '</ul>';

Related

ACF Fields - Array Loop - IF Statement

I am using this code that returns an array:
$values = get_field('sp_specialism', $prev_id);var_dump($values);
The output of the array is:
array(3) { [0]=> string(4) "Asia" [1]=> string(6) "Canada" [2]=> string(9) "Caribbean" }
I am trying to loop through the array and output Asia Canada Caribbean as list items
if($values) { echo '<ul>'; foreach($values as $value) { echo '<li>' . $value . '</li>'; } echo '</ul>'; }
However my if statement is erroring out. Not sure why this happens.
I am using this within another piece of code like this:
$string = "<span class=\"bkcards-exp\">" . get_field('sp_location', $prev_id) . "</span><span class=\"bkcards-left\">" . if($values) { echo '<ul>'; foreach($values as $value) { echo '<li>' . $value . '</li>'; } echo '</ul>'; } . "</span>"
You can't concatenate an if statement with a string. Instead, you can divide the string into multiple different statements as shown in the following snippet.
$string = '<span class="bkcards-exp">' .
get_field('sp_location', $prev_id) .
'</span><span class="bkcards-left">';
if($values) {
$string .= '<ul>';
foreach($values as $value) {
$string .= '<li>' . $value . '</li>';
}
$string .= '</ul>';
}
$string .= '</span>';

In osclass, how display cities only with ads?

How can I display regions and their cities only with ads? And why I can't see numbers of ads? I would like use jQuery but I don't know how.
<?php
$aRegions = Region::newInstance()->getByCountry('AT');
if(count($aRegions) > 0 ) { ?>
<ul>
<?php
foreach($aRegions as $region) {
//print_r ($region);
echo "<li>";
//$region['pk_i_id'].
?>
<div class="accordionButton">
<a href="javascript:void()">
<?php echo $region['s_name']."\n"; ?>
</a>
<?php // echo "</em>(". $region['items'].")</em>";?>
</div>
<?php
$aCities = City::newInstance()-> getByRegion($region['pk_i_id']);
if(count($aCities) > 0 ) {
echo "<div class=\"accordionContent\">";
echo "<ul>";
foreach($aCities as $city) {
// print_r ($city);
//$city["pk_i_id"].'
echo "<li>";
echo "<a href='". osc_search_url( array( 'sRegion'=>$region["s_name"], 'sCity' => $city['s_name'] ) ) ."'> ";
echo $city["s_name"]."\n";
echo "</em>(". $city['items'].")</em>";
echo "</a>";
echo "</li>";
}
}
echo "</ul>";
echo "</li>";
} ?>
</ul>
<?php
}
?>
The solution for cities is:
<?php if(osc_count_list_cities() > 0 ) { ?>
<ul>
<?php while(osc_has_list_cities() ) { ?>
<li><?php echo osc_list_city_name() ; ?> <em>(<?php echo osc_list_city_items() ; ?>)</em></li>
<?php } ?>
</ul>
<?php } ?>
Now it's tested and workes for cities
This will list all cities and their number of items.
The solution for regions > cities is:
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$locations[$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
echo '<ul>';
while(osc_has_list_regions() ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
Thanks! Your code is good! I need to rewrite it now for country display. This is my code but does not work properly, instead of showing German regions this code display austrian regions again:
<?php
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$locations[$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
?>
<ul>
<?php while(osc_has_countries() ) { ?>
<li><?php echo osc_country_name() ; ?> <em>(<?php echo osc_country_items() ; ?>)</em>
<?php if (osc_country_name() =='Austria') { ?>
<?php
echo '<ul>';
while(osc_has_list_regions('AT') ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
if (osc_country_name() =='Germany') { ?>
<?php
echo '<ul>';
while(osc_has_list_regions('DE') ) {
$region_id = osc_list_region_id();
echo '<li>' . osc_list_region_name() . '<em>(' . osc_list_region_items() . ')</em>' ;
echo '<ul>';
foreach($locations[$region_id] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul>';
}
}
} ?>
The solution for COUNTRIES > REGIONS > CITIES in Osclass 3.7.x is
$locations = array();
if(osc_count_list_cities() > 0 ) {
while(osc_has_list_cities() ) {
$city_id = osc_list_city_id();
$city = City::newInstance()->findByPrimaryKey($city_id);
$region_id = $city['fk_i_region_id'];
$country_code = strtolower($city['fk_c_country_code']);
$locations[$country_code][$region_id][$city_id] = array("cityurl"=>osc_list_city_url(), "cityname"=>osc_list_city_name(), "cityitems"=>osc_list_city_items());
}
$locationsRegions = array();
while(osc_has_list_regions() ) {
$region_id = osc_list_region_id();
$region = Region::newInstance()->findByPrimaryKey($region_id);
$country_code = strtolower($region['fk_c_country_code']);
$locationsRegions[$country_code][$region_id] = array("regionurl"=>osc_list_region_url(), "regionname"=>osc_list_region_name(), "regionitems"=>osc_list_region_items());
}
echo "<ul>";
while(osc_has_list_countries() ) {
$country_code = strtolower(osc_list_country_code());
echo '<li>' . osc_list_country_name() . '<em>(' . osc_list_country_items() . ')</em>' ;
echo '<ul>';
foreach($locationsRegions[$country_code] as $regionId => $aregion) {
echo '<li>';
echo '' . $aregion['regionname'] . '<em>(' . $aregion['regionitems'] . ')</em></br>' ;
echo '<ul>';
foreach($locations[$country_code][$regionId] as $acity) {
echo '<li>' . $acity['cityname'] . '<em>(' . $acity['cityitems'] . ')</em></li>' ;
}
echo '</ul></li><br/>';
}
echo '</ul></li><br/>';
}
echo '</ul>';
}

Array explode has empty value for html tag <li> how to not include that value

I have a random id generator which could be anywhere from 1 to x number of ids
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
I'm trying to go through each value and present it as an un-ordered list
<?php
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
$array = explode("|", $ranGen);
echo '<ul>' ;
foreach ($array as $value) {
echo '<li>' . trim($value) . '</li>';
}
echo '</ul>';
?>
I get the following results
<ul>
<li>IDENTIFIER </li>
<li> c0402347-8b93-49e4-991b-8213ea2921b1 </li>
<li> e8087fc5-ded7-43ab-858d-127fe23f90bc </li>
<li></li>
I do not want the last blank list - is there a way to do this
Test the value before you echo:
foreach ($array as $value) {
if( trim($value) != '') {
echo '<li>' . trim($value) . '</li>';
}
}
You have two options:
Check if the value is empty within the loop
Ignore the last part (if all of your input strings have that superfluous part at the end)
See this example:
<?php
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
$array = explode("|", $ranGen);
echo '<ul>';
foreach ($array as $value) {
// Option 1
if (trim($value) !== "") {
echo '<li>' . trim($value) . '</li>';
}
}
echo '</ul>';
// Option 2
$array = explode("|", $ranGen, -1);
echo '<ul>';
foreach ($array as $value) {
echo '<li>' . trim($value) . '</li>';
}
echo '</ul>';
?>
Replace this line : echo '<li>' . trim($value) . '</li>';
with this: echo trim($value) ? '<li>' . trim($value) . '</li>' : null;
Just filter it out:
$array = array_filter(explode("|", $ranGen));

How can I show only the first <a> in a loop and hide all other elements?

I am using a function
function recursiveArrayToList($array = array()){
echo '<ul>';
foreach ($array as $key => $value) {
echo '<li>' . $key . '';
if (is_array($value)) {
recursiveArrayToList($value);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
that creates a ul list.
I want to hide all elements EXCEPT the first <a>. This should be displayed.
I used this solution to detect the first element:
function recursiveArrayToList($array = array()){
$first = true;
echo '<ul>';
foreach ($array as $key => $value) {
if ( $first )
{
echo "first element, that should be shown";
$first = false;
}
else
{
echo "all other elements that should be hidden";
}
echo '<li>' . $key . '';
if (is_array($value)) {
recursiveArrayToList($value);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
But now all my elements that do have children get the text "first element that should be shown" not only the first element as expected.
I made a mistake in my question, so to be very clear I will show here what I need:
<ul><---- Show only this element
<li>farm
<ul><---- Do not show
<li>animals</li>
<ul><---- Do not show
<li>horses</li>
<ul>
<li>fred</li>
<li>sam</li>
<li>alan</li>
<li>john</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
And here is my script:
<script>
$(window).load(function(){
$('li a').click(function (e) {
e.preventDefault();
var ullist = $(this).parent().children('ul:first');
ullist.slideToggle();
});
});
</script>
CSS
.hidden {
display: none;
}
PHP
$class = !$first ? "class='hidden'" : "";
echo '<li ' . $class . '>' . $key . '';
$first = false;
EDIT
Overall this is what you should have:
function recursiveArrayToList($array = array(), $first = true){
echo '<ul>';
foreach ($array as $key => $value) {
$class = !$first ? "class='hidden'" : "";
echo '<li ' . $class . '>' . $key . '';
$first = false;
if (is_array($value)) {
recursiveArrayToList($value, false);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
You can pass a parameter by reference that will indicate if it's the current first element:
function recursiveArrayToList($array = array(), &$first){
echo '<ul>';
foreach ($array as $key => $value) {
if ( $first )
{
echo "first element, that should be shown";
$first = false;
}
else
{
echo "all other elements that should be hidden";
}
echo '<li>' . $key . '';
if (is_array($value)) {
recursiveArrayToList($value, $first);
}
echo '</li>';
}
echo '</ul>';
}
$first = true;
echo recursiveArrayToList($array, $first);
Note that I passed $first by reference (hence the & before the parameter name) - This mean that changes in the value of this parameters inside the function will save to the original $first that was declared outside the function scope.
Alternative solution, use a default parameter value to the $first variable:
function recursiveArrayToList($array = array(), $first = true){
echo '<ul>';
foreach ($array as $key => $value) {
if ( $first )
{
echo "first element, that should be shown";
$first = false; // You can comment out this line in order to allow the display of all the first "<li>" on the first level
}
else
{
echo "all other elements that should be hidden";
}
echo '<li>' . $key . '';
if (is_array($value)) {
recursiveArrayToList($value, false);
}
echo '</li>';
}
echo '</ul>';
}
echo recursiveArrayToList($array);
Now, when you're calling recursiveArrayToList on the first time, the $first is true by default, and the recursive call is passing false to prevent outputting the string.

How to add multiple ID in category

How to add in this code multiple ID category.
I have in kategorija_id 5 arrays:
1 - News
2 - Magazine
3 - Kokursi
4 - Grantovi ect..
I whant to display just key 1 and 2
when i add array i just print the first key
$kategorija = Kategorija::model()->findByPk($item->kategorija_**id=1,2**);
Thnx :)
Code update:
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
<?php
$i = 0;
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ( $i % 3 == 0 && $i > 0 ) {
echo "</ul></div><div class='item'><ul class='thumbnails'>";
}
$imageUrl = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
if ( !file_exists($imageUrl) || empty($item->slika)) {
if ( $lastDefault == 0 ) {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket.png";
$lastDefault = 1;
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket_redv2.png";
$lastDefault = 0;
}
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
}
echo "<li class='span3 thmb-elem'>";
echo "<div class='thumbnail right-caption image'>";
echo "<a class='img-link' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><img class='img-class' src='$imagePath'></a>";
echo "</div>";
echo "<div class='caption caption-link'>";
echo "<div class='naziv'><img class='arrow-img' src='". Yii::app()->getBaseUrl() . "/themes/shadow_dancer/images/arrow.png'> " . strtoupper($kategorija->naziv) . "</div>";
echo "<p><a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><strong>" . $item->naslov . "</strong></a></p>";
echo "</div>";
echo "</li>";
$i++;
}
?>
</ul>
</div>
</div>
I solve this problem adding this line of code
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
now code look like this and it works perfect.
<?php
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
echo '<li>';
echo '<img src="<?php echo Yii::app()->theme->baseUrl; ?>/images/infopaket/info_paket.png" />';
echo '<h2>';
echo strtoupper($kategorija->naziv);
echo '</h2>';
echo '<h1>';
echo "<a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'>";
echo $item->naslov;
echo '</h1>';
echo '</a>';
echo '</li>';
}
}
?>
findByPk() accepts first argument as array too. So you can do
category::model()->findByPk(array('1', '2'));
Also, $item->category_id will return only single entry. If your item has multiple categories, than set up relations to categories and then get categories ID's with
CHtml::listData($item->categoriesRelationName, 'id', 'id');
can use function ($data){return $data->attribtues} too ---------^--- to get all attributes of model

Categories