Need to add second line and third delimiter on php explode - php

This is tailing off my other question which was successfully answered: stackoverflow.com/questions/8597929/need-to-create-li-with-list-of-different-links-using-php-explode-method
so now I have this:
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
and that, using the following in a textarea which is defined to "my_custom_output":
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
and the result is
text1
text2
text3
which are successfully linked and styled. (i didnt make them links because stackoverflow doesn't let me post more than two links because i only have 3 rep).
So my next and final desired task is to do this:
text1
description 1
text2
description 2
text3
description 3
where the text1 etc are linked like before but the descriptions are not linked.
So I will do my best, right here in stackoverflow, to try it. However, I expect I will need some help. Let's go:
<?php
$separator1 = "\n";
$separator2 = ":";
$separator3 = ";";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
and to use the following in the textarea which is is defined to "my_custom_output":
text1:text1-page-url;Text1 Description
text2:new-text2-page;Description For Text2
text3:different-page-text3;A Text3 Description
and I need the output to be:
text1
Text1 Description
text2
Description For Text2
..etc
I don't know if semicolon will work, but I can't use a space (\s) because there are spaces in the description. I am open to suggestions.
====================================================
MY NEWEST TRY:
<?php
$separator1 = "\n";
$separator2 = ":";
$separator3 = ";";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$itemarray = explode($separator2, trim($item));
$item_text = $itemarray[0];
list($item_links, $item_desc) = explode($separator3,$itemarray[1]);
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
IT WORKS!!! =D

Not sure if I understand this well enough (I'm not sure what get_custom_field() gets you - can't find that as a regular PHP function), but when you explode an item that has multiple instances of the delimiter, you'll get multiple arrays.
So:
$textarea = "text1:text1-page-url;Text1 Description";
$data = explode(':',$textarea);
// at this point $data[0] will contain "text1", while $data[1] contains text1-page-url;Text1 Description"
$descarray = explode(';',$data[1]);
// then $descarray[0] contains "text1-page-url" and $descarray[1] contains "Text1 Description" so you can echo this out however you like.
To work with your code..
Assume each $item is each row at this point, like this:
$item = "text1:text1-page-url;Text1 Description";
Then this will do the trick:
foreach ($array as $item) {
$itemarray = explode($separator2, trim($item));
$item_text = $itemarray[0];
list($item_links, $item_desc) = explode(';',$itemarray[1]);
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}

I'd slightly optimize the algorithm as there is no need for second explode(). Just separate substrings within one row with the same separators (and don't forget to escape that separator inside all your data):
<?php
$row_separator = "\n";
$line_separator = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($row_separator, $textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links, $item_desc) = explode($line_separator, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a><br /><span class="desc-class">' . $item_desc . '</span></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And here is the data format I suggest to use (separate all fields by :)
text1:text1-page-url:Text1 Description
text2:new-text2-page:Description For Text2
text3:different-page-text3:A Text3 Description

Related

Breaking with a divider if more than one inside foreach

$linkPairs returns the following:
array (size=2)
0 => string 'Test|https://www.test2.com' (length=26)
1 => string 'Test2|http://www.test2.com/' (length=27)
How would I be successful on breaking the links using a divider as this: Test | Test2 .. the format that I'm getting now is TestTest2.
So I have the following code:
$linkPairs = explode(",", $atts['link']); // separate the pairs
$output = '';
$output .= '<li class="slide">
<p><img src="' . esc_url($atts['image']) . '" alt="" /></p>
<p>'. $atts['headline'] .'</p>
<p>'. $atts['body'] .'</p>';
foreach($linkPairs as $linkPair) {
$pair = explode("|", $linkPair); // separate the title and url
$output .= '' . $pair[0] . '';
}
$output .= '</li>';
One method to do it, would be add the links to an array, and implode them with what you wish to have separate them:
$output .= '<li class="slide">
<p><img src="' . esc_url($atts['image']) . '" alt="" /></p>
<p>'. $atts['headline'] .'</p>
<p>'. $atts['body'] .'</p>';
$hrefs = array();// define and clean for this item
foreach($linkPairs as $linkPair) {
$pair = explode("|", $linkPair); // separate the title and url
$hrefs[] = '' . $pair[0] . '';
}
$output .= implode(' | ',$hrefs);// implode them here
$output .= '</li>';
The imploded pipe, is not to be confused with the original pipe used in the raw data you work with. You mentioned you wanted to space your hrefs with a pipe, so that is the example above.
Update your foreach to add the divider, then rtrim() the last one
$divider = ' | ';
foreach($linkPairs as $linkPair) {
$pair = explode("|", $linkPair); // separate the title and url
$output .= '' . $pair[0] . ''.$divider;
}
$output = rtrim($output,$divider);
since it's good to know all possible ways you can also do the following count number of elements inthe linkPairs array then track the array pointer location to know the last link so that you don't add | to the last entry. I have explained everything i added in comment infront of the line.
$linkPairs = explode(",", $atts); // separate the pairs
$output = '';
$output .= '<li class="slide">
<p><img src="' . esc_url($atts['image']) . '" alt="" /></p>
<p>'. $atts['headline'] .'</p>
<p>'. $atts['body'] .'</p>';
$li=count($linkPairs);//number of element of array
$i=1;//track the current location of array_pointer
foreach($linkPairs as $linkPair) {
if($i!=$li){
$symbol="|";//add | if the link is not the last link
}
else {
$symbol="";//add nothing if the link is the last link
}
$pair = explode("|", $linkPair); // separate the title and url
$output .= '' . $pair[0] . ''.$symbol;//$symbol is used to add | to the link
$i++;
}
$output .= '</li>';

Show Different Divs based on query result

Here what I got in view
I want to show it as Separate divs based on vendor name
How can i Group the vandor names and Show in different divs ?
I don't know how the data is being stored/retrieved but here is how I might do such sorting - assuming the variable $rows is set contains an associative array of all rows (similar to the results of a basic database query) and assuming "vendor" is the column name for the vendors (TSZ, TSR, etc), "sku" is the name of the SKU column, and "store_price" is the name of the store price column:
<?php
$vendorData = [];
foreach( $rows as $row ) {
if( !isset($vendorData[$row['vendor']]) ) {
$vendorData[$row['vendor']] = '';
}
$vendorData[$row['vendor']] .= '<div class="sku">' .
$row['sku'] . '</div>' .
'<div class="price">' . $row['store_price'] . '</div>';
}
foreach( $vendorData as $vendorName => $vendorHTML ) {
echo '<div id="' . $vendorName . '" class="vendor">' .
'<h3 class="name">' . $vendorName . '</h3>' .
'<div class="data">' . $vendorHTML . '</div></div>';
}
?>
Here is thing what you need:-
$newDataRow = []
foreach($dataRows as $dataItemRow):
$newDataRow[$dataRows['vendor']][] = $dataItemRow;
endforeach;
$op = '';
foreach($newDataRow as $vandorName => $vandorItemData):
$op .= '<div class="vendor_item_group">';
$op .= '<div class="vendor_name">'.$vandorName.'</div>';
$op .= '<div class="item_group">';
foreach($vandorItemData as $record):
$op .= '<span>'.$record['sku'].'</span>';
$op .= '<span>'.$record['price'].'</span>';
endforeach;
$op .='</div>';
$op .= '</div>';
endforeach;
echo $op;
Output Should be like this:-
TSZ TSR
TSZ K377 TSR319
2300 2250
TSR319
2300

Print specific array value via function argument

$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $menu[$key] . '">' . $menu[$key] . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu[1]); ?>
What i basically want to do is to pass a specific array value when i'm echoing out the menu.
I'm building a single page website with anchors and i want to pass value's so i can echo out the "top"-link.
I'm stuck at the point on how to pass the $key value trough the function.
**edit: I'm trying to print specific links. I want a function that is able to print out an link but i want to specify the link to print via the function argument.
for example:
<?php echo main_menu($key = '0'); ?>
result:
prints url: top
<?php echo main_menu($key = '2'); ?>
result:
prints url: photography
**
(A lack of jargon makes it a bit harder to explain and even harder to google.
I got my books in front of me but this is taking a lot more time than it should.)
You either need to pass the entire array and loop, or pass a single array item and not loop:
Single Item:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
$return .= '<a class="menu" href="index.php#' . $menu . '">' . $menu . '</a>' . PHP_EOL .'';
$return .= '</div>';
return $return;
}
echo main_menu($menu[1]);
Entire Array:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach($menu as $value) {
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
echo main_menu($menu);
You don't need $menu[$key] just use the $value.
Should you not just be using $value inside your loop? And passing the entire array rather than one item of the $menu array?
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu); ?>
Try:
echo main_menu($menu); // You will get your links printed
Instead of
echo main_menu($menu[1]); // In this case error is occured like : **Invalid argument supplied for foreach**
NOTE: You can use $value instead of $menu[$key]

