in reply to mysql database

Ok, I'm going to assume that you have fixed the SQL statements, and the checkbox creations prints, as per instructed in the previous nodes

About the actual algorithm...

If i understand your problem correctly, you want to
print a list of all of the items in @id
and
if this $id[$i] is also in the database (identified by variable $identifier)
print $id with a checked checkbox,
else print $id without an unchecked checkbox.

If this is what you want to do, then...
1. Loop through @id,
1.1 Query database using your $id as a search criteria,
1.2 If there is a match, then
1.2.1 print a checked box with $id
1.3 If there is no match, then
1.3.1 print an unchecked box.

It this is not what you want to do, just forget I mentioned it.

PS: If @id is huge, then another algorithm may be more appropriate, which minimizes all the queries to the database.

My $.02 worth: When I was first learning to program, my mentor insisted that I write my algorithm 1st, and then step through it with 'dummy data' while pretending to be the computer (do what I say, not what I mean). This method works well when you are still at the learning phase of programming.

Replies are listed 'Best First'.
Re: Re: mysql database
by bory (Beadle) on Dec 15, 2003 at 09:22 UTC
    I found how to do it but it's printig the values that are in the database twice:once checked and once unchecked.I don't know how to print them only once as checked
    for (my $i=0; $i<=$#id; $i++){ my $sth=$dbh->prepare("SELECT Data,Identifier,User,Version FROM ddts,d +elivery WHERE `User`=\"@user\" AND `Identifier`=\"$id[$i]\" AND deliv +ery.Data=ddts.Data"); $sth->execute(); while (my ($data,$identifier,$user,$version)=$sth- +>fetchrow_array ) { print "<tr><td><input type=\"checkbox\" name= +\"rt\" CHECKED value=\"$identifier\"> $identifier</td></tr>";} if ($id[$i] ne $identifier){ print "<tr><td><input type=\"checkbox\" name=\"rt\ +" value=\"$id[$i]\"> $id[$i]</td></tr>"; } + $sth->finish; }
    Thank you very much
      Maybe(?) your query match to $identifier has extra spaces?

      I will be willing to help you solve this if you want (later this week, not now), but I would need to know a bit more info.

      Could you give me a sample of the input from the db (not the whole thing please), explain why you are querying based on 'user'=\"@user\", print out the select statement before querying, and print the results (as is) of each row fetched from the database.

      It's possible that just doing this may help you solve the problem yourself.

      UPDATE ... ignore above

      Silly me, over lunch I realized what was wrong (at least I think that this is the problem).

      When you query the database, you use a while loop. When a row is returned, you print the resulting CHECKED box. So far so good.

      But, still within the while loop, a new query is performed, and there is no row returned. Now the $identifier is 'undef', and the while loop is exited.

      So what happens next? The if statement is executed, and $identifier (which is 'undef' because of the last query to the db) is not equal to $id. The condition is true, and the unchecked box is printed.

      There are certainly a lot better ways of doing this than what I am about to suggest, (it's a quick and dirty fix) but it should work.

      PS: Note that I used a placeholder, which many people have mentioned in the other posts.
      PSS: I don't have a db to play with, so I think that this works, but I actually haven't checked the syntax of the execute statement. Please check before using!

      my $sth=$dbh->prepare( "SELECT Data,Identifier,User,Version FROM ddts, ". "delivery WHERE `User`=\"@user\" AND `Identifier`= ?" . " AND delivery.Data=ddts.Data"); foreach $id (@id){ $sth->execute($id); my $found = 0; while (my ($data,$identifier,$user,$version)=$sth->fetchrow_array ) { print "<tr><td><input type=\"checkbox\" name=\"rt\" CHECKED ". "value=\"$identifier\"> $identifier</td></tr>"; $found = 1; } unless ($found){ print "<tr><td><input type=\"checkbox\" name=\"rt\" ". "value=\"$id[$i]\"> $id[$i]</td></tr>"; } } $sth->finish;