Lassie Posted February 12, 2015 Share Posted February 12, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/294550-problem-with-foreach-loop/ Share on other sites More sharing options...
cyberRobot Posted February 12, 2015 Share Posted February 12, 2015 If you post your existing code, we may be able to help. Quote Link to comment https://forums.phpfreaks.com/topic/294550-problem-with-foreach-loop/#findComment-1505540 Share on other sites More sharing options...
Lassie Posted February 12, 2015 Author Share Posted February 12, 2015 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/294550-problem-with-foreach-loop/#findComment-1505542 Share on other sites More sharing options...
Solution Lassie Posted February 13, 2015 Author Solution Share Posted February 13, 2015 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/294550-problem-with-foreach-loop/#findComment-1505604 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.