0

How can i make an array of veriables generated using the values passed through a while loop

I am using osclass for a uni project and i find no way whatsoever to make a list os usable variables from the custom catagory fields i added.

I have made Identifiers for the custom catagories also ( SLUGs ) and have tried many different approaches to grab and echo the custom catagory values elsewhere on the page

I cannot isolate the custom catagory values by any means.

Below is the code used to display the custom catagory values on my osclass page

<?php if( osc_count_item_meta() >= 1 ) { ?> 
   
          <?php while ( osc_has_item_meta() ) { ?>
          
                    <?php if(osc_item_meta_value()!='') { ?> 
                   
                   
                    // I WOULD LIKE TO MAKE MAKE AN ARRAY OF VERIABLES SO I CAN USE THE CUSTOM CATAGORY DATA 
                    // TRIED SO FAR if(osc_item_meta_name()!='') {  $caughtcountry = osc_item_meta_value('country'); } 
                    // BUT THIS APPROACH DOES NOT WORK 

                    <?php } ?>
            
          <?php } ?>
          
          
      
    <?php } ?>`
    

I have tried using the identifiers that i added to the catagory in Admin panel

I have also tried using the Current PHP but cannot grab the values of specific custom catagories

Below is an example of one of my attempts to grab a custom catagory value but it only shows the 1st value within the values instead of cataching the 'age' value using the 'age' identifier i used.

 <?php if( osc_count_item_meta() >0 ) {  // The If osc_count_item_meta() >=1 
      if(osc_item_meta_value('age')!='') {  $caughtage = osc_item_meta_value('age'); }  else { $caughtage=''; }
                
    }   else { $caughtage=''; }
     ?>

Also tried the following

<?php if( osc_count_item_meta() >0 ) { 

      if(osc_item_meta_name('age')!='') {  $caughtage = osc_item_meta_value('age'); }  else { $caughtage=''; }
                
    }   else { $caughtage=''; }
     ?>

Have also tried common sense but that doesnt work either

<?php if( osc_item_age()!='' ) { $Age = osc_item_age(); } else {$Age='';} ?>

I am completely stumped.

The code below loops through the custom catagories but isolating them into variables im struggling with, yet i imagine it would be as easy as an array loop

<?php if( osc_count_item_meta() >= 1 ) { ?> 

<div id="custom_fields">
<div class="meta_list">

    <?php while ( osc_has_item_meta() ) { ?>
        <?php if(osc_item_meta_value()!='') { ?> 
            <div class="meta">
                <strong><?php echo osc_item_meta_name(); ?>:</strong> 
                <span><?php echo osc_item_meta_value(); ?></span>
            </div>

        <?php } ?>
    <?php } ?>
<?php } ?>
    
  
<?php } ?>

Any help would be greatly appreciated.

UPDATE: ( SOLVED - BUT STILL A PROBLEM )

Finallty Manged to grab the Values and pass inro a Variable, To Be Honest Lack Of Sleep Frustrated me and made me miss the obvious solution

 <?php if( osc_count_item_meta() >= 1 ) { ?> 
    
    
          <?php while ( osc_has_item_meta() ) { ?>
          
          
                    <?php if(osc_item_meta_value()!='') { ?>
                    
                            <?php $customData = osc_item_meta_name(); ?>
                            
                                 <? if ( $customData =="Gender")  { $caughtGender = osc_item_meta_value(); } else { $cuaghtGender = ""; } ?>
                                 <? if ( $customData =="Age")  { $caughtAge = osc_item_meta_value(); } else { $cuaghtAge = ""; } ?>
                                 <? if ( $customData =="Nationality")  { $caughtNationality = osc_item_meta_value();  } else { $cuaghtNationality = ""; } ?>
                                 <? if ( $customData =="Seeking")  { $caughtSeeking = osc_item_meta_value();  } else { $cuaghtSeeking = ""; } ?>
                                 <? if ( $customData =="Interests")  { $caughtInterests = osc_item_meta_value();  } else { $cuaghtInterests = ""; } ?>
                                 <? if ( $customData =="Pets")  { $caughtPets = osc_item_meta_value();  } else { $cuaghtPets = ""; } ?>
                               
                                 
                    <?php } ?>
                                       
          <?php } ?> <!-- WHILE LOOP END -->
          
     <?php } ?>

However having dont this. The next while loop of the same item_meta data isnt processed and shows no results. Does not throw any errors but just doesnt show any results. As though i have used the while loop and cant make another, Which is Odd to me..

How do i again make a While Loop that will show data and why does the 2nd loop not show data. I am egar to educate, thank you for any responces.

5
  • Why is this question tagged with JavaScript? It looks like a PHP question. Commented Sep 14 at 23:54
  • Can you please edit your question and describe clearly what happens and what you expected to happen instead? Commented Sep 15 at 2:32
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Sep 15 at 2:32
  • Same as Guillaume Outters has explained but i want usable variables so i can echo them in other locations. Commented Sep 15 at 2:35
  • @GuillaumeOutters 's answer builds an associative array ( $all ) which means if you know that age should exist then after the loop it can be accessed as $all['age'] . If there is a possibility it does not exist then some validation would need to be performed after the loop to verify required parameters exist. Commented Sep 15 at 12:18

1 Answer 1

2

By downloading the sources for OSClass I could find the osc_item_meta_value() function (in oc-includes/osclass/helpers/hItems.php) that does not take any parameter. So your call to osc_item_meta_value('country') or osc_item_meta_value('age') don't do anything different from osc_item_meta_value(), that is, return the current value.

By searching on the web I could find a solved question that makes me think that the magic lays in the repetitive calls to osc_has_item_meta() to walk through all metas (you cannot control which one to fetch, as you would like);
and then you have to use osc_item_meta_name() to know which meta you are reading (again without a parameter: you can't tell it what meta you'd like, you wait for it to tell you which one it is presently working on).

$all = array();
if(osc_count_item_meta() >= 1)
    while(osc_has_item_meta())
        if(osc_item_meta_value() != "")
            $all[osc_item_meta_name()] = osc_item_meta_value(); // Also there's osc_item_meta_slug() that could be of interest.

A quick way to display them (that you posted as a comment but then deleted) would be to echo '<div class="meta"><strong>'.htmlspecialchars(osc_item_meta_name()).'</strong>'.htmlspecialchars(osc_item_meta_value()).'</div>'; during the loop.

But then if you want a nice display where you control the order, I'd advize using an array ($all in my example) to store the names and values whatever the order they arrive,
then pick the values you want from this array (or sort it and display it in a second loop).

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

2 Comments

So thats great i get an array, how do i make variables now from that array ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.