Jump to content

Duplicate data showing from single variable


Go to solution Solved by Texan78,

Recommended Posts

Hello, I am having a small issue which I think is just a simple problem but I can't seem to solve it. I have a small simple script that parses a text file of data sorts it through an array and creates variables to display the data in a table.

 

Problem I am having is when I call the script inside the page for it to be displayed with include statement it is showing duplicate data from a single variable. In this case $tnumber[1]. As you can see from this screen shot it shows "Tracking 1 active thunderstorms" twice.

 

GMap2.png

 

 

If I call the script directly in the browser it displays it correctly as shown below. So I am not sure as to why when I include the script in my page it shows duplicate data or if there is something I am doing wrong.

 

GMap1.png

 

 

Here is the code that parses the text file then creates the table to display the data. The variable that is being duplicated is on line 93 but I don't know why.

<?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

// 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 );

// Intialize values
$tracreport = "";
$tnumber = 1;
$alertflag = 0;
$tracwarning = "";

// If there aren't any thunderstorms, then say it

if (preg_match("/No thunderstorms detected/i", $trac)) {
		$tracreport = "<table width='100%' cellpadding='0' cellspacing='0' style='margin-top:15px; background-color:#000; color:#FFF;'>
			<tr>
			    <td style='padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;'>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];

                //$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 = "<table width='100%' cellpadding='0' cellspacing='0' style='margin-top:15px; background-color:#000; color:#FFF;'>
			<tr>
			    <td style='padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;'>No data currently available or thunderstorms detected.</td>
			</tr>
               </table>";
		}

// This builds each line of text for each thunderstorm
// begin your table
$tracreport .=  "<table width='100%' cellpadding='0' cellspacing='0' style='margin-top:15px; background-color:#000; color:#FFF;'>
        <tr>
            <td style='padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;'> Tracking " . $tnumber[1] . " active thunderstorms</td>
        </tr>
        <tr>
            <td>
            <table width='100%' cellpadding='0' cellspacing='0'>
                <tr>
                    <td>
                    <table id='tracReport' width='100%' cellpadding='0' cellspacing='0'>
                        <tr>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Cell ID</td>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Tracking Since</td>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Bearing</td>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Distance</td>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Intensity</td>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Trend</td>
                            <td style='background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;'>Last Strike</td
                        </tr>"; // break the top part of the table
// create your loop
foreach($tid as $k => $v){
         $tracreport .= "<tr>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tid[$k] . "</td>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdtime[$k] . "</td>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdirection[$k] . "  °</td>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $tdistance[$k] . " miles</td>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . strtolower($tintensity[$k]) . "</td>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" .  trim(strtolower($ttrend[$k])) . "</td>
                            <td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" .  trim(strtolower($tactivity[$k])) . "</td>
                        </tr>";
}  // close your loop
   // finish your table
    $tracreport .= "</table>
                    </table>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
    </table>";

      }

echo $tracreport;



?>

-Thanks

your code that you didn't post showing how you are including the file, is likely including the file twice or including it in a loop and it likely doesn't have any $tid array the first time it it is included/looped over so you only get the initial table heading and not the result form the foreach($tid ...) loop.

 

also, $tnumber isn't an array. how come you are trying to reference it using array syntax $tnumber[1]?

I call the script inside the page for it to be displayed with include statement

 

I didn't show the code for how I am including it because it was stated in my OP. It is just a simple include statement. Also for simplicity since the page it is included in is long and has other code in it not relevant. The script is being called like so.

<?php include('tracreportNS.php'); ?>

Like stated in my original post and displayed with a screenshot when you call the script directly in the browser it displays correctly.

 

http://www.mesquiteweather.net/tracreportNS.php

 

I didn't write the initial script. I am just trying to modify it for what I am needing it for as the initial script was written for something else that it being used for in another page. It's not inside the foreach loop because then it will duplicate every time there is new row of data. It is in the initial array at the beginning of the code that  parses the text file to create the variable.

 

What do I need to do to fix this?

 

-Thanks

the code you posted ISN'T where the problem is. you proved that yourself when you browsed directly to it. the problem is in the other file that is including this code. if you cannot determine what in the other code is causing this, you will need to post it or hire someone to fix this for you.

  • Solution

Ok I found the issue. In the inside table the is an id like this "id='tracReport'".

 

In the head of the page I am including this in I have this small jquery script that refreshes the table data.

<script type="text/javascript">
    $(document).ready(function(){
      refreshTable();
    });

    function refreshTable(){
        $('#tracReport').load('tracreportNS.php', function(){
           setTimeout(refreshTable, 30000);
        });
    }
</script>

So I just moved the ID that references that script to the outer table and it is working correctly now.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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