Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7547393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:26:32+00:00 2026-05-30T09:26:32+00:00

Here is what I am trying to do. I am on a category page

  • 0

Here is what I am trying to do. I am on a category page in wordpress (this category is actually a bunch of food items) and I am trying to implement a nutrition calculator for the posts. So as the loop progresses, it displays some of the information from custom fields and stores the rest in an array.

            <?php
        $nutrition_info = array();   //Master Multidimentional Array
        $posts = query_posts($query_string . '&orderby=title&order=asc&posts_per_page=-1');//show all posts in our the category sorted alphabetically
        if ( have_posts() ) : while ( have_posts() ) : the_post(); 
            $prodslug = basename(get_permalink());
            global $post;$thePostID = $post->ID;
            $posttitle = get_the_title();
            // what to render
            ?>
            <div id="<?php echo $prodslug ?>" class="gridprod">
                <a class="prodimg" href="<?php echo get_permalink() ?>">
                    <img src="<?php echo get('imagery_display_image'); ?>" height="200" width="210">
                </a>
                <div class="overlay">
                    <a class="prodname" href="<?php echo get_permalink() ?>"><?php the_title(); ?></a>
                    <a href="javascript:void(0)" class="add softserve-scooped <?php echo $thePostID ?>"> Add </a><br>
                    <a class="prodlink" href="<?php echo get_permalink() ?>">details ...</a>
                </div>
            </div>
            <?php $temp = array(
                title => $posttitle,
                slug => $prodslug,
                calories => get('nutritional_information_calories'),
                totalfat => get('nutritional_information_total_fat'),
                saturated => get('nutritional_information_saturated_fat'),
                trans => get('nutritional_information_trans_fat'),
                cholesterol => get('nutritional_information_cholesterol'),
                sodium => get('nutritional_information_sodium'),
                carbs => get('nutritional_information_total_carbs'),
                fiber => get('nutritional_information_fiber'),
                sugar => get('nutritional_information_sugar'),
                protien => get('nutritional_information_protien'),
                vitamina => get('nutritional_information_vitamin_a'),
                vitamind => get('nutritional_information_vitamin_d'),
                calcium => get('nutritional_information_calcium_')) ?>
                <?php array_push($nutrition_info, $temp); ?>
        <?php endwhile; 
        <?php endif; ?>

This part is working just fine. $nutrition_info is populated as a multidimensional array as intended. So now I need to make it so I can access this information on command with jquery (ie. when .add is clicked take all the associated values in the array and add them to a variable that displays in the calculator.

I have tried multiple methods and have had no luck so I am coming to you a defeated man in search of advice. any ideas… or if I have to come at it a different way… anything to get me there.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:26:33+00:00Added an answer on May 30, 2026 at 9:26 am

    The problem is that you have your nutrition info stored in a multidimensional array in PHP, which is server-side, and you need to access it via jQuery, which is client-side. So the solution is to convert your nutrition info into a client-side Javascript array. Luckily PHP offers a quick way to do this — after your loop, use the json_encode method to convert your array:

    <script type="text/javascript">
    var nutrition_info = <?php echo json_encode($nutrition_info); ?>;
    </script>
    

    From within a jQuery event handler you should be able to access all your nutrition info.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I am trying to implement a class for complex numbers using books and
here is what I'm trying to do: page.replace_html('manage_categories_list', :partial => /categories/categories, :layout => :modal)
I am trying to pull information for items to display on the page. For
I just imported my posts from blogger into wordpress. In doing this, the tags
I am trying to style category names with different colours. Here is my PHP:
I have this page and I am trying to link to the /our-other-brands page
Here's what Im trying to do. I have a hyperlink like this on my
I am trying to get the index-page of a Wordpress-blog show some very specific
I'm new to template editing in wordpress and I'm trying to get a page
Programming Student here...trying to work on a project but I'm stuck. The project is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.