Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insiering Within a Loop on Change of Value 1

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I have a relatively simple function that lists tracks of recordings and on occasion the recording is multiple-disk or on several sides if it happens to be vinyl. The function is working but I'm having a difficult time trying to figure out how to make the ordered list numbering start over for the second or more sides. The field Side is an integer value of the side: 1, 2, 3, 4 etc. If no side change, then it is always 1 for any given album.

The bit marked in red is the portion I can't figure out. Any ideas?

Code:
function TrackListing ($RecordID, $DBconn)  {

	global $SongName;

	if ($RecordID && $DBconn)  {
		$TracksAssigned = DLookup("TracksAssigned", "recordings", "ID=".$RecordID, $DBconn);
		$PartialListing = DLookup("PsrtialListing", "song_assignments", "AssignID=".$TracksAssigned, $DBconn);
		$TrackList = "<div class=\"HeaderCaptions\">Tracks</div>\n\n<div class=\"ListTracks\">\n";

		if ($TracksAssigned) {
			$Query = "SELECT s.Title, s.Composer, s.PlayTime, sa.ListCaption, sa.SubCaption, sa.Side, s.PrevUnreleased, r.SpecialTracks
								FROM songs s, song_assignments sa, recordings r
								WHERE TrackID = s.ID
								AND AssignID = TracksAssigned
								AND r.ID = $RecordID 
								ORDER BY Side, TrackOrder";

			$DBconn->query($Query);

			$TrackList .= ($PartialListing == 1)  ? "<ul>":"<ol>"; 

			while ($DBconn->next_record())  {
				$Title = $DBconn->f("Title");
				$Composer = $DBconn->f("Composer");
				$Playtime = $DBconn->f("PlayTime");
				$Unreleased = $DBconn->f("PrevUnreleased");
				$ListCaption = $DBconn->f("ListCaption");
				$SubCaption = $DBconn->f("SubCaption");
				$Side = $DBconn->f("Side");
				$SpecialTracks = $DBconn->f("SpecialTracks");

				$Composer = ($Composer) ? "&nbsp;&nbsp;&nbsp;($Composer)":"";
				$Playtime = ($Playtime) ? " - $Playtime":"";
				$PrevUnreleased = ($Unreleased == 1) ? "&nbsp;***":"";

				$TrackList .= ($ListCaption && $ListCaption != "NULL")  ? "<p><strong>$ListCaption</strong>":"";
				$TrackList .= ($SubCaption && $SubCaption != "NULL")  ? "<strong>$SubCaption</strong>":"";
				$TrackList .= "<li><strong>" . $Title . $PrevUnreleased . "</strong>" . $Composer . $Playtime . "</li>\n";
				[COLOR=red]$TrackList .=  (($ListCaption  || $SubCaption != "NULL")  && $PartialListing != 1 && $Side > 1) ? "</ol>\n\n<ol>": "</ul>\n\n<ul>";[/color]
			}
		} elseif ($SpecialTracks) {
			$TrackList .= $SpecialTracks;
		}
	}

	$TrackList .= ($PartialListing == 1) ? "</ul>\n\n":"</ol>\n\n";
	$TrackList .= "</div>\n";

	if ($Unreleased == 1) {
		$TrackList .= "<p><strong>*** Previously unreleased</strong>";
	}

	return $TrackList;
}
 
Got it! A slight rearranging and modification of the conditionals did the trick:

Code:
$TrackList .=  ($ListCaption && $Side > 1 && ($ListCaption != "NULL" || $SubCaption != "NULL")) ? "</ol>\n\n<ol>\n":""; 
$TrackList .= ($ListCaption && $ListCaption != "NULL")  ? "<p><strong>$ListCaption</strong>\n\n":"";
$TrackList .= ($SubCaption && $SubCaption != "NULL")  ? "<strong>$SubCaption</strong>\n\n":"";
$TrackList .= "<li><strong>$Title $PrevUnreleased</strong>$Composer $Playtime</li>\n";
 
you are testing whether a value is equal to the STRING "NULL". This is not the same as the php NULL value.

to test for null use this

Code:
if(is_null($var)):
endif;

to test whether something is empty use this
Code:
if(empty($var)):
endif;

and to test whether a variable exists, use this
Code:
if(isset($var)):
endif;

note that 'empty' has a particular meaning in php. for example the following constitute empty:
"" //empty string
0 //0 as a number (including 0.0 etc
NULL // the php null value
FALSE // the boolean false
array() // an empty array
// also an instantiated but unset variable evaluates to empty.

read more here

also isset needs special handling since it will evaluate to false both if a variable is not set and if a variable's value has been set to the PHP NULL constant.

note also that if you use is_null on an non-existent variable you will get a warning. so if there is a doubt that the variable exists, and you really need to test for NULL (and thus cannot use empty()) then you must do this to avoid the warning
Code:
if(isset($var) && !is_null($var)):

endif;
no warning is thrown because conditionals evaluate left to right, and an unset variable fails the first limb.
 
Thank you for that. In know all that already but a reminder never hurts! In this case, NULL is valid as it is the default of the database field so it's either NULL or it has a value. In other words, it is evaluating the field content, not the variable value.
 
unless the column default is a string 'NULL' (which would be very odd indeed), php will evaluate mysql null columns as the PHP NULL constant. i.e. a test against the string value of NULL.

for example
Code:
echo NULL == "NULL";
//will yield boolean false.

dissecting your first code line as an example

Code:
$ListCaption && $Side > 1 && ($ListCaption != "NULL" || $SubCaption != "NULL")) ? "</ol>\n\n<ol>\n":"";

this is testing for whether $ListCaption is evaluating to boolean false. a NULL value in this context will evaluate to a boolean false. so if the value is NOT NULL, then you pass the first test and the middle test becomes nugatory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top