Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been to countless posts looking for the answer to this and have tried nearly every combination. So obviously, I'm missing a step so small that it's likely unfathomable to the Wordpress community.

My code writes the options to the database, but fails to display them back. The array is NOT set. Multiple options can be added and deleted.

Writes the data:

<?php
     $mycontents = array('content' => $_POST['cont'], 'content2' => $_POST['cont2']);
     update_option('slider_contents',$mycontents);
?>

Here are the database entries:

a:2:{s:7:"content";a:3:{i:0;s:19:"This is content 1-a";i:1;s:19:"This is content 2-a";i:2;s:19:"This is content 3-a";}s:8:"content2";a:3:{i:0;s:19:"This is content 1-b";i:1;s:19:"This is content 2-b";i:2;s:19:"This is content 3-b";}}

Trying to read and display the data:

<?php
    $the_contents=get_option('slider_contents');
    foreach ($the_contents as $content) {
         $content1=stripslashes($content->content);
         $content2=stripslashes($content->content2);
?>
<li><textarea name="cont[]" rows="3" style="width:70%;" ><?php echo $content1; ?></textarea><br><textarea name="cont2[]" rows="3" style="width:70%;" ><?php echo $content2; ?></textarea><br><input type="button" value="Delete this option" onClick="delete_field(this);"  /><input type="button" value="Add new option" onClick="add_to_field(this);"  /></li>

        <?php } ?>

I've also tried...

<?php
    $the_contents=get_option('slider_contents');
    foreach ($the_contents as $content) {
         $content1=stripslashes($content['content']);
         $content2=stripslashes($content['content2']);
?>

The output of var_dump($the_contents); is :

array(2) { 
  ["content"]=> array(3) { 
    [0]=> string(19) "This is content 1-a" 
    [1]=> string(19) "This is content 2-a" 
    [2]=> string(19) "This is content 3-a" 
  } 
  ["content2"]=> array(3) { 
    [0]=> string(19) "This is content 1-b" 
    [1]=> string(19) "This is content 2-b" 
    [2]=> string(19) "This is content 3-b" 
  } 
}
share|improve this question
2  
Add var_dump($the_contents); immediately after your get_option line and paste the output into your question, please. –  s_ha_dum Feb 24 '13 at 16:02
    
array(2) { ["content"]=> array(3) { [0]=> string(19) "This is content 1-a" [1]=> string(19) "This is content 2-a" [2]=> string(19) "This is content 3-a" } ["content2"]=> array(3) { [0]=> string(19) "This is content 1-b" [1]=> string(19) "This is content 2-b" [2]=> string(19) "This is content 3-b" } } –  Jim S. Feb 24 '13 at 18:05
    
"... and paste the output into your question" Formatting in comments is minimal making anything besides ordinary language content hard to read. –  s_ha_dum Feb 24 '13 at 18:09

1 Answer 1

Let's look at your code. Your first code block is treating an array like an object so your second attempt is closer to accurate:

$the_contents=get_option('slider_contents');
// var_dump($the_content);
foreach ($the_contents as $content) {
     $content1=stripslashes($content['content']);
     $content2=stripslashes($content['content2']);

Assuming you did what I suggested and placed the var_dump where I have in that code block, then what is happening is this: foreach ($the_contents as $content) { lets you loop through the array. At each iteration, $content is itself an array that looks like:

array(3) { 
    [0]=> string(19) "This is content 1-a" 
    [1]=> string(19) "This is content 2-a" 
    [2]=> string(19) "This is content 3-a" 
}

So when you try to access $content['content'] you are trying to access a key that doesn't exist-- you've already looped "past" that. You can demonstrate this for yourself by running:

$the_contents = unserialize('a:2:{s:7:"content";a:3:{i:0;s:19:"This is content 1-a";i:1;s:19:"This is content 2-a";i:2;s:19:"This is content 3-a";}s:8:"content2";a:3:{i:0;s:19:"This is content 1-b";i:1;s:19:"This is content 2-b";i:2;s:19:"This is content 3-b";}}');
foreach ($the_contents as $content) {
  var_dump($content); 
}

What you need to be doing is looping over that $contents array and taking each piece individually.

foreach ($the_contents as $content) { // this part you already have
  foreach ($content as $c) {
    echo stripslashes($c);
    // you are building a string, of course, but that is the idea
  }
}
share|improve this answer
    
Thanks for the explanation. This certainly looks the way to go, but I'm thinking I may not be formulating my array correctly to begin with. Lets say I have 5 options per record (rather than 2). Would I need to do 5 stacked foreach loops? Should I format my update_option array differently instead? I'm at the start of making a simple slider-type plugin where I can add as many slides as I wish. Each slide has 5-6 attributes occupying one record (color, header, text, etc). These attributes will be occupying content, content1, etc. –  Jim S. Feb 24 '13 at 19:09
    
You would only need 5 nested loops if you have nested arrays that deep, and if you do you should probably at least consider doing it differently. You might be better off starting another question to ask that. It is really a pretty different question. I am not sure the question would be WordPress specific (this one is questionable, honestly), though, so maybe Stack Overflow –  s_ha_dum Feb 24 '13 at 19:21
    
@NickYoung : not with that array. Try it. –  s_ha_dum May 20 at 1:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.