mdmartiny Posted September 23, 2011 Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247718-changing-the-output-in-a-while-loop/ Share on other sites More sharing options...
WebStyles Posted September 23, 2011 Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247718-changing-the-output-in-a-while-loop/#findComment-1272092 Share on other sites More sharing options...
mdmartiny Posted September 23, 2011 Author Share Posted September 23, 2011 That worked perfectly..... Now I am going to break it down and see how it works Quote Link to comment https://forums.phpfreaks.com/topic/247718-changing-the-output-in-a-while-loop/#findComment-1272147 Share on other sites More sharing options...
WebStyles Posted September 23, 2011 Share Posted September 23, 2011 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'])); } Quote Link to comment https://forums.phpfreaks.com/topic/247718-changing-the-output-in-a-while-loop/#findComment-1272156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.