1

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

<a href="#" id="<?php echo $nexts; ?>">next</a>

calling the id with jquery

$('#design1').on('click',function() {
  $('#web1').removeClass().addClass('big1');
});
5
  • 1
    when asking a question, always make sure to indent your code and not mix tabs and spaces, because you want people to be able to read your code =) On a secondary note, don't use <a href="#"> anchors. This is a leftover from html 3.2, under html5 (and html 4.01) rules, this link will actually take you to the top of the page. Commented Nov 30, 2013 at 6:57
  • 1
    can u explain clearly, what u want to do with the loo[p Commented Nov 30, 2013 at 7:01
  • what is the output that you want?? can you please tell what you are trying to do with the loop??? Commented Nov 30, 2013 at 7:14
  • //My array Array ( [0] => design1 [1] => design2 [2] => design3 [3] => design4 ) I want the output design1 then next time i click the link design2 and so on Commented Nov 30, 2013 at 9:15
  • Well I've tried everything everyone has recomended but I still can't get it to do what I want. if anyone else has any suggestions please enlighten me. Thanks guys! Commented Nov 30, 2013 at 9:48

3 Answers 3

2
foreach( $designslist as $value )

needs to be

foreach( $designslist as $i => $value )

$i is the array index

Sign up to request clarification or add additional context in comments.

1 Comment

//My array Array ( [0] => design1 [1] => design2 [2] => design3 [3] => design4 ) //set the current index? $current_id = current($designslist); //Next equals $next = $designslist[$i + 1]; $i = 0; foreach( $designslist as $i => $value ) if( $value !== $next )continue; $nexts = $next; echo $nexts; $i++; } what i need to do is loop through my array and get the design to be added into my <a href="" id="HERE"> which will keep looping each time I click the link. Thanks for all your help.
0

You can try array search

$tag       = array_search($value, $designslist);
$nexts     = $$designslist[$tag]; 

Comments

0

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.

2 Comments

//My array Array ( [0] => design1 [1] => design2 [2] => design3 [3] => design4 ) $current_id = current($designslist); $next = $designslist[$i + 1];--foreach( $designslist as $i => $value ){if( $value !== $next )continue; $nexts = $next; echo $nexts; $i++;} how do I echo out design1 then next time i click the link it should be design2 and so on
You'll will need a variable in your JS to count the current "i", so you can get the next one. I think you should create a new question, focused in the JS / JQuery part!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.