How do I add these php tags to the <ul> tag?

I am modifying a wordpress function. I have the following code:
$output .= $r['link_before'] . '<ul><li class="bbp-forum-info">' . $title . '</li>' . $counts . '<li class="bbp-forum-freshness">' . $subfresh . '</li></ul>' . $r['link_after'];
And I need the ul tag to look like this:
<ul id="bbp-forum-<?php bbp_forum_id(); ?>" <?php bbp_forum_class(); ?>>
However, I don't know how to add those php tags inside of that code. I have tried it in a number of different ways, but haven't been able to get it to work. Any advice?
use the . to concatenate strings:
$c='a'.'b';
echo $c //outputs 'ab'
knowing that, just:
$output .= $r['link_before'] . '<ul id="bbp-forum-'.bbp_forum_id().'" '.bbp_forum_class().'><li class="bbp-forum-info">' . $title . '</li>' . $counts . '<li class="bbp-forum-freshness">' . $subfresh . '</li></ul>' . $r['link_after'];
if you are adding it correctly
$output .= $r['link_before'] . '<ul id="bbp-forum-<?php bbp_forum_id(); ?>" <?php bbp_forum_class(); ?>><li class="bbp-forum-info">' . $title . '</li>' . $counts . '<li class="bbp-forum-freshness">' . $subfresh . '</li></ul>' . $r['link_after'];
This is hard to answer without further knowlege of the system.
ensure bbp_forum_class(); echoes something along the lines of class="foo"
if that isnt your issue, your style may be overridden by the li class. ensure you dont have conflicting css!

