Jump to content

WordPress Template logic: Conditional display coding partially failing


LDMartin1959

Recommended Posts

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 by LDMartin1959
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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	
	  }
} ?>
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.