New to PHP, need help with code

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
mattlopez
Forum Newbie
Posts: 2
Joined: Thu Jul 15, 2010 10:36 am

New to PHP, need help with code

Post by mattlopez »

Hi,

I am very new to PHP and have been asked to make a change to a website that was built 7 years ago.

There is a press page at:
http://lindastein.com/press/index.php

The way this was set up, you would enter the title, the date, the article, etc into a content management system. When you view it online, you would click a link (made up of the title) that would take you to the article. Some of these entries do not have an article. Is there a way to change the php so that if there is no article the title would not become a link?

Thanks for any help you can give.

Here is the code:

Code: Select all

<?php
	# Press
	# $Id: Exp $

	require_once("../../includes/prepend.php");

	# page vars
	$_page['title'] = "Press";

	$p = trim($_GET['p']);
	if (!isset($db)) $db = new DB_sd;


	if ($p != "") { ## SHOW ARTICLE
		# GET IMAGES
		$sql = sprintf("SELECT id, src, caption, alt, ord FROM tblPressImage WHERE active=1 && pressID='%s'", $p);
		$db->query($sql);
		while($db->next_record()) {
			$pressImage[$db->f("ord")]['id'] = $db->f("id");
			$pressImage[$db->f("ord")]['src'] = $db->f("src");
			$pressImage[$db->f("ord")]['caption'] = $db->f("caption");
			$pressImage[$db->f("ord")]['alt'] = $db->f("alt");
			$pressImage[$db->f("ord")]['align'] = $db->f("align");
		}

		# GET ARTICLE
		$sql = sprintf("SELECT title, body, author, affiliation, publication, issue, section, date FROM tblPress WHERE active=1 && ID='%s'", $p);
		$db->query($sql);

		if ($db->num_rows() > 0) {
			dispTop();
			$db->next_record();

			$author = trim($db->f("author")) != "" ? trim($db->f("author")) : FALSE;
			$affiliation = trim($db->f("affiliation")) != "" ? trim($db->f("affiliation")) : FALSE;
			$publication = trim($db->f("publication")) != "" ? trim($db->f("publication")) : FALSE;
			$issue = trim($db->f("issue")) != "" ? trim($db->f("issue")) : FALSE;
			$section = trim($db->f("section")) != "" ? trim($db->f("section")) : FALSE;
			$date = trim($db->f("date")) != "" ? trim($db->f("date")) : FALSE;

			# Header
			if ($author	|| $affiliation || $publication || $issue || $section || $date) {
				$aHead = $author;
				$aHead .= $author && $affiliation ? " - " : "";
				$aHead .= $affiliation;
				$aHead = $aHead ? sprintf('<span class="articleAuthor">%s</span>', $aHead) : "";
				$aHead .= $aHead && $publication ? "<br>" : "";
				$aHead .= $publication;
				$aHead .= $publication && ($issue || $section) ? " - " : "";
				$aHead .= $issue;
				$aHead .= $issue && $section ? ", " : "";
				$aHead .= $section;
				$aHead .= $aHead && $date ? "<br>" : "";
				$aHead .= $date;
			} else {
				$aHead = FALSE;
			}

			# Body
			$aBody = formatBodyText($db->f("body"));

			if (isset($pressImage)) {
				$img = '<table cellspacing="0" cellpadding="0" border="0" align="right" width="150" style="margin-left: 15px;">';

				reset($pressImage);
				foreach($pressImage as $k=>$v) {
					$img .= sprintf('<tr><td align="right"><img src="/images/getImg.php?t=press&i=%s" alt="%s" title="%s" border="0"><div class="caption">%s</div></td></tr>',
						$v['id'],
						$v['alt'],
						$v['alt'],
						$v['caption']);
				}

				$img .= "</table>";
				$aBody = $img.$aBody;
			}

			# output to browser
			print('<table cellspacing="0" cellpadding="0" border="0" width="694" align="center"><tr><td>');
			printf('<div class="articleTitle">%s</div>',
				$db->f("title"));
			if ($aHead) printf('<div class="articleHeader">%s</div>', $aHead);
			printf('<div class="articleBody">%s</div>', $aBody);
			print('<br><a href="/press/index.php">Read More</a>');
			print('</td></tr></table>');
		} else {
			pPressList();
		}
	} else { ## SHOW LIST
		pPressList();
	}

	dispBottom();

	function pPressList() {
		global $_page, $db;

		$_page['subtitle'] = "CLICK ON EACH LINK TO READ FROM THE ARTICLE";

		# GET LIST
		if (!isset($db)) $db = new DB_sd;

		$sql = sprintf("SELECT id, title, publication, date, abstract, author FROM tblPress WHERE active=1 ORDER BY ord DESC");
		$db->query($sql);

		dispTop();

		print('<div class="pageTitle" style="margin: 0 0 10 25px;"><span class="pageSubtitle">PUBLISHED IN NEWSPAPERS, MAGAZINES & JOURNALS</span></div><table cellspacing="0" cellpadding="0" border="0" width="694" align="center">');
		while($db->next_record()) {
			printf('<tr><td colspan="2"><a href="/press/index.php?p=%s" class="listTitle">%s - %s - <i>%s</i>, %s</a></td></tr><tr><td><img src="/images/1x1.gif" width="15" height="1" border="0" alt=""></td><td><span class="listAbstract">%s</span></td></tr><tr><td><img src="/images/1x1.gif" width="1" height="10" border="0" alt=""></td></tr>',
				$db->f("id"),
				$db->f("author"),
				$db->f("title"),
				$db->f("publication"),
				$db->f("date"),
				$db->f("abstract"));
		}
		print('</table>');
	}
?>
Last edited by mattlopez on Thu Jul 15, 2010 2:17 pm, edited 1 time in total.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: New to PHP, need help with code

Post by JakeJ »

That's a lot of code to slog through and my brain is fried right now, but here's a couple of suggestions.

First, when you post code, please click the PHP Code button and paste the text between it to make it more readable.

As for the URL's, find the place in your code that creates the url for the title. Then it's a simple matter of putting in an if statement.

Code: Select all

If ($article == "something"){
 //add URL to title
 echo $title;
}
Else {
 echo $title;
}
Give it a shot and then post the code again along with the specific part you're having trouble with.
mattlopez
Forum Newbie
Posts: 2
Joined: Thu Jul 15, 2010 10:36 am

Re: New to PHP, need help with code

Post by mattlopez »

Thanks Jake,

I'll give it a try and let you know how it goes.

Matt
Post Reply