kinda urgent... conditional html displaying

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shadow8807
Forum Newbie
Posts: 9
Joined: Mon Feb 12, 2007 4:00 pm

kinda urgent... conditional html displaying

Post by shadow8807 »

Everah | Please use

Code: Select all

,

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]


Basically my code checks for the number of rows for a value. If the value is within what it needs to be it displays a checkbox, if it doesnt then it will not display anything.

Code: Select all

$dbh=mysql_connect ('localhost','user','pass') or die ("Unable to connect to MySQL server: <br>" . mysql_error());
mysql_select_db ('databasename', $dbh) or die ("Unable to select database: <br>" . mysql_error());
$query = "SELECT * FROM database WHERE code='ES-C1-07'";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
if ($numrows > "30") { // if program is full
}
else
{
<input type="checkbox" name="ES-C1-07" value="ES-C1-07">;
}
Why doesn't something like this work? Any help is appreciated!!!


Everah | Please use

Code: Select all

,

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]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Note: As a general rule, putting 'urgent' and other discriptive terms that might suggest you need immediate help in your title is a sure fire way to get your post overlooked. Everyone's posts are urgent, and the volunteers that offer assistance in these forums do the best they can with that they have, regardless of the urgency of your particular request. This is not point you out directly, but being new, I thought I'd at least prepare you for our community. That being said...

Here's the logic you are giving the parser (with a little code cleanup):

Code: Select all

<?php
// get the number of rows from the query
$numrows = mysql_num_rows($result);

// If the number of rows in the table is more than 30, do absolutely nothing
if ($numrows > 30) { 
  // if program is full
}
else
{
  // Since there are less than 30 rows, echo out a form field.
  echo '<input type="checkbox" name="ES-C1-07" value="ES-C1-07">';
}
?>
That second half could be shortened to:

Code: Select all

<?php
if ($numrows <= 30) { 
  // Since there are less than 30 rows, echo out a form field.
  echo '<input type="checkbox" name="ES-C1-07" value="ES-C1-07">';
}
?>
the DtTvB
Forum Newbie
Posts: 11
Joined: Sun Feb 11, 2007 6:10 am

Post by the DtTvB »

Code: Select all

// ............
else
{
// You can close the PHP block here. Everything will be outputted as-is.
?>
<input type="checkbox" name="ES-C1-07" value="ES-C1-07">;
<?php
// Start parsing PHP again..
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yeah, but that would be spaghetti... :wink:
shadow8807
Forum Newbie
Posts: 9
Joined: Mon Feb 12, 2007 4:00 pm

Post by shadow8807 »

Thanks to everyone for their input. Everything is running great. This code returns no errors and is working as it should.

Here is the final PHP code:

Code: Select all

<?
$dbh=mysql_pconnect ('localhost','username','password') or die ("Unable to connect to MySQL server: <br>" . mysql_error());
mysql_select_db ('database', $dbh) or die ("Unable to select database: <br>" . mysql_error());
$query = "SELECT * FROM TRANSACTION_PROGRAM_CODES WHERE PROGRAM_CODE='ES-C1-07'";
         $result = mysql_query($query);
         $numrows = mysql_num_rows($result);
         if ($numrows > 30) { // if program is full
         }
         else
         { 
         echo '<input type="checkbox" name="ES-C1-07" value="ES-C1-07">';
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I still think you should ditch the idea of placing your actions in an else after an if that does nothing. It just seems cleaner code to me.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

I'm with everah...bad coding practice there buddy. it works..but..seems backwards....
Post Reply