Jump to content

Changing the output in a while loop


mdmartiny

Recommended Posts

I am trying to write some code for my website. I pull information out of a database using a while loop. What am I trying to figure out is how to change some of the information. Some of the information used by the database is not there. It is not there cause I lost it and don't know what to put in place.

 

The missing information is a Date Field. In the database if the field is blank it displays 0000-00-00. On the page it displays 01-01-1970. What I want to do is when the date is 01-01-1970 it actually shows N/A.

 

How would I go about doing this?

 

This is the code that I am using to draw the information from the database.

if ($year == 'other') {
        $sqlC = "SELECT * FROM `ttmautos` WHERE YEAR(date_return) < 2008 ORDER BY `date_return` DESC";
        $resultC = mysql_query($sqlC) or die(mysql_error());
        if (mysql_num_rows($resultC) == 0) {
            echo "There currently are no signatures in the database from $year";
        } else {
            $autographs = "<table id='ttm'>\n";
        $autographs .= "<tr class='first'><th>Player</th><th>Date Sent</th><th>Date Returned</th><th>Item Signed</th><th>Project</th></tr>\n";
        $autographs .= "<tr><td id=\"blank\"></td><td id=\"blank\"></td><td id=\"blank\"></td><td id=\"blank\"></td><td id=\"blank\"></td></tr>\n";
        while ($row = mysql_fetch_assoc($resultC)) {
		$category = $row['category'];
            $fname = $row['f_name'];
            $lname = $row['l_name'];
            $dsent = date('m-d-Y', strtotime($row['date_sent']));
            $dreturn = date('m-d-Y', strtotime($row['date_return']));
            $ireturn = $row['item_return'];
            $project = $row['project'];
            $autographs .= "<tr><td class=\"auto_cell\">$fname $lname</td>";
            $autographs .= "<td class=\"auto_cell\">$dsent</td>";
            $autographs .= "<td class=\"auto_cell\">$dreturn</td>";
            $autographs .= "<td class=\"auto_cell\"><a href=\"signatures.php?c=$category&l=$lname&f=$fname\">$ireturn</a></td>";
            $autographs .= "<td class=\"auto_cell\"><a href='projects/$project.php'>$project</a></td></tr>\n";
            }
            $autographs .= "</table>";
            echo "$autographs";
        }
    }
}

 

I hope that I have given enough information to help me out with this issue.

Link to comment
https://forums.phpfreaks.com/topic/247718-changing-the-output-in-a-while-loop/
Share on other sites

instead of this line:

$dsent = date('m-d-Y', strtotime($row['date_sent']));

 

try something like:

 

$dsent = $row['date_sent'] == '0000-00-00' ? 'N/A' : date('m-d-Y', strtotime($row['date_sent']));

 

* do the same for other dates.

it's called the ternary operator... basically it's exactly the same as doing this: (except it saves typing and fits nicely on one line)

 

if($row['date_sent'] == '0000-00-00'){
   $dsent = 'N/A';
}else{
   $dsent =  date('m-d-Y', strtotime($row['date_sent']));
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.