Start index numbering at 1 (not 0) with $key - php

I've got a wordpress shortcode, giving me an array of ids to build a list of links. Everything's fine so far. But now I'd like to number these links, so I use $key to output the index. Unfortunately numbering (within the span tag) starts with zero (0) and I can't figure a way to add + 1 to $key :(
function wp_ytlinks($atts){
extract(shortcode_atts(array( 'id' => 1 ), $atts));
echo '<div class="videonav">';
$value = explode(',', $id);
$index = 0;
foreach ($value as $key=>$att_id)
{
echo '<a class="yt-vid" id="yt-load" href="http://www.youtube.com/embed/' .
$att_id . '?rel=0" target="screen"><span>' . $key . '</span></a>';
}
};
add_shortcode('ytlinks', 'wp_ytlinks');
The Shortcode looks like this: [ytlinks id="J0Np2hn84Tc,cVHGLdZQgEw"]
Can s.o. please help?

OKay, I don't know, after an hour of stupid back and forth I tried something REALLY easy and it worked...
foreach ($value as $key=>$att_id) {
$nums = $key + 1;
...
Why do I have to post a question here to find such an easy solution?! Sorry guys...

Related

Pulling in fields through an array

I'm using a wordpress plugin names "Advanced Custom Fields".
Within the admin I have a select field where multiple choices can be selected.
So for example the admin will have the following selected....
/apples-category/ : Read More on apples
/pears-category/ : Read More on pears
/oranges-category/ : Read More on oranges
/peach-category/ : Read More on peaches
The first part which is the value (/apples-category/) will be used within a hyper link. The second part which is the label (Read More on apples) will be the anchor text.
I've put the following together which outputs the following correct labels but for some reason they all have the href (value) of the very last option (/peach-category/)
Read More on apples, Read More on pears, Read More on oranges, Read More on peaches
Here's my code and apologies if it looks obvious, I've just started out coding and still need assistance :)
Hope you can assist.
<?php
$labels = array();
$field = get_field_object('select_fruit');
$values = get_field('select_fruit');
foreach ($values as $value) {
$labels[] = $field['choices'][ $value ];
}
foreach ($labels as $k=>$label){
echo '<a href="'.$value.'" />'.strtoupper($label).'</a>'.($k == count($labels) - 1 ? '' : ', ');
}?>
Try this code:
<?php
$fruit_array = array();
$field = get_field_object('select_fruit');
if(!empty($field['value']))
{
foreach ($field['value'] as $value)
{
$fruit_array[$value]=$field['choices'][ $value ];
}
$cnt=0;
foreach ($fruit_array as $fruit_key=>$fruit_value)
{
echo '<a href="'.$fruit_key.'" />'.strtoupper($fruit_value).'</a>'.($cnt == count($fruit_array) - 1 ? '' : ', ');
$cnt++;
}
}
?>

Loop through foreach array

I'm having a problem looping through my array to get just one value each time. I want to gray only 1 value because I'm calling that value in jquery. I'm new and sorry if this is too basic but I've been trying for days, pulling my hair out!
$designslist = array('design1','design2','design3','design4');
$current_index = 0;
$current_id = current($designslist);
$next = $designslist[$current_index + 1];
foreach( $designslist as $value )
{
if( $value !== $next )continue;
$nexts = $next;
echo $nexts;
$current_index++;
}
Inside my html
next
calling the id with jquery
$('#design1').on('click',function() {
$('#web1').removeClass().addClass('big1');
});
foreach( $designslist as $value )
needs to be
foreach( $designslist as $i => $value )
$i is the array index
You can try array search
$tag = array_search($value, $designslist);
$nexts = $$designslist[$tag];
Ok, you seem to be mixing for with foreach using these "current_id" and "next" variables.
You can use this kind of foreach statement:
foreach ($designList as $key => $value) {
echo "The key of $value is $key";
}
As you want to return only unique values, you should use the "array_unique" function. Try this:
foreach (array_unique($designList) as $key => $value) {
echo "The key of $value is $key";
}
I know it's not related to the question, but, as you said you're new to this, there are some PHP tips you should follow.
You're mixing snake_case with camelCase in your variables. I recommend using camelCase, but it's up to you.

Updating and Sorting a Multidimensional PHP Array

I have a mySQL query that dumps into an array, is sorted and then displayed, and that works perfectly.
My problem is that I want to update these values, re-sort them and display. I've been able to update the values, but it's within the same for loop and so it doesn't re-sort. So then I close the loop and re-sort but it doesn't save the updates I made in the first loop. Help is much appreciated.
foreach ($arr as $mks){
if ($mks['Column1']==$Var) {$mks['Column2'] = $mks['Column2'] + 1;
}
My sorting code
foreach ($arr as $mks){
echo $mks['Column1'] . ", " . $mks['Column2'] . "<br>";
}
How do I get this to re-save into my array properly?! I've tried googling this for most of the morning but has lead to frustration.
I think this is what you need:
foreach ($arr as $key => $mks){
if ($mks[$key] == $Var) {
$arr[$key] = $mks+1;
}
}
Now, $arr will contain the modified array, and you can use / modify it further as you need.
I don't see any "sorting code" but you can try to use references "&" instead of copy vars in your foreach(s) :
foreach($arr as & $mks){
...
}
Instead of copying your cells it will to give you a reference and every modifications done on it will be saved in the array.
Try changing your array like this instead
foreach ($arr as $key => $mks){
if ($mks['Column1']==$Var) {$arr[$key]['Column2'] = $mks['Column2'] + 1;
}

Making A List Of Links With PHP

I would like to know how to make a list of links appear on my page displaying a name but when you click it, it navigates to the link.
I currently know how to make a list and display the items of it using the foreach command and arrays but is there a way I can make it so I have an array, containing an array, containing the name of the link and the link itself, like so:
$links = array(array("Google","//google.co.uk"),array("Bing","//bing.co.uk"))
foreach ($links as $myurl){
foreach ($myurl as $url){
echo "<a href='".$url."'>".$myurl."</a>";
}};
I know the above doesn't work but if anyone can help with this problem, it is much appreciated.
$links = array('Google' => 'www.google.com', 'Yahoo' => 'www.yahoo.com');
foreach($links as $k => $v) {
echo '' . $v . '';
}
As you can see I don't specify http or https, just // works on both! See: http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml
You can add links to $links with:
$links['stackoverflow'] = 'www.stackoverflow.com';
$links = array(
array("Google","//google.co.uk"),
array("Bing","//bing.co.uk")
);
foreach ($links as $urlitem){
echo "<a href='".$urlitem[1]."'>".$urlitem[0]."</a>";
}

Find if an array value shows up in a string. (Kind of a reverse in_array?)

On a site I'm working on there's a subheadline that only should be shown if the information isn't already shown in the main headline. The main headline is an arbitrary string, while the subheadline is created programatically. The subheadline is generated from the contents of a multi-dimensional array (the contents of which are also used for other parts of the page.)
I used the PHP example of foreach to drill down through the array (only half-understanding how it was working), and then tried strpos to see if the values in the array were in the headline string.
Unfortunately, it doesn't work. There is a high chance that I made a stupid mistake in how I thought it's supposed to work. Or that the variable that tells the site to hide the subhead ("hider") is constantly reset to "no" as a result of the other values in the array.
foreach ($arr_info as $i1 => $n1) {
foreach ($n1 as $i2 => $n2) {
foreach ($n2 as $i3 => $n3) {
$pos = strpos($headline, $n3);
if ($pos === false) {
$hider="no";
} else {
$hider="yes";
}
}
}
Any ideas? Greatly appreciate the help.
Add this:
$hider="yes";
break;
Hope it helps
I think a cleaner approach would be to construct a regex out of the values and see if it matches your string:
$values = array();
array_walk_recursive($arr_info, function($k){$GLOBALS['values'][] = preg_quote($k);});
$hider = preg_match('/(' . implode($values, '|') . ')/', $headline) ? 'yes' : 'no';

Categories