ACF Fields - Array Loop - IF Statement - php

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>';

Related

Implode array list elements with indexing

I have an array list that I wish to implode as I want to display each array with indexing to target that element. I'm trying but it is not displaying correctly. What am I doing wrong?
<?php
/*
var_dump($names); // below
array(4) {
[0]=>
string(22) "David Kaul"
[1]=>
string(23) "Julius Kaul"
[2]=>
string(22) "Robert Kaul"
[3]=>
string(22) "Juohn Kaul"
} */
for ($i = 0; $i <= 10; $i+=1) {
echo '<li class="item-' . $i . '">' . implode('</li><li class="item-'.$i.'">', $names) . '</li>';
}
?>
I think you just want a simple foreach over the $names array:
$names = array("David Kaul","Julius Kaul","Robert Kaul","Juohn Kaul");
foreach ($names as $i => $name) {
echo '<li class="item-' . $i . '">' . $name. '</li>' . "\n";
}
Output:
<li class="item-0">David Kaul</li>
<li class="item-1">Julius Kaul</li>
<li class="item-2">Robert Kaul</li>
<li class="item-3">Juohn Kaul</li>
Demo on 3v4l.org

PHP output multiple variable values if they exist

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>';

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));

PHP Fetch array in table with individual styling

I have an array that i pull from a database that looks like this
id - name - shortlink - downloadurl - count - date
Now I want to display that array as a table so the administartor of the site can se the content of the database. For that, I'm using this code:
function build_table($array){
// start table
$html = '<table id="usertable">';
// header row
$html .= '<tr class="header">';
foreach($array[0] as $key=>$value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key=>$value){
$html .= '<tr>';
foreach($value as $key2=>$value2){
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
And it works great.
The problem is that i wan't do display some of the columns with a different code. E.g. 'downloadurl' which is an webadress, i want to make clickable. I just can't figure out how to split up the function so that i can write the code for the individual columns.
You could try something similar to the following:
using conditionals
This is probably the most straight forward way to go about handling different attributes per column - not without its drawbacks (noted below)
// data rows
foreach( $array as $key => $value) {
$html .= '<tr>';
foreach($value as $key2 => $value2) {
if ($key2 == 'downloadurl') {
$html .= '<td>Download</td>';
} else {
$html .= '<td>' . $value2 . '</td>';
}
}
$html .= '</tr>';
}
using switch statement
If you decide on this route, it may be a bit easier to manage in the long run as if () elseif () else() can become difficult to read over time.
foreach($value as $key2 => $value2) {
switch($key2) {
case 'downloadurl':
$html .= '<td>Download</td>';
break;
default:
$html .= '<td>' . $value2 . '</td>';
}
}
use this function to generate urls:
function getURL($downloadURL)
{
$html = "<a href = '$downloadURL'>Download</a>";
return $html;
}
And your function build_table():
function build_table($array){
// start table
$html = '<table id = "usertable">';
// header row
$html .= '<tr class = "header">';
foreach($array[0] as $key => $value){
$html .= '<th>' . $key . '</th>';
}
$html .= '</tr>';
// data rows
foreach( $array as $key => $value){
$html .= '<tr>';
foreach($value as $key2 => $value2){
$value2 = ($key2 == 'downloadurl') ? getURL($value2) : $value2;
$html .= '<td>' . $value2 . '</td>';
}
$html .= '</tr>';
}
// finish table and return it
$html .= '</table>';
return $html;
}

How can I echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.

Categories