Page 1 of 2
IF statement - How do I use a wildcard?
Posted: Thu Aug 03, 2006 7:48 pm
by fatman
I am trying to change the backgound color of the table cell where this info is stored, if the line returned from the DB is anything from Google. I was thinking using the wildcard (*) like in mySQL statements. The syntax is a problem though?
Code: Select all
<td <?php if ($row_myStats['referer'] == 'http://www.google.pt/search?q=alojamentos%2Beconomicos%2Balgarve&hl=pt-PT&lr=&start=10&sa=N') {echo "bgcolor=#FF0000";} ?>><?php echo $row_myStats['referer']; ?></td>
What I was thinking was something like this:
Posted: Thu Aug 03, 2006 8:04 pm
by LiveFree
Code: Select all
if (preg_match("#(.*?)Google(.*?)#i", $text)){
// You passed!
}
That will fire if the word Google is ANYWHERE in the $text var
Posted: Thu Aug 03, 2006 8:14 pm
by Ollie Saunders
Code: Select all
if (preg_match('/^(http://)(www\.)?.*(google\.).*/i', trim($text))){
// You passed!
}
might be better.
Stunning, Thanks
Posted: Thu Aug 03, 2006 8:17 pm
by fatman
Works like a charm. I see exactly what you did, but I would never have found that myself
Thanks
Now I want to use it in a mySQL statement
Posted: Thu Aug 03, 2006 9:55 pm
by fatman
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Now I want to use it in a mySQL statement
I want to count the records in this query. I tried this, but it doesn't work?
Code: Select all
$google = preg_match("#(.*?)Google(.*?)#i", $row_myStats['referer']);
mysql_select_db($database_sunrise, $sunrise);
$query_google = "SELECT referer FROM myStats WHERE referer = '$google'";
$google = mysql_query($query_google, $sunrise) or die(mysql_error());
$row_google = mysql_fetch_assoc($google);
$totalRows_google = mysql_num_rows($google);
Then I tried to show it like this:
Code: Select all
<?php echo $totalRows_myStats_counter ?>
Like I said, doesn't work
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Aug 03, 2006 10:07 pm
by feyd
I'm failing to see where $totalRows_myStats_counter is created.
Sorry, yes
Posted: Thu Aug 03, 2006 10:26 pm
by fatman
I'm inside my own head, so I see what I'm thinking ....
It looks like this:
Code: Select all
<?php
mysql_select_db($database_sunrise, $sunrise);
$query_myStats = "SELECT * FROM myStats WHERE `no` > 1 AND `host` <> '$ip_skip' AND host LIKE '$ip_show' ORDER BY `no` DESC";
$myStats = mysql_query($query_myStats, $sunrise) or die(mysql_error());
$row_myStats = mysql_fetch_assoc($myStats);
$totalRows_myStats = mysql_num_rows($myStats);
$google = preg_match("#(.*?)Google(.*?)#i", $row_myStats['referer']); // To define what we discussed
mysql_select_db($database_sunrise, $sunrise);
$query_google = "SELECT referer FROM myStats WHERE referer LIKE '$google'";
$google = mysql_query($query_google, $sunrise) or die(mysql_error());
$row_google = mysql_fetch_assoc($google);
$totalRows_google = mysql_num_rows($google);
?>
And then I want to show it
Only now I see I posted this last piece of code wrong, sorry. I am trying to use it like this but no luck!
Posted: Thu Aug 03, 2006 10:40 pm
by feyd
Are you sure you want to use the return value of preg_match()?
That's a hint, if you didn't get it.
I'm a bit lost here
Posted: Thu Aug 03, 2006 10:56 pm
by fatman
I have many lines in my DB that read something like this:
http://www.google.pt/search?q=alojament ... rt=10&sa=N
This is in the field: 'referer' and they all differ, obviously
I wanted to count how many lines contain the word google
After I thought about it:
STUPID STUPID STUPID STUPID I dont smack myself enough.
Code: Select all
$query_google = "SELECT referer FROM myStats WHERE referer = '%google%'";
Works like a charm. I wrote something like this 2 years ago.....
Actually, NO!
Posted: Thu Aug 03, 2006 11:01 pm
by fatman
Code: Select all
$query_google = "SELECT referer FROM myStats WHERE referer LIKE '%google%'";
Works
Posted: Fri Aug 04, 2006 12:47 am
by nickvd
Yes, but what would happen if the referrer was some how set to: "lafkjflkjf29083jr9w8j9wjgoogledf09s8ujsd09f8jdsa98djf" It should match, and that wouldnt be good...
Read up on preg_match and try doing a var_dump() or a print_r() on what it returns...
So?
Posted: Fri Aug 04, 2006 1:21 am
by fatman
I'm afraid I dont know what you are trying to say
Posted: Fri Aug 04, 2006 1:25 am
by jmut
LiveFree wrote:Code: Select all
if (preg_match("#(.*?)Google(.*?)#i", $text)){
// You passed!
}
That will fire if the word Google is ANYWHERE in the $text var
BTW: Use this instead of such preg_match
Should be much faster
OK?
Posted: Fri Aug 04, 2006 1:49 am
by fatman
Gotha!
What is the advantage of the one over the other?
Re: OK?
Posted: Fri Aug 04, 2006 2:03 am
by jmut
fatman wrote:Gotha!
What is the advantage of the one over the other?
not sure if you were refereing to
but as I mentioned should be much faster. and my comment was just btw
