0

i am very new to wordpress and i have dived in deep waters in other peoples code :(. so i have this data array as an option_value in wp_options table

a:2:{s:16:"block1_names_arr"; a:7:{i:0; s:6:"name1"; i:1; s:9:"name2"; i:2; s:8:"name3"; i:3; s:12:"name4"; i:4; s:11:"name5"; i:5; s:7:"name6"; i:6; s:7:"name7"; }s:16:"block2_names_arr"; a:7:{i:0; s:44:"surname1"; i:1; s:76:"surname2"; i:2; s:62:"surname3"; i:3; s:86:"surname4"; i:4; s:67:"surname5"; i:5; s:68:"surname6"; i:6; s:48:"surname7"; }}

i want to create a two column table in the front end showing names and surnames per line. i made a function and i managed to display the first set of data(the names) but i can't find a way to display the second

function names_table(){

$options = get_option('all_names');
$block1_names_arr = $options['block1_names_arr'];
$block2_names_arr = $options['block2_names_arr'];

$html = '

        <table class="table_open uv-table-dailies responsive table">
                <thead>
                    <tr>
                        <th>Event</th>
                        <th>Pick</th>
                    </tr>
                </thead>
                <tbody>';

                foreach($block1_names_arr as $bookie){
                    $html.='<tr class="tr_open_res">
                        <td>'. $bookie .'</td>
                        <td>CANT DISPLAY THE SURNAMES HERE</td>
                    </tr>';
                }
                $html.='
            </tbody>
            </table>';
return $html;}

i am not even sure if this is the correct way to do such a thing...

1 Answer 1

1

Don't know how you stored your data. Just answering from assumption:

<?php
$options = get_option('all_names');
?>

<table>
   <tbody>
      <?php
      $_counter = 0;
      foreach( $options as $option ) : ?>
         <tr>
            <td><?php echo $option['block1_names_arr'][$_counter]; ?></td>
            <td><?php echo $option['block2_names_arr'][$_counter]; ?></td>
         </tr>
      <?php
      $_counter++;
      endforeach; ?>
   </tbody>
</table>
4
  • thank you, it worked! i had to add $block1_names_arr = $options['block1_names_arr']; and $block2_names_arr = $options['block2_names_arr']; and change the echo to $block1_names_arr[$_counter] or else i was getting just Undefined index for block1_names_arr and block2_names_arr in the cells instead of values.
    – JoePanpi
    Jun 7, 2018 at 13:50
  • so what is better. add a function in functions.php and call it from where i need the table or just use the code in place?
    – JoePanpi
    Jun 7, 2018 at 13:51
  • my bad.. It works but it only returns the first 2 values instead of 7.
    – JoePanpi
    Jun 7, 2018 at 14:08
  • can't help without the $options = get_option('all_names'); var_dump($options); Jun 8, 2018 at 13:42

Your Answer

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

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