Thatch77 Posted September 22, 2017 Share Posted September 22, 2017 Hi guys, so im fairly new to coding in general (few months). I'm so close to doing this but i just cant wrap my head around this little portion. So basically i need to show a certain amount of images depnding on the package someone buys. So i need gold to show 10, silver 5 and standard 1. I have done alot of it already but i just cant finish it off, or seem to. Ive been at it for a while now! Heres the code: if (sizeof($imgMeta)>0) { echo '<div class="img_wrap">'; foreach ($imgMeta as $value) { if ($value) { $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); //print_r($imgUrl); echo '<img src="'.$imgUrl[0].'">'; } } echo '</div>'; } } if($i > 1 && $package !='Gold');{ echo '<img src="'.$imgUrl[0].'">'; I will be so grateful if anyone can tell me how to finish this and explain why so i can improve. I get that im calling the array and as its in a loop it will keep going from 0. But How do i stop it at 5 and 1 depending on package? Thanks in advance Quote Link to comment Share on other sites More sharing options...
Barand Posted September 22, 2017 Share Posted September 22, 2017 Use a for() loop. If $N is the required number of images for ($i=0; $i<$N; $i++) { // show image $i } Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 Sorry as im a bit new to coding could you tell me what that does exactly? Putting that code in wont fix it completely will it? Cause it doesnt declare wether its silver/gold etc? Sorry if i sound stupid im just trying to wrap my head around it Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 Im a bit confused how to declare the amount of images allowed to show. As in $N = ? As its a wp function/array its calling how to i stop at 5 images and 10 images etc? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 22, 2017 Share Posted September 22, 2017 (edited) You could use an array to store the number allowed for each package $allowed = [ 'gold' => 10, 'silver' => 5, 'standard' => 1 ]; $package = 'silver'; // from your input for ($i=0; $i<$allowed[$package]; $i++) { // output image $i } Edited September 23, 2017 by ignace Added missing ', fixed typo Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 The thing is its alrady in the array wp_get_attachment_image_src($value,'thumbnail');? I just dont understand how to stop at 5 and 1 basically. Im new to all of this and really need to finish this today as it has to be handed in. How would you write it to actually achieve each package? Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 $allowed = array('Gold' =>10, 'Silver' => 5, 'Standard' =>1); $package= 'Standard'; // from your input for ($i=0; $i<$allowed[$package]; $i++) { // output image $i } So i tested this but it is still showing more than 1 image for standard package? Any idea why? Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 Is this possibly the issue also? Wont this display all images in the array $imgMeta? if (sizeof($imgMeta)>0) { echo '<div class="img_wrap">'; foreach ($imgMeta as $value) { if ($value) { $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); //print_r($imgUrl); Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 I just cant seem to get it. The last few lines of this code. In my logic is. If package is silver echo the array $imgurl position 0. So 1 image. But it doesnt display one image it loops all of them cause its in a loop. Nps. So maybe i say $imgUrl[0,1,2,3,4]. Only use images at those positions. But that doesnt work so im assuming way off. Updated code : if(empty($user_photo[0])) { $user_photo = '<img src="'.get_template_directory_uri().'/images/empty_avatar.jpg"/>'; } else { $user_photo = '<img src="'.$user_photo[0].'"/>'; } if($user_photo != ''){ $description_block .= '<div class="pf-itempage-sidebarinfo-photo">'.$user_photo.'</div>'; $package = get_post_meta($the_post_id, 'packagename',true); $imgMeta = get_post_meta($the_post_id, 'webbupointfinder_item_images'); //print_r($imgMeta); if (sizeof($imgMeta)>0) { echo '<div class="img_wrap">'; foreach ($imgMeta as $value) { if ($value) { $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); //print_r($imgUrl); echo '<img src="'.$imgUrl[0].'">'; } } echo '</div>'; } if($package != 'Silver'){ echo '<img src="'.$imgUrl[0]">'; } } Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 $the_post_id = get_the_id(); $user_photo = wp_get_attachment_image_src( get_post_thumbnail_id( $the_post_id ), 'full' ); // $user_photo = wp_get_attachment_image(get_user_meta( $user->ID, 'user_photo', true ),'medium'); if(empty($user_photo[0])) { $user_photo = '<img src="'.get_template_directory_uri().'/images/empty_avatar.jpg"/>'; } else { $user_photo = '<img src="'.$user_photo[0].'"/>'; } if($user_photo != ''){ $description_block .= '<div class="pf-itempage-sidebarinfo-photo">'.$user_photo.'</div>'; $package = get_post_meta($the_post_id, 'packagename',true); $imgMeta = get_post_meta($the_post_id, 'webbupointfinder_item_images'); //print_r($imgMeta); if (sizeof($imgMeta)>0) { echo '<div class="img_wrap">'; foreach ($imgMeta as $value){ if ($value) { $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); //print_r($imgUrl); echo '<img src="'.$imgUrl[0].'">'; } } echo '</div>'; } } if($package == 'Gold'){ echo '<img src="'.$imgUrl[0].'">'; } elseif($package == 'Silver'){ echo '<img src="'.$imgUrl[0].'">'; }else{ echo '<img src="'.$imgUrl[0].'">'; } Feel like i should have added this from the start, but as the $package variable is already done. Im just struggling with what if($package == 'Gold'){ echo '<img src="'.$imgUrl[0].'">'; } elseif($package == 'Silver'){ echo '<img src="'.$imgUrl[0].'">'; }else{ echo '<img src="'.$imgUrl[0].'">'; } is actually doing? I mean i get imgUrl is an array, but how do i stop that at 5 images and 10 images etc? It just loops and displays all of them across all subscriptions. Sorry if it seems obvious.... Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 22, 2017 Share Posted September 22, 2017 (edited) You're trying to limit the output after you've already done the output. Your foreach() loop prints out all the images, no matter what. $the_post_id = get_the_id(); $user_photo = wp_get_attachment_image_src( get_post_thumbnail_id( $the_post_id ), 'full' ); $levels = ['gold'=>10, 'silver'=>5, 'standard'=>1]; if ( empty( $user_photo[ 0 ] ) ) { $user_photo = '<img src="' . get_template_directory_uri() . '/images/empty_avatar.jpg"/>'; } else { $user_photo = '<img src="' . esc_url($user_photo[ 0 ]) . '"/>'; } if ( $user_photo != '' ) { $description_block = '<div class="pf-itempage-sidebarinfo-photo">' . wp_kses_post($user_photo) . '</div>'; $package = get_post_meta( $the_post_id, 'packagename', true ); if ( sizeof( $imgMeta ) > 0 ) { $imgMeta = maybe_unserialize( get_post_meta( $the_post_id, 'webbupointfinder_item_images' ) ); $imgs = array_slice($imgMeta, 0, $levels[strtolower($package)]); echo '<div class="img_wrap">'; foreach ( $imgs as $value ) { if ( $value ) { $imgUrl = wp_get_attachment_image_src( $value, 'thumbnail' ); echo '<img src="' . esc_url($imgUrl[ 0 ]) . '">'; } } echo '</div>'; } } Let's take your code as the starting point - we create the array of possible package levels (Gold, Silver, and Standard) and assign them the correct number of displayed images. I'm assuming because you've been looping through $imgMeta that it's actually an array, but I'm using maybe_unserialize() just in case. Using the $levels array, we use array_slice() to get only the number of image IDs allowed according to package level. Then you can loop through those as you were doing before (without any limits) and it should only display the correct number of images. Couple other little things - you need to escape your output. I've added some escaping to your code. Also, when posting code on this forum, please use the 'code' button (it looks like this : < > ) in the toolbar. Makes it a lot easier to read. One more thing - this code is completely untested. Edited September 22, 2017 by maxxd 1 Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 22, 2017 Author Share Posted September 22, 2017 (edited) Hi, thanks for your reply. For some reason the $levels array is breaking the page. Not sure why yet so will need to take another look but if you notice why please let me know and thanks so much for your help guys Ok so fixed that syntax error but it messes the page styling up massively and for some reason displays only 1 image regardless of package type? Debug mode says that undefned variable on this line : if ( sizeof( $imgMeta ) > 0 ) { Edited September 22, 2017 by Thatch77 Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 23, 2017 Author Share Posted September 23, 2017 So here i have changed my code quite alot. But i am now getting the problem of bool(false) when i var dump.. I just dont know what im missing... $the_post_id = get_the_id(); $user_photo = wp_get_attachment_image_src( get_post_thumbnail_id( $the_post_id ), 'full' ); // $user_photo = wp_get_attachment_image(get_user_meta( $user->ID, 'user_photo', true ),'medium'); if(empty($user_photo[0])) { $user_photo = '<img src="'.get_template_directory_uri().'/images/empty_avatar.jpg"/>'; } else { $user_photo = '<img src="'.$user_photo[0].'"/>'; } if ($user_photo != ''){ $description_block .= '<div class="pf-itempage-sidebarinfo-photo">'.$user_photo.'</div>'; $package = get_post_meta($the_post_id, 'packagename', true); $imgMeta = get_post_meta($the_post_id, 'webbupointfinder_item_images'); $numImages = array( 'Standard' => 1, 'Silver' => 5, 'Gold' => 10 ); $maxImages = (isset($numImages[$package]) ? $numImages[$package] : 1); // just in case if (!$has_post_image) { $maxImages -= 1; } $i = 0; foreach ($imgMeta as $img) { if ($i > $imgMeta) { break; } $i++; $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); // include $imgUrl as the src of the img in HTML. } var_dump($imgUrl); } Quote Link to comment Share on other sites More sharing options...
Thatch77 Posted September 23, 2017 Author Share Posted September 23, 2017 (edited) Reverting back to original code i had. My boss says keep code as: $the_post_id = get_the_id(); $user_photo = wp_get_attachment_image_src( get_post_thumbnail_id( $the_post_id ), 'full' ); // $user_photo = wp_get_attachment_image(get_user_meta( $user->ID, 'user_photo', true ),'medium'); if(empty($user_photo[0])) { $user_photo = '<img src="'.get_template_directory_uri().'/images/empty_avatar.jpg"/>'; } else { $user_photo = '<img src="'.$user_photo[0].'"/>'; } if ($user_photo != ''){ $description_block .= '<div class="pf-itempage-sidebarinfo-photo">'.$user_photo.'</div>'; $package = get_post_meta($the_post_id, 'packagename', true); $imgMeta = get_post_meta($the_post_id, 'webbupointfinder_item_images'); if (sizeof($imgMeta)>0) { echo '<div class="img_wrap">'; foreach ($imgMeta as $value) { if ($value) { $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); //print_r($imgUrl); echo '<img src="'.$imgUrl[0].'">'; } } echo '</div>'; } } if($i > 1 && $package !='Gold');{ } Get the code back to how it was (displaying all images) and add the if statement that determines whether the image shows or not - that is all required on this task If anyone is able to tell me how to do that great. Because i cant figure it out Edited September 23, 2017 by Thatch77 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 23, 2017 Share Posted September 23, 2017 (edited) This is such a long topic and you still seem confused. Here is my re-working of the logic. Not sure if it is going to help you but at least it tries to make less assumptions and more clearly identifies what you are trying to analyze. $the_post_id = get_the_id(); $user_photo = wp_get_attachment_image_src( get_post_thumbnail_id( $the_post_id ), 'full' ); //***** Per the doc that I googled, this function will return a 4 element array or FALSE. // Checking for an empty first element is a vague way of determining the result. // Better this to be more precise: //if(empty($user_photo[0])) if ($user_photo === false) { $user_photo = '<img src="'.get_template_directory_uri().'/images/empty_avatar.jpg"/>'; } else { $user_photo = '<img src="'.$user_photo[0].'"/>'; //**** if you have a user_photo array you should continue handling it instead of ending the if/else here. //} //if ($user_photo != '') //{ //**** $description_block .= '<div class="pf-itempage-sidebarinfo-photo">'.$user_photo.'</div>'; $package = get_post_meta($the_post_id, 'packagename', true); $imgMeta = get_post_meta($the_post_id, 'webbupointfinder_item_images'); if (sizeof($imgMeta) > 0) { echo '<div class="img_wrap">'; foreach ($imgMeta as $value) { //**** if you need to be concerned about empty array values here, CHECK for them!! // if ($value) if ($value <> '') { $imgUrl = wp_get_attachment_image_src($value,'thumbnail'); //**** You are risking a return value of false //**** here again but you are not handling it. echo '<img src="'.$imgUrl[0].'">'; } } echo '</div>'; } } if($i > 1 && $package !='Gold'); { //*** what is this? } Make your tests more clear by testing for exactly what you are looking for. You keep querying aboutthe empty function when there are better ways of checking your results. And you don't check for the things you should be checking for. Edited September 23, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 24, 2017 Share Posted September 24, 2017 Once again, you're outputting all the images, regardless of the package level limits. If your image IDs are stored in a serialized string in the post meta table (as they seem to be), you need to unserialize the string and then use just the number of images that match the package level limit. That's the reason for the array_slice() call in the code that I posted earlier. $packages = array( 'gold' => 10, 'silver' => 5, 'standard' => 1 ); $imgMeta = array( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ); print("<p>This is \$imgMeta:</p><pre>".var_export($imgMeta, true)."</pre>"); $gold = array_slice($imgMeta, 0, $packages['gold']); $silver = array_slice($imgMeta, 0, $packages['silver']); $standard = array_slice($imgMeta, 0, $packages['standard']); print("<p>This is \$gold:</p><pre>".var_export($gold, true)."</pre>"); print("<p>This is \$silver:</p><pre>".var_export($silver, true)."</pre>"); print("<p>This is \$standard:</p><pre>".var_export($standard, true)."</pre>"); Now, assuming that your user's package level is stored in the $package variable, replace the literal string with the variable in the array_slice() call. Quote Link to comment 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.