LDMartin1959 Posted December 20, 2013 Share Posted December 20, 2013 (edited) I am a WP and PHP novice still flailing in the deep end of the pool. I am trying to add some logic to display content on certain pages (via WP templates), not on others. I've made some progress but having issues. Here is the plan: On certain pages with an post type of "EVENT" I want content to appear, but not on OTHER pages with an post type of "EVENT". The problem I am running into is that part of the content is appearing on ALL event pages even if they don't meet the criteria. The logic is intended to be this: If the page is post_type "EVENT", AND if the indicated table contains a field with a record CAT which matches the current "POST ID", AND contains a record TYPE which matches value "1", display table cells which contain text "Presented by: ", an href and an image, else display table cells which contain a nonbreaking space. The problem is that the text "Presented by: " is appearing on ALL the even pages, regardless of whether they meet the criteria or not -- which would tend to indicate a problem with the logic code -- but by the same token the href and image are correctly appearing ONLY on the pages that meet the criteria and are NOT appearing on along with the rogue text on pages which do not meet the criteria. I am sure it is a coding error on my part but I am not sure what. ANY help anyone could give would be GREATLY appreciated. The code involved is below (oh, and I should mention that the code in question is in the HEADER.PHP file): <?php if($post->post_type=='event') { $q1=mysql_query("SELECT * FROM table_name WHERE type = 1 AND cat = $post->ID "); $row_diamond=mysql_fetch_object($q1); if ($q1){ ?> <td width="115" align="right" valign="middle" id="diamond1"><strong style="position:relative;bottom:-25px;">Presented by: </strong></td> <td id="diamond2" width="190"> <a href="<?=$row_diamond->website?>" title="<?=$row_diamond->website_title?>" target="_blank" style="cursor:pointer;"><img style="position:relative;bottom:5px;" src="<?php bloginfo('siteurl'); ?>/wp-content/plugins/sean_event/files/<?=$row_diamond->image?>" alt="<?=$row_diamond->alt_text?>"border="0"></a> <br /> </td> <?php } else{ ?> <td width="115" align="right" valign="middle" id="diamond1"> </td> <td id="diamond2" width="190"> </td> <?php } } ?> Edited December 20, 2013 by LDMartin1959 Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/ Share on other sites More sharing options...
Ch0cu3r Posted December 20, 2013 Share Posted December 20, 2013 This is you logic error here if ($q1){ You should be checking to see if the query has returned any results. mysql_query only returns false if there is an error. You can use mysql_num_nows to see if the query returned any results. if (mysql_num_rows($ql) != 0){ $row_diamond=mysql_fetch_object($q1); Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462840 Share on other sites More sharing options...
LDMartin1959 Posted December 20, 2013 Author Share Posted December 20, 2013 This is you logic error here if ($q1){ You should be checking to see if the query has returned any results. mysql_query only returns false if there is an error. You can use mysql_num_nows to see if the query returned any results. if (mysql_num_rows($ql) != 0){ $row_diamond=mysql_fetch_object($q1); I'll give that a try and see how that works out. I appreciate the info. Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462843 Share on other sites More sharing options...
LDMartin1959 Posted December 20, 2013 Author Share Posted December 20, 2013 I'll give that a try and see how that works out. I appreciate the info. This is you logic error here if ($q1){ You should be checking to see if the query has returned any results. mysql_query only returns false if there is an error. You can use mysql_num_nows to see if the query returned any results. if (mysql_num_rows($ql) != 0){ $row_diamond=mysql_fetch_object($q1); Whoops. I musta done something wrong: I replaced $row_diamond=mysql_fetch_object($q1); if ($q1){ with if (mysql_num_rows($ql) != 0){ $row_diamond=mysql_fetch_object($q1); The result was that the content is no longer showing up on the page it is supposed, the page which fulfills the criteria. Did I replace too much? Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462844 Share on other sites More sharing options...
LDMartin1959 Posted December 20, 2013 Author Share Posted December 20, 2013 I restore the code back to the original configuration and added a line to simply echo the value in mysql_num_rows($ql) after if ($q1) to see what it contains. Simply adding that echo killed the display of the intended content from everywhere, where intended and not; when I removed that echo the content returned as before (complete where intended, partial display where not intended to display any). I did an assignment $test_q1 = mysql_num_rows($ql) which also killed the display of the intended content from everywhere, where intended and not; when I removed that echo the content returned as before (complete where intended, partial display where not intended to display any). I'm not sure what I misunderstood of your instruction but obviously I need additional clarification. Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462848 Share on other sites More sharing options...
Ch0cu3r Posted December 20, 2013 Share Posted December 20, 2013 This is what your code should be <?php if($post->post_type=='event') { $q1=mysql_query("SELECT * FROM table_name WHERE type = 1 AND cat = $post->ID "); if (mysql_num_rows($q1)) { $row_diamond=mysql_fetch_object($q1); ?> <td width="115" align="right" valign="middle" id="diamond1"><strong style="position:relative;bottom:-25px;">Presented by: </strong></td> <td id="diamond2" width="190"> <a href="<?=$row_diamond->website?>" title="<?=$row_diamond->website_title?>" target="_blank" style="cursor:pointer;"><img style="position:relative;bottom:5px;" src="<?php bloginfo('siteurl'); ?>/wp-content/plugins/sean_event/files/<?=$row_diamond->image?>" alt="<?=$row_diamond->alt_text?>"border="0"></a> <br /> </td> <?php } else{ ?> <td width="115" align="right" valign="middle" id="diamond1"> </td> <td id="diamond2" width="190"> </td> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462850 Share on other sites More sharing options...
LDMartin1959 Posted December 20, 2013 Author Share Posted December 20, 2013 That still causes the content to disappear entirely. Which suggests that the query is not returning any results, yet I know it is (at least, at some point) because when the content IS being generated it is coming from the values from the query. Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462853 Share on other sites More sharing options...
Ch0cu3r Posted December 20, 2013 Share Posted December 20, 2013 That still causes the content to disappear entirely. or it could mean the code is causing an error. Check your servers error log or enable error reporting at the top of the script ini_set('display_errors', 'On'); error_reporting(E_ALL); As a side note I think it would be better if you performed your queries using the database object ($wpdb) provided by wordpress rather than using the mysql_* functions Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462858 Share on other sites More sharing options...
LDMartin1959 Posted December 20, 2013 Author Share Posted December 20, 2013 As a side note I think it would be better if you performed your queries using the database object ($wpdb) provided by wordpress rather than using the mysql_* functions You've just totally lost me!! Quote Link to comment https://forums.phpfreaks.com/topic/284878-wordpress-template-logic-conditional-display-coding-partially-failing/#findComment-1462860 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.