andrew has asked for the wisdom of the Perl Monks concerning the following question:

print qq~ <p><b>Shopping Cart Manager</b></p> <table align="center" width="370" cellpadding="0" cellspacing="0"> <td colspan="4"> <table align="center" width="370" cellpadding="4" bgcolor="black +" cellspacing="1"> <tr> <td class="text" bgcolor="#3399FF" width="200" align="center"> <b>Category</b> </td> <td class="text" bgcolor="#3399FF" width="50" align="center"> <b>Sub</b> </td> <td class="text" bgcolor="#3399FF" width="50" align="center"> <b>Item</b> </td> <td class="text" bgcolor="#3399FF" width="70">&nbsp;</td> </tr> ~; if($id) { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +$id'"); $sth->execute or die $dbh->errstr; } else { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +0'"); $sth->execute or die $dbh->errstr; } while ( @slog = $sth->fetchrow_array ) { $sth2 = $dbh->prepare("SELECT id,name FROM category WHERE parent = + '$slog[0]'"); $sth2->execute or die $dbh->errstr; $rv = $sth2->rows; $subs = $rv; $sth3 = $dbh->prepare("SELECT category FROM items WHERE category = + '$slog[0]'"); $sth3->execute or die $dbh->errstr; $rv = $sth3->rows; $subs2 = $rv; print qq~ <tr> <td bgcolor="#FFFFFF" class="darktext" width="200"> <a href="admin.cgi?go=cart&id=$slog[0]">$slog[1]</a> </td> <td bgcolor="#FFFFFF" class="darktext" width="50"> $subs </td> <td bgcolor="#FFFFFF" class="darktext" width="50"> $subs2 </td> <td align="center" bgcolor="#FFFFFF" class="darktext" width="7 +0"> ~; if($id){ print qq~ <a href="admin.cgi?cart=settings&id=$slog[0]&otherid +=$id">settings</a> ~; } else { print qq~ <a href="admin.cgi?cart=settings&id=$slog[0]">settin +gs</a> ~; } print qq~ </td> </tr> ~; } print qq~ </table> </td> </tr> </table><br> ~;
Now if there are no results for
if($id) { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +$id'"); $sth->execute or die $dbh->errstr; } else { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +0'"); $sth->execute or die $dbh->errstr; }
Then I dont want it to print
print qq~ <p><b>Shopping Cart Manager</b></p> <table align="center" width="370" cellpadding="0" cellspacing="0"> <td colspan="4"> <table align="center" width="370" cellpadding="4" bgcolor="black +" cellspacing="1"> <tr> <td class="text" bgcolor="#3399FF" width="200" align="center"> <b>Category</b> </td> <td class="text" bgcolor="#3399FF" width="50" align="center"> <b>Sub</b> </td> <td class="text" bgcolor="#3399FF" width="50" align="center"> <b>Item</b> </td> <td class="text" bgcolor="#3399FF" width="70">&nbsp;</td> </tr> ~;

Replies are listed 'Best First'.
Re: If no results come back ...
by ehdonhon (Curate) on Jul 21, 2002 at 02:29 UTC

    Hello Andrew,

    Looks like you might be new to the monastary. I'd like to suggest that in the future, you are more likely to get answers to your questions if you can come up with a minimal case that describes what you are trying to do rather than just pasting all of your code into a window. Monks are lazy busy people, and really don't like to have to read through a lot of extraneous fluff in order to sift through and find the actual question. In general, the shorter and more consise your question, the more likely you are to get a helpful answer.

    Now, on to your question, if I understand your question, you want to avoid the first print statement if $id doesn't exist? If that is the case, there are a ton of ways to do that. First, you could just make your if block bigger:

    if ( $id ) { print qq~ ... }

    Or you could get sneaky and use perl's short-circut evaluations..

    $id and print qq~ ...

    Or you could add another 'if' statement:

    print qq~ html stuff here... ~ if ( $id );
      no if there are no results from the SQL code.
      $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +$id'"); $sth->execute or die $dbh->errstr;

        Ahh.. you have stumbled upon the other reason for being concise, so people know what you are asking for. :)

        If you want to know if there are any results, check the return value from $sth->execute. But you have to be careful since 0 results returns "0E0" (zero, but true). That trick is what prevents the 'or die' part of your command from running if no results return. Basically, you just need to know to evaluate the return value from the execute command in a numeric context. Also, if you need to know the result of that query to decide if you should print something, then obviously you need to run the query before you try to print it..

        my $rows; if($id) { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +$id'"); $sth->execute or die $dbh->errstr; } else { $sth = $dbh->prepare("SELECT id,name FROM category WHERE parent = ' +0'"); $rows = $sth->execute or die $dbh->errstr; } if ( $rows + 0 ) { print qq~ ... ~; }
Re: If no results come back ...
by screamingeagle (Curate) on Jul 21, 2002 at 03:21 UTC
    you could also do :
    if($id) { $sql = "SELECT count(*) FROM category WHERE parent = '$id'"; } else { $sql = "SELECT count(*) FROM category WHERE parent = '0'"; } $sth = $dbh->prepare($sql) || die "Error: $!"; $sth->execute or die $dbh->errstr; my ($count) = $sth->fetchrow_array; if ($count > 0) { print qq... <Then fire the sQL which gets the data>
      It has to be in a while or for statement cause it gets more than 1 row
        so what is the problem? ( why don't you just put it in a while or for loop or do you need further instruction?)