Jump to content

Listing packages help please?


Thatch77

Recommended Posts

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 $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?

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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]">';    
                        }
                        
}
Link to comment
Share on other sites

$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....

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ) {
Link to comment
Share on other sites

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);
                   }
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 about

the empty function when there are better ways of checking your results.

 

And you don't check for the things you should be checking for.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.