PDA

View Full Version : Compare a string variable, I think


2bz2p
05-28-2008, 09:56 PM
What I am trying to do is compare the data in one cell and write an if statement that says if this is XXXX-XXXX then dont show a map but if the data is XXXX-XXXX-XXXX or XXXX-XXXX-XXXX-XXXX then show the map.

the Code is stuck because I havent been able to figure it out the asdf is frustration and walked away with it like that :roll:


<?php if ($row_pirep[route] !== is_string( asdf)) {?>
<div class="sum_img"><img src="http://gc.kls2.com/cgi-bin/gcmap?path=<?php echo $row_pirep[route] ;?>" width="455" height="262" /><br />
Maps generated by the
<a href="http://gc.kls2.com/">Great Circle Mapper</a>&nbsp;-
copyright <a href="http://www.kls2.com/~karl/">Karl L. Swartz</a>. </div></div>
<?php }?>


Thanks
2b

davidj
05-28-2008, 10:05 PM
<?php if ($row_pirep['route'] == "string" ) {?>

<div class="sum_img"><img src="http://gc.kls2.com/cgi-bin/gcmap?path=<?php echo $row_pirep[route] ;?>" width="455" height="262" /><br />
Maps generated by the
<a href="http://gc.kls2.com/">Great Circle Mapper</a>&nbsp;-
copyright <a href="http://www.kls2.com/~karl/">Karl L. Swartz</a>. </div></div>

<?php }?>


you can use === if your wanting to be pedantic and test for type also

davidj
05-28-2008, 10:11 PM
An alternative


<?php if(!(strcmp($row_pirep['route'], "string"))) {?>




<?php } ?>

2bz2p
05-28-2008, 10:32 PM
now when you say string you mean the pattern right?

So for example (I think thats the right pattern)


<?php if ($row_pirep['route'] == "....\-...." ) {?>

<div class="sum_img"><img src="http://gc.kls2.com/cgi-bin/gcmap?path=<?php echo $row_pirep[route] ;?>" width="455" height="262" /><br />
Maps generated by the
<a href="http://gc.kls2.com/">Great Circle Mapper</a>&nbsp;-
copyright <a href="http://www.kls2.com/~karl/">Karl L. Swartz</a>. </div></div>

<?php }?>

davidj
05-28-2008, 10:33 PM
are you matching the pattern or the actual contents of the string

davidj
05-28-2008, 10:40 PM
$string = "888-667";

if ($row_pirep['route'] == $string ) {



OK as you can see above the condition would be true if .....


$row_pirep['route'] == "888-667"


the conditional statement above has no understanding of patterns so it is matching the string literally.

2bz2p
05-28-2008, 11:37 PM
Sweet I learned something new, though it would be a pattern, though no it wouldnt it would be, See with having very little php knowledge I tend to play things out in my head and then put it in code and my thought process orriginally was to exclude non-stop flights though if it is like the example I would want to keep it.

Now when the pirep form posts the data to the db I have it set to change the case to uppercase and to allow only 4 letters (because the internation airport codes are only 4 letters).

example

KDOV-KDOV will not give you a route because the starting airport is the same as the ending airport so I wouldnt want to post the map image, though if the person flew KBOS-KATL then it would show you a route on the map and I would like it to show the map.

so in theory I am trying to say if the start airport is KDOV (or whatever airport) and the end airport is the same then do not show the map. which in this case I need to add some kind of wildcard because their are a bunch of ICAO Codes that can be used.

thanks again
2b

davidj
05-29-2008, 05:53 AM
so are you saying that the variable which contains the airport code looks like this...

KDOV-KDOV

and you want to detect if the 2 seperate codes are the same?

so you have 1 variable which looks like the above and you want to split it in order to compare

pete
05-29-2008, 08:33 AM
I would do this:


$str = "KDOV-QWTT";
$bits = explode("-",$str);
$match = preg_match("/^[A-Z]{4}-[A-Z]{4}$/",$str);
if($match && $bits[0]!=$bits[1]){
echo 'OK';
} else {
echo 'not OK';
}


change $str = "KDOV-QWTT"; to $str = "KDOV-KDOV"; and also malform the str to $str = "KDOV-23;1" for example, this will ensure it checks for the correct pattern 4 upper case letters hyphen 4 upper case letters.

2bz2p
05-29-2008, 04:26 PM
I gotcha now just through it in dreamweaver, I will see what I can come up with from here and also research all the new commands. Now I know where to start and I will post the code once I am done

Thank you again yall
2b

2bz2p
05-29-2008, 05:06 PM
Sorry for the double post but I got it.

I used a switch beacuse of DJ's Tut, and it made sense to me.

2 things though,

a. I dont need to check case because after the form is submitted it checks it and submits it to the db correctly. Though that was a nice lesson on comparison, though what does preg_match do? couldnt find it in my book and why do you have / in the comparison portion "/^[A-Z]{4}-[A-Z]{4}$/"


b. the users have 3 options (non-stop, one-stop, and two-stop) so the comparison $match variable wouldnt allow for the 3 ICAOs and 4 ICAO's.

But all in all here is what I came up with, let me know what yall think,
(if you want run it, there is a guy who wrote a db of every airport in the world and then produces a map and statistical info)


$row_pirep = "KBOS-KDOV-KATL" ;
$str = $row_pirep;
$icao = explode("-",$str);
switch($route){

case ($icao[0] == $icao[1]):
echo '<img src="http://gc.kls2.com/cgi-bin/gcmap?path=';
echo $row_pirep;
echo ' " width="455" height="262" /><br /> Maps generated by the
<a href="http://gc.kls2.com/">Great Circle Mapper</a>&nbsp;-
copyright © <a href="http://www.kls2.com/~karl/">Karl L. Swartz</a>' ; break;

case ($icao):
echo '<img src="http://gc.kls2.com/cgi-bin/gcmap?path=';
echo $row_pirep;
echo ' " width="455" height="262" /><br /> Maps generated by the
<a href="http://gc.kls2.com/">Great Circle Mapper</a>&nbsp;-
copyright © <a href="http://www.kls2.com/~karl/">Karl L. Swartz</a>' ; break;



return;
};


Thanks again
2b

pete
05-29-2008, 06:07 PM
http://uk.php.net/preg_match