digibucc Posted April 17, 2012 Share Posted April 17, 2012 I don't even really know how to title it. I have this piece of code, i wrote. it works with wordpress api but honestly i don't think that matters much. what it does, is pick $ci number of categories, and then picks 2 posts from each category. it then assigns the image in/attached to that post to an array, to be used later. this all works fine, the problem is when it picks a post that doesn't have an image. right now, it can't tell that. writing in a bit to be able to tell whether there is an image or not should be easy. i'm thinking it can be as basic as file_exists or as complicated as parsing the post. the part i have the problem with - is how do i make it go back and pick another post/category when it comes up short? more than once? i want it to keep going back until it comes up with an image. i'd rather not expand the search any farther than necessary <?php if ( false === ( $scroller = get_transient( $stransient ) ) ) { if (!is_array($scroller) || count($scroller)==0) { $args = array( 'type' => 'post', 'parent' => 18, 'exclude' => 63, 149, 278, 62 ); $companies = get_categories( $args ); while (count($companies) > $ci){unset($companies[array_rand($companies)]);} shuffle($companies);$truckco= array(); foreach ($companies as $company){ $argus = array('numberposts' => 2, 'orderby' => 'rand', 'post_type' => 'post', 'category' => $company->cat_ID ); $listings = get_posts($argus); foreach($listings as $tn=>$post) { $truckco[$company->slug]['truck_'. $tn]['image'] = get_the_image( array( 'image_scan' => true, 'format' => 'array', 'width' => '172', 'height' => '129' ) ); $truckco[$company->slug]['truck_'. $tn]['pid'] = $post->ID; } } ?> thank you Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/ Share on other sites More sharing options...
batwimp Posted April 17, 2012 Share Posted April 17, 2012 Have you looked into recursive functions? Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/#findComment-1338122 Share on other sites More sharing options...
digibucc Posted April 17, 2012 Author Share Posted April 17, 2012 i had not. that should fit in very nicely i think i was going to use goto but for some reason i really don't want to, this way i don't have to. thanks Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/#findComment-1338125 Share on other sites More sharing options...
Psycho Posted April 17, 2012 Share Posted April 17, 2012 It's hard to tell from that code, but are you running queries in loops? There are much more efficient ways of doing that. Anyway, as to your requested problem, you don't state how you are getting the image. Are the image names/path stored in the database? If so, what is the value of the field when there is no image? You could simply change the query used to get the posts to only pull records that have an image. However, if the image names are not stored in the database and you are storing them named in a way to associate them with the posts (e.g. using the post id), then you would have a real problem. A recursive function (especially with database requests) is a bad idea. If the query is requesting random records you have no idea how many requests would be needed to find two records with images. And, what if some category didn't have two posts with associated images - the recursive function would be caught in an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/#findComment-1338140 Share on other sites More sharing options...
batwimp Posted April 17, 2012 Share Posted April 17, 2012 I didn't see any database code in what was provided. But Psycho's right. You need to be very careful with recursive functions, especially if there are database queries involved. You should always give yourself a way to break out of them if they get out of control. But they can also be very helpful if you don't know how many times a function (or other section of code) will need to be called. Provide some more code and the gurus will be able to help you more than I can. Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/#findComment-1338143 Share on other sites More sharing options...
digibucc Posted April 17, 2012 Author Share Posted April 17, 2012 oh absolutely, that's what i was looking for i am aware of the dangers and so was looking for the most efficient option. i am using the wordpress api, which allows me to load the information(url) which is in a database, however through their functions only which do not allow specifying an image to exist. I could call the database directly but i would like not to do that. i have updated the code. right now, the code: it's not in a loop, but an if statement gets all categories (Except excluded) picks 2 posts from each category checks to make sure each post has an image if 1 or none, remove the category trim categories to final desired number use images <?php $ci = 8; // Companies Included, integer that is divisible by 2 $to = 48; // timeout in hours if ( false === ( $scroller = get_transient( $stransient ) ) ) { if (!is_array($scroller) || count($scroller)==0) { $args = array( 'type' => 'post', 'parent' => 18, 'exclude' => 63, 149, 278, 62 ); $companies = get_categories( $args ); shuffle($companies);$truckco= array(); $x=3; foreach ($companies as $company){ $count=0; $argus = array('numberposts' => 2, 'orderby' => 'rand', 'post_type' => 'post', 'category' => $company->cat_ID ); $listings = get_posts($argus); foreach($listings as $tn=>$post) { $truckco[$company->slug]['truck_'. $tn]['image'] = get_the_image( array( 'image_scan' => true, 'format' => 'array', 'width' => '172', 'height' => '129' ) ); $truckco[$company->slug]['truck_'. $tn]['pid'] = $post->ID; $file_loc = str_replace('http://www', '', $truckco[$company->slug]['truck_'. $tn]['image'][src]); //echo $file_loc. '<br>'; if (file_exists($file_loc)) { //echo 'image found! <br>'; } else { //echo 'image not found <br>'; unset($truckco[$company->slug]['truck_'. $tn]); } } } //var_dump($truckco); //echo count($truckco). '<br>'; foreach ($truckco as $co=>$trks){ if (count($trks) < 2) { unset($truckco[$co]); } } //var_dump($truckco); //echo 'count here ='. count($truckco); while (count($trucko) > $ci){unset($trucko[array_rand($trucko)]);} ?> instead of just removing the category, i would like to try again $x number of times. the only thing i can think of is creating a function and calling it in a while statement in that event, but i figured there would be a better way. Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/#findComment-1338200 Share on other sites More sharing options...
Psycho Posted April 17, 2012 Share Posted April 17, 2012 i am using the wordpress api, which allows me to load the information(url) which is in a database, however through their functions only which do not allow specifying an image to exist. I could call the database directly but i would like not to do that. instead of just removing the category, i would like to try again $x number of times. the only thing i can think of is creating a function and calling it in a while statement in that event, but i figured there would be a better way. You can't have it both ways. If you are dead-set on using a flawed method to get the data you need then you will need to rely upon a flawed method to resolve your issue. The "better way" is to create your own query with a condition to only return records where there is an image. But, if you are dead-set on not wanting go down this path, the best solution I can think of would be to query X number of records instead of 2. Then loop over the results and check if the record has an image. If so, add it to your results. If not, go to the next record. Once you have two records with images, break out of the processing loop. Querying one larger set of records will be substantially more efficient than having to do multiple queries to get two records at a time - and I don't see how the current process would allow you to ensure you are not getting duplicates if you rerun the query. Quote Link to comment https://forums.phpfreaks.com/topic/261106-repeating-a-section-of-code/#findComment-1338255 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.