Need to create li with list of different links using php explode method

here is my working php explode code for NON links:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li>' . $item . '</li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
..and the code which defines "my_custom_output", which I input into my textarea field:
text1,text2,text3,etc
..and the finished product:
text1
text2
text3
etc
So that works.
Now what I want to do is make text1 be a link to mywebsite.com/text1-page-url/
I was able to get this far:
<?php
$textarea = get_custom_field('my_custom_output');
$array = explode(',',$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
$item = trim($item); // clear off white-space
$output .= '<li class="link-class"><a title="' . $item . '" href="http://mywebsite.com/' . $item_links . '">' . $item . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
Now, I would like $item_links to define just the rest of the url. For example:
I want to put input this into my textarea:
text1:text1-page-url,text2:new-text2-page,text3:different-page-text3
and have the output be this:
text1
text2
..etc (stackoverflow forced me to only have two links in this post because I only have 0 reputation)
another thing I want to do is change commas to new lines. I know the code for a new line is \n but I do not know how to swap it out. That way I can put this:
text1:text1-page-url
text2:new-text2-page
text3:different-page-text3
I hope I made this easy for you to understand. I am almost there, I am just stuck. Please help. Thank you!
Just split the $item inside your loop with explode():
<?php
$separator1 = "\n";
$separator2 = ":";
$textarea = get_custom_field('my_custom_output');
$array = explode($separator1,$textarea);
$output = ''; // initialize the variable
foreach ($array as $item) {
list($item_text, $item_links) = explode($separator2, trim($item));
$output .= '<li class="link-class"><a title="' . $item_text . '" href="http://mywebsite.com/' . $item_links . '">' . $item_text . '</a></li>';
}
?>
<ul>
<?php print $output; ?>
</ul>
And choose your string separators so $item_text, $item_links wouldn't contain them.
After you explode the string you could loop through again and separate the text and link into key/values. Something like...
$array_final = new array();
$array_temp = explode(',', $textarea);
foreach($array_temp as $item) {
list($text, $link) = explode(':', $item);
$array_final[$text] = $link;
}
foreach($array_final as $key => $value) {
echo '<li>'.$key.'</li>';
}
to change comma into new line you can use str_replace like
str_replace(",", "\r\n", $output)

Categories