Jump to content

[SOLVED] Simple IF ELSE statement in conjunction with ISSET and EMPTY


jarvis

Recommended Posts

Hi all,

 

I'm creating an article CMS system.

Each article can have it's own meta keywords and meta description, otherwise default ones are used which can be set via the CMS.

The main article page itself calls in a global header with the following code:

if(!isset($meta_keywords) || empty($meta_keywords)) { 

   $meta_key = "SELECT keywords FROM meta_content"; 
   $meta_key_result = mysql_query ($meta_key) or die( "Could not execute SQL query" );
   while ($row = mysql_fetch_array ($meta_key_result, MYSQL_ASSOC)) 
      { 
        $meta_keywords =$row['keywords']; 
      }        	
}

if(!isset($meta_description) || empty($meta_description)) {

   $meta_des = "SELECT description FROM meta_content"; 
   $meta_des_result = mysql_query ($meta_des) or die( "Could not execute SQL query" );
   while ($row = mysql_fetch_array ($meta_des_result, MYSQL_ASSOC)) 
      { 
        $meta_description =$row['description']; 
      }        
}

I then call this in using:

<meta name="keywords" content="<?php echo $meta_keywords; ?>" />
<meta name="description" content="<?php echo $meta_description; ?>" />

So in theory, if $meta_keywords and $meta_description are empty from the article, it will use the default!

 

This is the code I use on the article page to create the main page query & obtain the values for $meta_keywords and $meta_decription:

$query = "SELECT articles.article_id, articles.description, articles.status, articles.meta_keywords, articles.meta_description, articles.title, article_categories.category, article_categories.article_category_id FROM articles INNER JOIN (article_categories INNER JOIN article_associations ON article_categories.article_category_id = article_associations.article_category_id) ON articles.article_id = article_associations.article_id WHERE articles.article_id = $aid AND status = '1'";

#$result = mysql_query($query) or die( "Could not execute SQL query" );
$result =  mysql_query($query) or die(mysql_error());		

if (mysql_num_rows($result) == 1) { // if the query ran ok continue...

	// Retrieve the info from the db
	$row = mysql_fetch_array ($result, MYSQL_ASSOC);

	# Include Page Title and Call to header here for dynamic pages #
	$page_title = $row['title'] .', '. $row['category'];
	#$meta_keywords = $row['meta_keywords'];  //debug
	#$meta_description = $row['meta_description']; //debug

	if (($row['meta_keywords']) !=""){
		$meta_keywords = $row['meta_keywords'];
		echo $meta_keywords;
	} else {
		$meta_keywords = FALSE;	
		echo $meta_keywords;
	}

	if (($row['meta_description']) !=""){
		$meta_description = $row['meta_description'];
		echo $meta_description;
	} else {
		$meta_description = FALSE;
		echo $meta_description;
	}

	include ('./includes/header.html');
	# Include Page Title and Call to header here for dynamic pages #

All good so far, or so I thought! If the article has the meta keywords and description set, the page loads fine. If they're blank, the default should show, instead, my page has the following:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

I cannot work out this simple piece of code & it's now driving me mad! Any help is very much appreciated!!!

 

Thanks in Advanced!

Link to comment
Share on other sites

  $query = "SELECT articles.article_id, articles.description, articles.status, articles.meta_keywords, articles.meta_description, articles.title, article_categories.category, article_categories.article_category_id FROM articles INNER JOIN (article_categories INNER JOIN article_associations ON article_categories.article_category_id = article_associations.article_category_id) ON articles.article_id = article_associations.article_id WHERE articles.article_id = $aid AND status = '1'";

 

If (mysql_num_rows($query) == '0')

 

{

$meta_desc = 'Standard meta desc';

$meta_key = 'Standard meta key';

}

else

{

$row = mysql_fetch_array($query);

$meta_desc = $row['meta_desc'];

$meta_key = $row['meta_desc'];

};

 

 

Link to comment
Share on other sites

Thanks Tommyda, however, the standard/default keywords are pulled from the CMS as it allows them to be updated at any point. Hence why I push the values into the header via isset and then use if else.

 

The issue seems to be with this block of code:

$query = "SELECT articles.article_id, articles.description, articles.status, articles.meta_keywords, articles.meta_description, articles.title, article_categories.category, article_categories.article_category_id FROM articles INNER JOIN (article_categories INNER JOIN article_associations ON article_categories.article_category_id = article_associations.article_category_id) ON articles.article_id = article_associations.article_id WHERE articles.article_id = $aid AND status = '1'";	

$result = mysql_query($query) or die( "Could not execute SQL query" );

if (mysql_num_rows($result) > 0) { // if the query ran ok continue...

	// Retrieve the info from the db
	$row = mysql_fetch_array ($result, MYSQL_ASSOC);

	# Include Page Title and Call to header here for dynamic pages #
	$page_title = $row['title'] .', '. $row['category'] .', LP Gas';
	$meta_keywords = $row['meta_keywords'];
	$meta_description = $row['meta_description'];

	if (($row['meta_keywords']) !=""){
		$meta_keywords = $row['meta_keywords'];
		echo $meta_keywords;
	} else {
		$meta_keywords = FALSE;	
		echo $meta_keywords;
	}

	if (($row['meta_description']) !=""){
		$meta_description = $row['meta_description'];
		echo $meta_description;
	} else {
		$meta_description = FALSE;
		echo $meta_description;
	}

	include ('./includes/header.html');
	# Include Page Title and Call to header here for dynamic pages #

	# Create Breadcrumb #
	echo'<p><a href="index.php">Home</a> > <a href="article_list.php?id=' . $row['article_category_id'] . '">' . stripslashes($row['category']) . '</a> > ' . stripslashes($row['title']) . '</p>';
	# /Create Breadcrumb #

I believe it's because if the meta_keywords/description are empty when it retrieves the info for a given id, I need to make sure that either keywords/description are set to empty and use should then work in the header code i.e.

if(!isset($meta_keywords) || empty($meta_keywords)) { 

Although am no whizz at PHP by any means!

 

 

Cheers

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.