Gossamer Forum
Home : General : Perl Programming :

Skip entry in a MySQL while { }

Quote Reply
Skip entry in a MySQL while { }
Does anyone know if the following would work;

Code:
while (my $hit = $sth2->fetchrow_hashref) {

if ($IN->cookie('Advert1')) { next; }

push(@grabbed_ids, $hit->{ID}); # push the ID into an array...

}

The problem I am having, is that it doesn't seem to be going to the 'next' entry Unsure

Am I doing this correctly?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Well firstly if the cookie exists you'll end up skipping every single loop iteration. Nothing happens to the cookie inside the loop, therefore it always exists and so next; will always be called.

The logical reason for next not being called is that the cookie has expired or doesn't exist Crazy

Last edited by:

Paul: Feb 28, 2003, 2:48 AM
Quote Reply
Re: [Paul] Skip entry in a MySQL while { } In reply to
Yeah...thats what I thought.

I've narrowed it down now to the fact the cookie isn't set. Can anyone see anything wrong with the code I have to assign the cookie?

Code:
my $cookie;
my $used_id = $grabbed_ids[$rand];

if ($AdNumber == 1) {
$cookie = $IN->cookie( -name => 'Advert1', -value => "ID: $used_id", -path => '/', -expires => 30);
} elsif ($AdNumber == 2) {
$cookie = $IN->cookie( -name => 'Advert2', -value => "ID: $used_id", -path => '/', -expires => 30);
} elsif ($AdNumber == 3) {
$cookie = $IN->cookie( -name => 'Advert3', -value => "ID: $used_id", -path => '/', -expires => 30);
} elsif ($AdNumber == 4) {
$cookie = $IN->cookie( -name => 'Advert4', -value => "ID: $used_id", -path => '/', -expires => 30);
} else {
$cookie = $IN->cookie( -name => 'Advert5', -value => "ID: $used_id", -path => '/', -expires => 30);
}

# save the cookie...
print $IN->header( -cookie => $cookie );

I'll keep looking into this....but I'm just confused as to why the cookie isn't being set. Its using pretty much the same method as all my other Perl scripts Unsure I don't think its the fact that the $cookie is being set inside an 'if', but more that $IN->header( -cookie => $cookie ) isn't doing the right job :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Well you could chop all that down to one line.

Code:
$cookie = $IN->cookie( -name => "Advert$AdNumber", -value => "ID: $used_id", -path => '/', -expires => 30);

...and seconly I'm guessing the reason for your problem is your invalid use of -expires
Quote Reply
Re: [Paul] Skip entry in a MySQL while { } In reply to
Ok...I think it is down to the fact that the cookie is not being written too correctly. I turned on debugging, which prints out a header before the cookie is set....yet I didn't recieve an error message saying the cookie couldn't be set.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Paul] Skip entry in a MySQL while { } In reply to
Ok...this is a real pain in the arse. I wonder if there is a different way to record ID's that have been used....without using cookies Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Paul] Skip entry in a MySQL while { } In reply to
>>>...and seconly I'm guessing the reason for your problem is your invalid use of -expires <<<

Whats wrong with it? Unsure I need the cookie to expire in 30 seconds...

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
BTW: Removing the -expires part (i.e so it saves the cookie in browser memory), still means the cookie doesnt get set :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Quote:
Whats wrong with it? I need the cookie to expire in 30 seconds...

Yah, and how does your browser know you are setting 30 seconds and not 30 days?
Quote Reply
Re: [Paul] Skip entry in a MySQL while { } In reply to
Whoops...been doing too much PHP cookie stuff Tongue Still hasn't sorted it though :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Ok... I'm completly lost as to how to do this now. It doesn't look like I can use cookies...cos a header was already printed Unsure

Mmmm...

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
I've just been reading up...and I think sessions is going to be my best answer.... lets hope this option works Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Well, I'm kinda getting there. I'm now writing a session file, acording to their IP address....i.e 123_1.2.3.4

The problem I am now having, is the same as I started with Tongue I am trying to work out how to go onto the next record...I have;

Code:
while (my $hit = $sth2->fetchrow_hashref) {

# see if this advert already exists on the page...if it does,
# then goto the next one...
# grab out sessions ID numbers...split 'em, and compare...
open (FILE, "admin/sessions/123_$ENV{'REMOTE_ADDR'}") or return "Can't read session file. Reason: $!";
my @data = <FILE>;
close(FILE);

my $found = 0;
foreach (@data) {
chomp $_;
print "comparing: $_ == $hit->{ID}<BR>";
if ($hit->{ID} == $_) { $found = 1; }
}

if (!$found) {
push(@grabbed_ids, $hit->{ID}); # push the ID into an array...
}

if ($debug) { print "Grabbing links: $hit->{ID} <BR>"; }
}

The above code, is basically a 'while' loop....it goes through the data grabbed from MySQL, and then opens up the session file. It reads the session file, which looks something like;

3
2
5
4
6

The idea of the above code (with the session stuff) is to try and get ONLY unique ID's. The problem I am having is, that when I use the 'foreach', and then a 'next' to jump to the next record...its only do it for the 'foreach', rather than the 'while'. Am I making sense so far? Unsure

Basically, I need a way to get the while {} loop to go onto the next record, without pushing the current record into the array :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Andy, because the NEXT call is inside another looping function, you need to name the loop you want NEXT to apply to.

Code:
STH: while ($this == $stuff) {
#code
foreach (@thing) {
next STH if $whatever;
#code
}
#code
}

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] Skip entry in a MySQL while { } In reply to
Thanks Andrew. I've always wondered how that STUFF: worked. I'll have to remember that for the future.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Skip entry in a MySQL while { } In reply to
Perldoc.com is there so you don't have to remember.

http://www.perldoc.com/...8.0/pod/perlsyn.html