1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Help!

Discussion in 'PHP & Perl' started by crazyflx, Jun 9, 2010.

  1. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    I'm using a theme on my wordpress blog called "scarlett"

    You can view a working example of it here (it isn't mine): http://idonteatthat.com/

    You'll see on the right hand side, directly above the video (Hitler wants subway??) there are four posts, each has a thumbnail picture next to the post title.

    That thing rotates between all the posts a blog has on it, and automatically takes a picture from a custom field called "screen" (which the user fills in when making their post) and makes a thumbnail out of it.

    Well, on my blog, I don't always use the custom field, so I have blog posts appearing in that sidebar (which is referred to as a "feature list" and uses the PHP file featlist.php) that don't have images.

    I would like to make that sidebar only show posts IF the custom field "screen" was used while making the blog post, so that only posts with pictures appear there.

    Here is the code for "featlist.php" :

    Code:
    <div id="postlist">
    
    <ul class="spy">
    <?php $my_query = new WP_Query('orderby=rand'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post();?>
    <li>
    
    <?php $screen = get_post_meta($post->ID, 'screen', $single = true); 
    
    if ($screen !="")
    {?>
    
    <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
    <?php
    }
    ?>
    
    
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="fcats"><?php the_category(', '); ?> </div> 
    <div class="auth"> Posted by <?php the_author(); ?> </div> 
    
    </li>
    <?php endwhile; ?>
    
    </ul>
    </div>
    <div class="clear"></div>
     
  2. yeahright

    yeahright Registered Member

    Joined:
    Jan 17, 2009
    Messages:
    93
    Likes Received:
    32
    Code:
    <div id="postlist">
    
    <ul class="spy">
    <?php $my_query = new WP_Query('orderby=rand'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post();?>
    
    <?php $screen = get_post_meta($post->ID, 'screen', $single = true); 
    
    if(empty($screen)){
    ?>
    
    <li>
    <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="fcats"><?php the_category(', '); ?> </div> 
    <div class="auth"> Posted by <?php the_author(); ?> </div> 
    </li>
    
    <?php
    } //end if screen is empty
    ?>
    
    <?php endwhile; ?>
    
    </ul>
    </div>
    <div class="clear"></div>
    Should work.
     
    • Thanks Thanks x 1
  3. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Tried it, but nothing changed. I appreciate you giving it a shot though man, thanks!
     
  4. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    I'd be willing to pay $15 via PayPal for somebody to figure this out...
     
  5. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    According to Wordpress' source code, the return value of get_post_meta() could be boolean false, an empty string, an empty array, or some kind of actual data. *sigh*

    Right before your if ($screen !="") line, add this:

    Code:
    print_r($screen);
    Then refresh until you see a blog post in the sidebar without an image. Copy and paste the text that is outputted directly above it and reply back with it. That will shed some light on the problem, I think.
     
    • Thanks Thanks x 1
  6. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    Once you get what $screen returns from voyevoda input, change

    if ($screen != "") to if ($screen != "" && $screen != "what screen returned") and that should omit the return from showing the what you don't want it to.

    If $screen returns something different for the bad data each time, then figure out what is constant about it and do

    $constInString = strpos($screen,"constant output");
    if ($screen != "" && $consInString == false)

    Credit definitely goes to voyevoda on this one. Other wise i would have moved on and not replied at all.
     
    • Thanks Thanks x 1
  7. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:

    Alright, what happens when I used that little piece of code is this:

    When there IS a picture, the URL of the picture is displayed above everything else in the sidebar.

    When there IS NOT a picture there is no text at all.
     
  8. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    I'm not sure what to put
    Code:
    &&$screen !="HERE"
    because nothing appears.
     
  9. Hallow

    Hallow Newbie

    Joined:
    Sep 22, 2009
    Messages:
    21
    Likes Received:
    6
    Been a while since I've done a lot of wp work but shouldnt this work? Then again i might be way off, its been a while.

    Code:
    
    <div id="postlist">
    <ul class="spy">
    
    <?php
     $querystr = "
        SELECT wposts.* 
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wpostmeta.screen > '' 
        ORDER BY Rand
     ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
        <li>
        <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
        <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
        <div class="fcats"><?php the_category(', '); ?> </div> 
        <div class="auth"> Posted by <?php the_author(); ?> </div> 
        </li>
        </ul>
        </div>
        <div class="clear">
        </div>  
    
        <?php endforeach; ?>
        <?php endif; ?>
    
     
    • Thanks Thanks x 1
  10. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    I appreciate the attempt, but that didn't work. That made the sidebar disappear completely, moved everything else up and made the site just look generally all screwed up.
     
  11. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    In PHP, a string composed entirely of whitespace isn't considered "empty". It's possible that $screen is a single space character or something. Try this in the same spot I had you put the print_r($screen) above:

    Code:
    echo "<div class=\"bhw_test\">";
    print_r($screen);
    echo "</div>";
    
    Then refresh until you get a blank image and look at the page source and search for "bhw_test". See if there is ANYTHING (including whitespace) between the div tags.

    If so:

    Code:
    if(isset($screen) && $screen != false && !empty(trim($screen)))
    Should work.
     
    Last edited: Jun 10, 2010
  12. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    Try $screen != null because from what voyevoda said, "get_post_meta() could be boolean false, an empty string, an empty array", then null would be win, if that is the case.
     
  13. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Now the sidebar disappears and in it's place is the following message:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/thisweek/public_html/wp-content/themes/scarlett/featlist.php on line 9
     
  14. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    you are missing a ; probably to finish the line.

    Paste your code?
     
  15. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Thanks for your help guys, my knowledge of PHP is near nothing (obviously).

    Code:
    <div id="postlist">
    
    <ul class="spy">
    <?php $my_query = new WP_Query('orderby=rand'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post();?>
    <li>
    
    <?php $screen = get_post_meta($post->ID, 'screen', $single = true); 
    echo "<div class="bhw_test">";
    print_r($screen);
    echo "</div>";
    if ($screen !="")
    {?>
    
    <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
    <?php
    }
    ?>
    
    
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="fcats"><?php the_category(', '); ?> </div> 
    <div class="auth"> Posted by <?php the_author(); ?> </div> 
    
    </li>
    <?php endwhile; ?>
    
    </ul>
    </div>
    <div class="clear"></div>
     
  16. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    echo "<div class="bhw_test">";

    needs to be

    echo "<div class=\"bhw_test\">";

    Need to escape the " so it doesn't end the string prematurely.

    You can also do ?> <div class="bhw_test"> <?php to bounce from php to html
     
    • Thanks Thanks x 1
    Last edited: Jun 10, 2010
  17. borntoleadinCPA

    borntoleadinCPA Regular Member

    Joined:
    Dec 11, 2008
    Messages:
    224
    Likes Received:
    243
    This should work :)

    Code:
    <div id="postlist">
    
    <ul class="spy">
    <?php $my_query = new WP_Query('orderby=rand'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post();?>
    <?php $screen = get_post_meta($post->ID, 'screen', $single = true); 
    
    if ($screen !="")
    {?>
    <li>
    <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $screen; ?>&h=60&w=100&zc=1" alt=""/>
    
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <div class="fcats"><?php the_category(', '); ?> </div> 
    <div class="auth"> Posted by <?php the_author(); ?> </div> 
    
    </li>
    <?php
    }
    ?>
    
    <?php endwhile; ?>
    
    </ul>
    </div>
    <div class="clear"></div>
    If it did, PM me for paypal email. :D I could use the extra bucks.
     
  18. smith_scott

    smith_scott Regular Member

    Joined:
    May 6, 2010
    Messages:
    318
    Likes Received:
    12
    Occupation:
    Online Marketing Manager
    Location:
    SEO WORLD
    if problem is not solved I can try to resolve this issue.. Please let me know..thanks
     
  19. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    That worked perfectly.

    However, what's strange, is this is what the source code shows now:


    Code:
     <ul class="spy">
    <li>
    
    <div class="bhw_test"></div>
    
    <h2> 
     
    Last edited: Jun 10, 2010
  20. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    change
    if ($screen != "")

    to

    if (isset($screen) && $screen != false && !empty(trim($screen)) && $screen != NULL && $screen != "")