Page 1 of 1

[SOLVED] if statement & $_SERVER['QUERY_STRING'];) help.

Posted: Sun Jul 18, 2004 2:18 am
by Simon Angell
Hi all,
(still don't know what to call the topic, better then HELP NEEDED i guess. lol)
i am challenging my amature brains again, and once again, need some help...

Sample code snippet...

Code: Select all

<?
//NSW Warnings
if ($query = "IDN36501.txt") 
{$state = "NSW";
$filename = "Flood Watch 1.txt";
}
if ($query = "IDN28500.txt") 
{$state = "NSW";
$filename = "NSW Severe Weather Warning.txt";
}
if ($query = "IDN36501.txt") 
{$state = "NSW";
$filename = "Flood Watch 1.txt";
}
if ($query = "IDN28500.txt") 
{$state = "NSW";
$filename = "NSW Severe Weather Warning.txt";
}
if ($query = "IDN36501.txt") 
{$state = "NSW";
$filename = "Flood Watch 1.txt";
}
if ($query = "IDN28500.txt") 
{$state = "NSW";
$filename = "NSW Severe Weather Warning.txt";
}


//VIC Warnings
if ($query = "IDV20200.txt") 
{$state = "VIC";
$filename = "Coastal Wind Warning East of Wilsons Prom.txt";
}

?>
Ok, this above code checks the $query part of a URL ($query = $_SERVER['QUERY_STRING'];), matches it to the *.txt file, adds the $state and changes the $filename - which is then saved onto disk.
Now the above code works fine but the main problem is the number of different $query *.txt files numbers close to 300, and sorted into 7 different $states.
I have setup 7 files like this...
IDS20201 = Coastal Wind Warning 1
IDS20202 = Coastal Wind Warning 2
IDS20210 = Ocean Wind Warning
IDS20230 = Storm Tide Warning
IDS20235 = Storm Tide Advice
IDS20240 = Road Weather Alert
IDS20242 = Sheep Graziers' Advice
IDS20270 = Severe Weather Warning
IDS20280 = Severe Weather Advice 1
IDS20282 = Severe Weather Advice 2
IDS20300 = Flash Flood Warning
IDS20360 = Gawler River Flood Warning
IDS20362 = Torrens Flood Warning
IDS20364 = Onkaparinga Flood Warning
IDS20366 = Brownhill Creek Flood Warning
IDS20370 = Inland Flood Warning
Now this is just a small sample, but i am wanting a script that sees the above, mathces the if ($query) statment with the ID code and performs the $filename change with the corosponding title after the = sign...

Any help in doing this would be much appreciated..

Posted: Sun Jul 18, 2004 2:47 am
by feyd
I'd use [php_man]file[/php_man] and store each file's (I'm going to assume each one is a state) in an associative array like so:

Code: Select all

<?php

$data = array();
$states = array('NSW','QLD','VIC',...);
foreach($states as $state)
  $data[$state] = file($state.".txt");

?>
then just toss through the $data array, looking for the code..

Code: Select all

<?php

foreach($data as $state_i => $file)
{
  foreach($file as $line)
  {
    if(preg_match('#^'.$query.'\s*=\s*(.*?)\s*$#',$line,$match))
    {
       $state = $state_i;
       $filename = $match[1].'.txt';
       break;
    }
  }
  if(isset($filename))
    break;
}

?>

Posted: Sun Jul 18, 2004 5:06 am
by Simon Angell
Im am getting an error with this code,
Notice: Undefined variable: filename in c:\www.canberra-wx.com\wxws\warningsgrab.php on line 51
line51 of that file is as follows

Code: Select all

if( ($fp = fopen( "$dir/$state/$filename", 'wb+' )) !== false )
the start of the writing to local file...

Posted: Sun Jul 18, 2004 5:21 am
by kettle_drum
You mustnt have declaired $filename before you call this - your if statements must have all been false so it hasnt created a filename.

Posted: Sun Jul 18, 2004 5:36 am
by Simon Angell
It is using the code feyd wrote, i assume the code isn't working quiet right in the first place, and therefor being returned false, and then the subsiquent code for $filename isn't doing anything, and the $filename is then coming up undefined?

Posted: Sun Jul 18, 2004 5:47 am
by Simon Angell
oh, and to add using this feyd's code

Code: Select all

<?php 
$query = $_SERVER['QUERY_STRING'];
$data = array(); 
$states = array('NSW','QLD','VIC','SA','QLD','NT','WA','TAS'); 
foreach($states as $state) 
  $data[$state] = file($state.".txt"); 
?>
this error appers...
Notice: Undefined variable: filename in c:\www.canberra-wx.com\wxws\warningsgrab.php on line 51

Warning: fopen(Warnings/TAS/): failed to open stream: Permission denied in c:\www.canberra-wx.com\wxws\warningsgrab.php on line 51
NOW, the first part of the error we are covering above, and the second part is related to that, but the problem is that its only trying to write the $filename into the TAS directory ... the last one in the Array - even if the the State is different as stated?

Posted: Sun Jul 18, 2004 5:51 am
by Simon Angell
its ok, i fixed the problems... the $state and $filename array data was set out like this
IDS20201 = Coastal Wind Warning 1
when it should be like
IDS20201.txt = Coastal Wind Warning 1