Jump to content

Problem with foreach loop


Go to solution Solved by Lassie,

Recommended Posts

I want to use the results of a db query to print images associated with several wordpress posts.

To do this I need to pass the post id to the image processing code.

I can get all the post ids ok but I am not getting the code in my foreach to work.

Is their an error or a better approach please.

I have attached my file.gallery_test2.html

 

Link to comment
https://forums.phpfreaks.com/topic/294550-problem-with-foreach-loop/
Share on other sites

Sorry I did attach a file. Anyway below is the code .

Thanks

<?php
 
/*
 * Template Name: gallery_test2
 * 
 */
?>
<?php get_header(); ?>
 
<div id="container">
<div id="content" role="main">
                <?php
/* Run the loop to output the page.
* If you want to overload this in a child theme then include a file
* called loop-page.php and that will be used instead.
*/
get_template_part( 'loop', 'page' );
?>
                            
                        <?php
                        //get post id from form
                        //select all post ids for a specific address
                        
                        
                        function bdw_get_images() {
                        global $posts;
                                    
                        
                        $wpdb->show_errors();
                        $attachments_id=array();
                        $property_title="1 hawkesley Close";
                        global $wpdb;
                        $result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post ID
                        while ( $row = mysql_fetch_assoc( $result ) ) {
 
$attachments_id[]=$row['post_id'];
                                }
                                
                                print_r($attachments_id);
                                
                                foreach ($attachments_id[] as $iPostID => $attachment_id) {
 
                          // Get images for this post
                          $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
 
                         // If images exist for this page
                        if($arrImages) {
 
                        // Get array keys representing attached image numbers
                          $arrKeys = array_keys($arrImages);
 
                         /******BEGIN BUBBLE SORT BY MENU ORDER************
                       // Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
                       foreach($arrImages as $oImage) {
                       $arrNewImages[] = $oImage;
                      }
 
                     // Bubble sort image object array by menu_order TODO: Turn this into std "sort-by" function in functions.php
                     for($i = 0; $i < sizeof($arrNewImages) - 1; $i++) {
                     for($j = 0; $j < sizeof($arrNewImages) - 1; $j++) {
                     if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order) {
                     $oTemp = $arrNewImages[$j];
                     $arrNewImages[$j] = $arrNewImages[$j + 1];
                     $arrNewImages[$j + 1] = $oTemp;
                }
            }
        }
 
                    // Reset arrKeys array
                    $arrKeys = array();
 
                    // Replace arrKeys with newly sorted object ids
                     foreach($arrNewImages as $oNewImage) {
                     $arrKeys[] = $oNewImage->ID;
        }
                 ******END BUBBLE SORT BY MENU ORDER**************/
 
                // Get the first image attachment
              //$iNum = $arrKeys[0];
                  foreach ($arrKeys as $iNum) {
 
               // Get the thumbnail url for the attachment
                $sThumbUrl = wp_get_attachment_thumb_url($iNum);
 
              // UNCOMMENT THIS IF YOU WANT THE FULL SIZE IMAGE INSTEAD OF THE THUMBNAIL
              //$sImageUrl = wp_get_attachment_url($iNum);
 
               // Build the <img> string
                 $sImgString = '<a href="' . get_permalink() . '">' .
                            '<img src="' . $sThumbUrl . '" width="150" height="150" alt="Thumbnail Image" title="Thumbnail Image" />' .
                        '</a>';
 
                 // Print the image
                echo $sImgString;
        }
   
          }
        
            }
 
              }
                                bdw_get_images() 
            ?>
                                
                       
                        
                            
</div><!-- #content -->
</div><!-- #container -->
 
 
<?php get_footer(); ?>
  • Solution

Hi I have solved my problem and post my revised code in case it of help to anyone.

I am sure it can be improved and it feels a long winded piece of code but thats where my abilities are right now.

Good luck.

 

 <?php
                        //get post id from form
                        //select all post ids for a specific address
                        
                        global $posts;
                        //$wpdb->show_errors();//debug
                        $attachments_id=array();
                        $property_title="23 Park Road";//property title will be passed from front end form where property is selected.
                        global $wpdb;
                        $result = mysql_query("SELECT post_id FROM sw_images_upload WHERE property_title = '$property_title' ");// Get the post IDs
                        while ( $row = mysql_fetch_assoc( $result ) ) {
 
$attachments_id[]=$row['post_id'];//put all post ids for specific address into array
                                }
                                
                               // print_r($attachments_id);
                                //extract post ids and cycle through image code to display images for selected property
                                foreach ($attachments_id as $iPostID) {
                                    //echo $iPostID;
                                
                                
                           // Get images for this post
                          $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
 
                         // If images exist for this page
                        if($arrImages) {
 
                        // Get array keys representing attached image numbers
                          $arrKeys = array_keys($arrImages);
 
                         /******BEGIN BUBBLE SORT BY MENU ORDER************
                       // Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
                       foreach($arrImages as $oImage) {
                       $arrNewImages[] = $oImage;
                      }
 
                     // Bubble sort image object array by menu_order TODO: Turn this into std "sort-by" function in functions.php
                     for($i = 0; $i < sizeof($arrNewImages) - 1; $i++) {
                     for($j = 0; $j < sizeof($arrNewImages) - 1; $j++) {
                     if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order) {
                     $oTemp = $arrNewImages[$j];
                     $arrNewImages[$j] = $arrNewImages[$j + 1];
                     $arrNewImages[$j + 1] = $oTemp;
                }
            }
        }
 
                    // Reset arrKeys array
                    $arrKeys = array();
 
                    // Replace arrKeys with newly sorted object ids
                     foreach($arrNewImages as $oNewImage) {
                     $arrKeys[] = $oNewImage->ID;
        }
                 ******END BUBBLE SORT BY MENU ORDER**************/
 
                // Get the first image attachment
              //$iNum = $arrKeys[0];
                  foreach ($arrKeys as $iNum) {
 
               // Get the thumbnail url for the attachment
                $sThumbUrl = wp_get_attachment_thumb_url($iNum);
 
              // UNCOMMENT THIS IF YOU WANT THE FULL SIZE IMAGE INSTEAD OF THE THUMBNAIL
              //$sImageUrl = wp_get_attachment_url($iNum);
 
               // Build the <img> string
                 $sImgString = '<a href="' . get_permalink() . '">' .
                            '<img src="' . $sThumbUrl . '" width="150" height="150" alt="Thumbnail Image" title="Thumbnail Image" />' .
                        '</a>';
 
                 // Print the image
                echo $sImgString;
        }
   
          }
        
                                }//end of foreach
 
              
                               
            ?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.