Texan78 Posted February 1, 2014 Share Posted February 1, 2014 Hello, I have this able below that is populated via data dynamically via a text file. It works great. What I am wanting to do and I had it like this at one point and did something and now it does work like it use to. I want to hide the header row when there is now data. Please see screen shots for examples. Here is what it looks like with data. Works perfect as it should. Ok here is what it looks like without data. Notice the top row is still showing. I want to hide this row if there is no data but, I can't put it in the loop or it will repeat that row each time, at least that's what I think will happen as I tried it once. Maybe I was doing something wrong. So I want that top row to be hidden if there is no data and only show this when there is no data. Like I said, it use to do this at one point but not sure what I changed to cause this to not work like that anymore. So how can I hide that row when there is no data without adding it in the loop and causing it to repeat for each line of data? Here is the code for the table and loop.... // This builds each line of text for each thunderstorm // begin your table $tracreport .= "<div id='tracReport'><table style='{$tracTable}'> <tr> <td style='{$td1Style}'> Tracking " . $tnumber[1] . " active thunderstorms</td> </tr> <tr> <td> <table width='100%' cellpadding='0' cellspacing='0'> <tr> <td style='{$tr1Style}'>Cell ID</td> <td style='{$tr1Style}'>Tracking Since</td> <td style='{$tr1Style}'>Bearing</td> <td style='{$tr1Style}'>Distance</td> <td style='{$tr1Style}'>Intensity</td> <td style='{$tr1Style}'>Trend</td> <td style='{$tr1Style}'>Current Rate</td> <td style='{$tr1Style}'>Peak Rate</td> <td style='{$tr1Style}'>Total</td> <td style='{$tr1Style}'>Last Strike</td> </tr>"; // break the top part of the table // create your loop foreach($tid as $k => $v){ // pass in storm intensity for current storm switch(strtolower(trim($tintensity[$k]))) { case 'weak': $stormIntensity = 'rgb(0, 255, 0)'; break; case 'moderate': $stormIntensity = 'rgb(255, 255, 0)'; break; case 'strong': $stormIntensity = 'rgb(255, 102, 0)'; break; case 'severe': $stormIntensity = 'rgb(198, 1, 204)'; break; } // replace default color with $stormIntensity $intCat = str_replace('color: #FFF;', "color: $stormIntensity;", $tr2Style); // pass in storm intensity for current storm switch(strtolower(trim($ttrend[$k]))) { case 'weakening': $stormTrend = 'rgb(0, 255, 0)'; break; case 'steady': $stormTrend = 'rgb(255, 255, 0)'; break; case 'intensifying': $stormTrend = 'rgb(255, 0, 0)'; break; } // replace default color with $stormIntensity $treCat = str_replace('color: #FFF;', "color: $stormTrend;", $tr2Style); $tracreport .= "<tr> <td style='{$intCat}'>" . $tid[$k] . "</td> <td style='{$tr2Style}'>" . $tdtime[$k] . "</td> <td style='{$tr2Style}'>" . $tdirection[$k] . "°</td> <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td> <td style='{$intCat}'>" . $tintensity[$k] . "</td> <td style='{$treCat}'>" . $ttrend[$k] . "</td> <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td> <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td> <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td> <td style='{$tr2Style}'>" . $tactivity[$k] . "</td> </tr>"; } // close your loop // finish your table $tracreport .= "</table> </td> </tr> </table></div>"; } echo $tracreport; And here is the entire code in case it helps any. <?php // The TRACreportshort takes Nexstorm's full TRACreport and shortens it to a more // managable size for displaying. // // It chops up the vital storm cell info, places it in arrays, and redisplays it as we wish // // Set some default values $measure = "mi"; // km, nm, or mi depending on your Nexstorm measurements // Let's assign the tables some styles $tracTable = "width:100%; margin-top: 15px; background-color:#000; color:#FFF;"; $td1Style = "padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;"; $tr1Style = "background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;"; $tr2Style = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;"; // Load the TRACreport and remove carriage returns, change the word 'no change' to 'steady' $trac = implode('', file('lightning/nexstorm/TRACReport.txt')); $tracnopara = preg_replace( '|\n|', '=', $trac ); $tracnopara = preg_replace( '|No change|', 'Steady', $tracnopara ); $tracnopara = preg_replace( '|/minute|', ' a min', $tracnopara); // If there aren't any thunderstorms, then say it if (preg_match("/No thunderstorms detected/i", $trac)) { $tracreport = "<table style='{$tracTable}'> <tr> <td style='{$td1Style}'>No nearby thunderstorms detected</td> </tr> </table>"; } else { // If there are thunderstorms, process them, putting them into various arrays // $tnumber[x] is for number of thunderstorms preg_match("/Tracking (.*) thunderstorms/Uis", $tracnopara, $tnumber); // $tid[x] is the ID number of the storm preg_match_all("/Thunderstorm ID (.*) detected/Uis", $tracnopara, $temp); $tid = $temp[1]; // $tdtime[x] is the time detected preg_match_all("/detected (.*)=/Uis", $tracnopara, $temp); $tdtime = $temp[1]; // $tdirection[x] is the direction of the storm preg_match_all("/Storm location bearing (.*) dgr/Uis", $tracnopara, $temp); $tdirection = $temp[1]; // $tdistance[x] is the distance in $measure amounts preg_match_all("/distance (.*) $measure/Uis", $tracnopara, $temp); $tdistance = $temp[1]; //$tintensity[x] is the intensity of the storm preg_match_all("/Intensity class (.*)=/Uis", $tracnopara, $temp); $tintensity = $temp[1]; //$ttrend[x] is the trend of the storm preg_match_all("/Intensity trend (.*)=/Uis", $tracnopara, $temp); $ttrend = $temp[1]; //$tcurraterate[x] Current strikerate preg_match_all("/current strikerate (.*)=/Uis", $tracnopara, $temp); $tcurraterate = $temp[1]; //$tpeakrate[x] Peak strikerate preg_match_all("/peak strikerate (.*)=/Uis", $tracnopara, $temp); $tpeakrate = $temp[1]; //$tpeakrate[x] Total recorded strikes preg_match_all("/total recorded strikes (.*)=/Uis", $tracnopara, $temp); $ttotalstrikes = $temp[1]; //$tactivity[x] Last recorded activity preg_match_all("/recorded activity (.*)=/Uis", $tracnopara, $temp); $tactivity = $temp[1]; // Set working for the header based on number of thunderstorms if ($tnumber[1] == 1){ } else { // Sometimes the TRACreport is empty so we need to say that //$tracreport = "<br />No data currently available or thunderstorms detected.<br/>"; } // This builds each line of text for each thunderstorm // begin your table $tracreport .= "<div id='tracReport'><table style='{$tracTable}'> <tr> <td style='{$td1Style}'> Tracking " . $tnumber[1] . " active thunderstorms</td> </tr> <tr> <td> <table width='100%' cellpadding='0' cellspacing='0'> <tr> <td style='{$tr1Style}'>Cell ID</td> <td style='{$tr1Style}'>Tracking Since</td> <td style='{$tr1Style}'>Bearing</td> <td style='{$tr1Style}'>Distance</td> <td style='{$tr1Style}'>Intensity</td> <td style='{$tr1Style}'>Trend</td> <td style='{$tr1Style}'>Current Rate</td> <td style='{$tr1Style}'>Peak Rate</td> <td style='{$tr1Style}'>Total</td> <td style='{$tr1Style}'>Last Strike</td> </tr>"; // break the top part of the table // create your loop foreach($tid as $k => $v){ // pass in storm intensity for current storm switch(strtolower(trim($tintensity[$k]))) { case 'weak': $stormIntensity = 'rgb(0, 255, 0)'; break; case 'moderate': $stormIntensity = 'rgb(255, 255, 0)'; break; case 'strong': $stormIntensity = 'rgb(255, 102, 0)'; break; case 'severe': $stormIntensity = 'rgb(198, 1, 204)'; break; } // replace default color with $stormIntensity $intCat = str_replace('color: #FFF;', "color: $stormIntensity;", $tr2Style); // pass in storm intensity for current storm switch(strtolower(trim($ttrend[$k]))) { case 'weakening': $stormTrend = 'rgb(0, 255, 0)'; break; case 'steady': $stormTrend = 'rgb(255, 255, 0)'; break; case 'intensifying': $stormTrend = 'rgb(255, 0, 0)'; break; } // replace default color with $stormIntensity $treCat = str_replace('color: #FFF;', "color: $stormTrend;", $tr2Style); $tracreport .= "<tr> <td style='{$intCat}'>" . $tid[$k] . "</td> <td style='{$tr2Style}'>" . $tdtime[$k] . "</td> <td style='{$tr2Style}'>" . $tdirection[$k] . "°</td> <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td> <td style='{$intCat}'>" . $tintensity[$k] . "</td> <td style='{$treCat}'>" . $ttrend[$k] . "</td> <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td> <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td> <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td> <td style='{$tr2Style}'>" . $tactivity[$k] . "</td> </tr>"; } // close your loop // finish your table $tracreport .= "</table> </td> </tr> </table></div>"; } echo $tracreport; ?> Any advice or suggestions would be great. I am sure it is something really simple that I am just overlooking. It has been a few months since I have worked with PHP. -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/285850-hiding-table-headers-in-loops/ Share on other sites More sharing options...
Barand Posted February 1, 2014 Share Posted February 1, 2014 pseudocode <table> <?php if no storms output "no storms" row else output headings rows while stormdata output storm data row endwhile endif ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/285850-hiding-table-headers-in-loops/#findComment-1467326 Share on other sites More sharing options...
Solution Texan78 Posted February 1, 2014 Author Solution Share Posted February 1, 2014 Thank you, that currently similar to how I have it now but it doesn't work correctly. This is what I have done and what I have done in the past but it still isn't working correctly. For my no storm message I have this. if (preg_match("/No thunderstorms detected/i", $trac)) { $noStormMessage = "<table style='{$tracTable}'> <tr> <td style='{$td1Style}'>No nearby thunderstorms detected</td> </tr> </table>"; } else { Then from there is there is a storm and data it goes to this. // If there are thunderstorms, process them, putting them into various arrays // $tnumber[x] is for number of thunderstorms preg_match("/Tracking (.*) thunderstorms/Uis", $tracnopara, $tnumber); // $tid[x] is the ID number of the storm preg_match_all("/Thunderstorm ID (.*) detected/Uis", $tracnopara, $temp); $tid = $temp[1]; // $tdtime[x] is the time detected preg_match_all("/detected (.*)=/Uis", $tracnopara, $temp); $tdtime = $temp[1]; // $tdirection[x] is the direction of the storm preg_match_all("/Storm location bearing (.*) dgr/Uis", $tracnopara, $temp); $tdirection = $temp[1]; // $tdistance[x] is the distance in $measure amounts preg_match_all("/distance (.*) $measure/Uis", $tracnopara, $temp); $tdistance = $temp[1]; //$tintensity[x] is the intensity of the storm preg_match_all("/Intensity class (.*)=/Uis", $tracnopara, $temp); $tintensity = $temp[1]; //$ttrend[x] is the trend of the storm preg_match_all("/Intensity trend (.*)=/Uis", $tracnopara, $temp); $ttrend = $temp[1]; //$tcurraterate[x] Current strikerate preg_match_all("/current strikerate (.*)=/Uis", $tracnopara, $temp); $tcurraterate = $temp[1]; //$tpeakrate[x] Peak strikerate preg_match_all("/peak strikerate (.*)=/Uis", $tracnopara, $temp); $tpeakrate = $temp[1]; //$tpeakrate[x] Total recorded strikes preg_match_all("/total recorded strikes (.*)=/Uis", $tracnopara, $temp); $ttotalstrikes = $temp[1]; //$tactivity[x] Last recorded activity preg_match_all("/recorded activity (.*)=/Uis", $tracnopara, $temp); $tactivity = $temp[1]; // Set working for the header based on number of thunderstorms if ($tnumber[1] == 1){ } // This builds each line of text for each thunderstorm // begin your table $tracreport .= "<div id='tracReport'><table style='{$tracTable}'> <tr> <td style='{$td1Style}'> Tracking " . $tnumber[1] . " active thunderstorms</td> </tr> <tr> <td> <table width='100%' cellpadding='0' cellspacing='0'> <tr> <td style='{$tr1Style}'>Cell ID</td> <td style='{$tr1Style}'>Tracking Since</td> <td style='{$tr1Style}'>Bearing</td> <td style='{$tr1Style}'>Distance</td> <td style='{$tr1Style}'>Intensity</td> <td style='{$tr1Style}'>Trend</td> <td style='{$tr1Style}'>Current Rate</td> <td style='{$tr1Style}'>Peak Rate</td> <td style='{$tr1Style}'>Total</td> <td style='{$tr1Style}'>Last Strike</td> </tr>"; // break the top part of the table // create your loop foreach($tid as $k => $v){ // pass in storm intensity for current storm switch(strtolower(trim($tintensity[$k]))) { case 'weak': $stormIntensity = 'rgb(0, 255, 0)'; break; case 'moderate': $stormIntensity = 'rgb(255, 255, 0)'; break; case 'strong': $stormIntensity = 'rgb(255, 102, 0)'; break; case 'severe': $stormIntensity = 'rgb(198, 1, 204)'; break; } // replace default color with $stormIntensity $intCat = str_replace('color: #FFF;', "color: $stormIntensity;", $tr2Style); // pass in storm intensity for current storm switch(strtolower(trim($ttrend[$k]))) { case 'weakening': $stormTrend = 'rgb(0, 255, 0)'; break; case 'steady': $stormTrend = 'rgb(255, 255, 0)'; break; case 'intensifying': $stormTrend = 'rgb(255, 0, 0)'; break; } // replace default color with $stormIntensity $treCat = str_replace('color: #FFF;', "color: $stormTrend;", $tr2Style); $tracreport .= "<tr> <td style='{$intCat}'>" . $tid[$k] . "</td> <td style='{$tr2Style}'>" . $tdtime[$k] . "</td> <td style='{$tr2Style}'>" . $tdirection[$k] . "°</td> <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td> <td style='{$intCat}'>" . $tintensity[$k] . "</td> <td style='{$treCat}'>" . $ttrend[$k] . "</td> <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td> <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td> <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td> <td style='{$tr2Style}'>" . $tactivity[$k] . "</td> </tr>"; } // close your loop // finish your table $tracreport .= "</table> </td> </tr> </table></div>"; } Then I put it all together like this. //If no storms were in the source, set no storm message if(!$tracreport) { $tracreport = $noStormMessage; } echo $tracreport; But when there is no data it shows an empty table and not the no storm message table from the beginning of the code. I am sure it is something easy I am missing but not sure what. -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/285850-hiding-table-headers-in-loops/#findComment-1467354 Share on other sites More sharing options...
Texan78 Posted February 1, 2014 Author Share Posted February 1, 2014 Well I can't edit the the above post but I got it sorted. I was missing a period before the = sign on the nostormmessage. $noStormMessage .= All working great now. Quote Link to comment https://forums.phpfreaks.com/topic/285850-hiding-table-headers-in-loops/#findComment-1467356 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.