Two queries - best way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Two queries - best way?

Post by psurrena »

My goal is here is to simplify. There are two sections to a website, "news" and "events". I have "view.php" which I want to use to view both of them. When you are at view.php, the URL would be "view.php?nid=XX" OR "view.php?eid=XX".

While I know my code below is not 100% funcitional, is using this method a good idea? A bad habit to get into?

Code: Select all

<?php
	require 'includes/connect.inc';
 	
	if (nid == TRUE) {					
		$nid = $_GET['nid'];
	
		$query  = "SELECT * FROM news WHERE nid='$nid'"; 
	    $result = mysql_query($query) or die('Error : ' . mysql_error());  
		$row 	= mysql_fetch_array($result, MYSQL_ASSOC);

		$ntitle = $row['ntitle'];
		$nfull  = $row['nfull'];
		$ndate	= $row['ndate'];
	
		$ntimestamp = strtotime($ndate);								
		$ndate = date('F d Y', $ntimestamp);
	
		$news   = '<p class="title-big">' . $ntitle . '</p>' . $ndate;
		$news  .= '<p class="text-body">' . $nfull . '</p>';
	
		echo $news;
	
	} elseif (eid == TRUE) {
		$eid = $_GET['eid'];
	
		$query  = "SELECT * FROM events WHERE eid='$eid'"; 
	    $result  = mysql_query($query) or die('Error : ' . mysql_error());  
		$row 	 = mysql_fetch_array($result, MYSQL_ASSOC);

		$etitle	      = $row['etitle'];
		$edate        = $row['edate'];
		$etime		  = $row['etime'];
		$edescription = $row['edescription'];
	
		$etimestamp = strtotime(edate);								
		$edate = date('F d Y', $etimestamp);
	
		$events   = '<p class="title-big">' . $etitle . '</p>' . $edate;
		$events  .= '<p class="text-body">' . $edescription . '</p>';
	
		echo $events;
	}
	
	mysql_close()
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Two queries - best way?

Post by Christopher »

psurrena wrote:My goal is here is to simplify. There are two sections to a website, "news" and "events". I have "view.php" which I want to use to view both of them. When you are at view.php, the URL would be "view.php?nid=XX" OR "view.php?eid=XX".

While I know my code below is not 100% funcitional, is using this method a good idea? A bad habit to get into?
First I think encoding information into both the key and the value will be less flexible in the long run. There are places where you can do it, but in general use unique keys for each value you want.

The convention many web developers are moving towards is to specify the "controller" and "action" needed for page building. These map onto a class name and method name so you can follow the Front Controller pattern to solve the problem. In your case there could be "news" and "events" controllers and the actoin is the ID, or you would have an article viewer where "news" and "events" were the actions. WIth clean URLs that might look like http://www.mysite.com/article/news/XX/ or just http://www.mysite.com/news/XX/
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

That's sounds like that would be the path I would like to be on. Is there a specific post or site that deals with that in particular?
Post Reply