jordanlyall Posted April 24, 2009 Share Posted April 24, 2009 There's got to be a way to simplify all this. I'm stuck. <?php $r1show1 = get_post_meta($post->ID, "r1-show1", true); $r1score = get_post_meta($post->ID, "r1-score", true); $r1source = get_post_meta($post->ID, "r1-source", true); $r1individual = get_post_meta($post->ID, "r1-individual", true); $r1snippet = get_post_meta($post->ID, "r1-snippet", true); $r1link = get_post_meta($post->ID, "r1-link", true); $r1show2 = get_post_meta($post->ID, "r1-show2", true); $r2show1 = get_post_meta($post->ID, "r2-show1", true); $r2score = get_post_meta($post->ID, "r2-score", true); $r2source = get_post_meta($post->ID, "r2-source", true); $r2individual = get_post_meta($post->ID, "r2-individual", true); $r2snippet = get_post_meta($post->ID, "r2-snippet", true); $r2link = get_post_meta($post->ID, "r2-link", true); $r2show2 = get_post_meta($post->ID, "r2-show2", true); $r3show1 = get_post_meta($post->ID, "r3-show1", true); $r3score = get_post_meta($post->ID, "r3-score", true); $r3source = get_post_meta($post->ID, "r3-source", true); $r3individual = get_post_meta($post->ID, "r3-individual", true); $r3snippet = get_post_meta($post->ID, "r3-snippet", true); $r3link = get_post_meta($post->ID, "r3-link", true); $r3show2 = get_post_meta($post->ID, "r3-show2", true); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155538-php-newbie-needs-help-array/ Share on other sites More sharing options...
Maq Posted April 24, 2009 Share Posted April 24, 2009 You could probably loop through the $_POST array, but what does the get_post_meta() method look like? Quote Link to comment https://forums.phpfreaks.com/topic/155538-php-newbie-needs-help-array/#findComment-818526 Share on other sites More sharing options...
PFMaBiSmAd Posted April 24, 2009 Share Posted April 24, 2009 You can probably reduce it down to a simple loop, but it would take knowing everything about your existing code and data. What relation is there between the pieces of data, will it always be those names or could a way that returns all the available fields be used? Where it is coming from (apparently a form, but it is also referencing an object)? If this is from an form, using arrays of form data usually results in the simplest code. What does get_post_meta do now? It might be possible to add a function to return all the variables at one time. What are you doing with the results of that code. It is inefficient to populate a bunch of variables, then just use those variables once in the code immediately after they were set. Seeing the form and all the relevant form processing code would be the quickest way of developing an optimized solution. Quote Link to comment https://forums.phpfreaks.com/topic/155538-php-newbie-needs-help-array/#findComment-818537 Share on other sites More sharing options...
Zhadus Posted April 24, 2009 Share Posted April 24, 2009 Given the information, this might be cleaner. <?php $varAr = array("show1", "score", "source", "individual", "snippet", "link", "show2"); for ($x = 1; $x < 4; $x++) { for ($y = 0; $y < sizeof($varAr); $y++) { $val[0] = "r" . $x . $varAr[$y]; $val[1] = "r" . $x . "-" . $varAr[$y]; ${$val[0]} = get_post_meta($post->ID, $val[1], true); } } ?> Same results, less lines. EDIT: Added proper tags. Quote Link to comment https://forums.phpfreaks.com/topic/155538-php-newbie-needs-help-array/#findComment-818564 Share on other sites More sharing options...
jordanlyall Posted April 24, 2009 Author Share Posted April 24, 2009 Thanks for the feedback. I'll give that last one a try. Regarding more background info, here's the scoop: The site is ComicBookCritics.com. Like Metacritic for comic books. As you might have guessed, it's running on Wordpress. I'm a hack when it comes to php. The site is more 'proof of concept' more than anything. It's starting to pick up some traffic and I figured I should optimize it. Each post, or comic book review, has dozens of custom fields. Perhaps this is an issue better suited for a Wordpress forum, but the problems I'm having stem from my lack of php knowledge. Here's the entire snippet of code I use to generate the ratings on an individual post: <?php $r1show1 = get_post_meta($post->ID, "r1-show1", true); $r1score = get_post_meta($post->ID, "r1-score", true); $r1source = get_post_meta($post->ID, "r1-source", true); $r1individual = get_post_meta($post->ID, "r1-individual", true); $r1snippet = get_post_meta($post->ID, "r1-snippet", true); $r1link = get_post_meta($post->ID, "r1-link", true); $r1show2 = get_post_meta($post->ID, "r1-show2", true); $r2show1 = get_post_meta($post->ID, "r2-show1", true); $r2score = get_post_meta($post->ID, "r2-score", true); $r2source = get_post_meta($post->ID, "r2-source", true); $r2individual = get_post_meta($post->ID, "r2-individual", true); $r2snippet = get_post_meta($post->ID, "r2-snippet", true); $r2link = get_post_meta($post->ID, "r2-link", true); $r2show2 = get_post_meta($post->ID, "r2-show2", true); $r3show1 = get_post_meta($post->ID, "r3-show1", true); $r3score = get_post_meta($post->ID, "r3-score", true); $r3source = get_post_meta($post->ID, "r3-source", true); $r3individual = get_post_meta($post->ID, "r3-individual", true); $r3snippet = get_post_meta($post->ID, "r3-snippet", true); $r3link = get_post_meta($post->ID, "r3-link", true); $r3show2 = get_post_meta($post->ID, "r3-show2", true); $r4show1 = get_post_meta($post->ID, "r4-show1", true); $r4score = get_post_meta($post->ID, "r4-score", true); $r4source = get_post_meta($post->ID, "r4-source", true); $r4individual = get_post_meta($post->ID, "r4-individual", true); $r4snippet = get_post_meta($post->ID, "r4-snippet", true); $r4link = get_post_meta($post->ID, "r4-link", true); $r4show2 = get_post_meta($post->ID, "r4-show2", true); ?> <?php function criticReview($show1, $score, $individual, $source, $snippet, $link, $show2){ echo $show1; echo "<div class=\"critic-item\"><div class=\"critic-item-score\" style=\"float:left;\"> ". $score ." </div>"; echo "<span class=\"critic-item-individual\">by ". $individual ." </span>"; echo "<span class=\"critic-item-source\"> ". $source ." </span><br />"; echo "<span class=\"critic-item-snippet\"><em> ". $snippet ." </em></span><br />"; echo "<a href=\" ". $link ." \">Read Full Review</a></div><hr />"; echo $show2; } criticReview($r1show1, $r1score, $r1individual, $r1source, $r1snippet, $r1link, $r1show2); criticReview($r2show1, $r2score, $r2individual, $r2source, $r2snippet, $r2link, $r2show2); criticReview($r3show1, $r3score, $r3individual, $r3source, $r3snippet, $r3link, $r3show2); criticReview($r4show1, $r4score, $r4individual, $r4source, $r4snippet, $r4link, $r4show2); ?> Any feedback is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/155538-php-newbie-needs-help-array/#findComment-818667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.