lukedubauskas Posted April 6, 2010 Share Posted April 6, 2010 Hi I am new to PHP & MySQL and am having troubles with something which I am sure is so simple. The database table has users date of birth, with the dob's on the current date being featured on the site. If there are no birthdays on the current date I would like a default message to be displayed - No Ambassador birthdays today. Below is the code, if someone could please help me I'd really appreciate it. Thanks. <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_ambassador_db, $ambassador_db); $query_rs_tbl_birthdays = "SELECT real_name FROM tbl_ambassadors WHERE dob = CURDATE() ORDER BY real_name ASC"; $rs_tbl_birthdays = mysql_query($query_rs_tbl_birthdays, $ambassador_db) or die(mysql_error()); $row_rs_tbl_birthdays = mysql_fetch_assoc($rs_tbl_birthdays); $totalRows_rs_tbl_birthdays = mysql_num_rows($rs_tbl_birthdays); session_cache_limiter('none'); session_start(); ob_start(); ?> <div class="text_side_panels" id="holder_birthdays"> <?php do { ?> <?php $colname_rs_tbl_birthdays = "dob"; if ($colname_rs_tbl_birthdays == "dob"){ echo $row_rs_tbl_birthdays['real_name']; } else { print("No Ambassador birthdays today"); } ?> <br /> <?php } while ($row_rs_tbl_birthdays = mysql_fetch_assoc($rs_tbl_birthdays)); ?> <br /> </div> <?php mysql_free_result($rs_tbl_birthdays); ?> Quote Link to comment https://forums.phpfreaks.com/topic/197698-help-needed-with-if-else-statement/ Share on other sites More sharing options...
teamatomic Posted April 6, 2010 Share Posted April 6, 2010 You define a function inside a conditional that checks for its existence, that wont work. Just call the function, you wrote it, you know it exists. If its the existence of data you are trying to verify then define the function, verify the data and call the function inside the verifying conditional. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197698-help-needed-with-if-else-statement/#findComment-1037530 Share on other sites More sharing options...
lukedubauskas Posted April 6, 2010 Author Share Posted April 6, 2010 Thanks for your reply. I think you just confused me even more. Can you please show me, I am really new to this but I want to get my head around it. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/197698-help-needed-with-if-else-statement/#findComment-1037556 Share on other sites More sharing options...
teamatomic Posted April 6, 2010 Share Posted April 6, 2010 Get rid of this: if (!function_exists("GetSQLValueString")) { and just define your function then use it. function GetSQLValueString($theValue, $theType, $theDefinedValue, $theNotDefinedValue) { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""); mysql_select_db($database_ambassador_db, $ambassador_db); $query_rs_tbl_birthdays = "SELECT real_name FROM tbl_ambassadors WHERE dob = CURDATE() ORDER BY real_name ASC"; $rs_tbl_birthdays = mysql_query($query_rs_tbl_birthdays, $ambassador_db) or die(mysql_error()); $row_rs_tbl_birthdays = mysql_fetch_assoc($rs_tbl_birthdays); $totalRows_rs_tbl_birthdays = mysql_num_rows($rs_tbl_birthdays); session_cache_limiter('none'); session_start(); ob_start(); ?> <div class="text_side_panels" id="holder_birthdays"> <?php do { ?> <?php $colname_rs_tbl_birthdays = "dob"; if ($colname_rs_tbl_birthdays == "dob"){ echo $row_rs_tbl_birthdays['real_name']; } else { print("No Ambassador birthdays today"); } ?> <br /> <?php } while ($row_rs_tbl_birthdays = mysql_fetch_assoc($rs_tbl_birthdays)); ?> <br /> </div> <?php mysql_free_result($rs_tbl_birthdays); ?> Something like that. But what you are doing? Where does $theValue and $theType come from? As your code stands from what you show those values dont exist. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197698-help-needed-with-if-else-statement/#findComment-1037587 Share on other sites More sharing options...
oni-kun Posted April 6, 2010 Share Posted April 6, 2010 I believe you need lessons on syntactically correct coding styles, PHP is based on C. A lot of your code is redundant. Quote Link to comment https://forums.phpfreaks.com/topic/197698-help-needed-with-if-else-statement/#findComment-1037588 Share on other sites More sharing options...
lukedubauskas Posted April 6, 2010 Author Share Posted April 6, 2010 Thanks guys. Got it working. Dreamweaver put all that redundant code in. Like I said, I'm only new to this. Here is what I ended up with and it works great. <?php mysql_select_db($database_ambassador_db, $ambassador_db); $query_rs_tbl_birthdays = "SELECT real_name FROM tbl_ambassadors WHERE dob = CURDATE() ORDER BY real_name ASC"; $rs_tbl_birthdays = mysql_query($query_rs_tbl_birthdays, $ambassador_db) or die(mysql_error()); $row_rs_tbl_birthdays = mysql_fetch_assoc($rs_tbl_birthdays); $totalRows_rs_tbl_birthdays = mysql_num_rows($rs_tbl_birthdays); ?> <div class="text_side_panels" id="holder_birthdays"> <?php do { ?> <?php echo $row_rs_tbl_birthdays['real_name']; ?> <?php if ($totalRows_rs_tbl_birthdays == 0) { // Show if recordset empty ?> <?php echo "No Ambassador birthdays today" ?> <?php } // Show if recordset empty ?> <br /> <?php } while ($row_rs_tbl_birthdays = mysql_fetch_assoc($rs_tbl_birthdays)); ?> <br /> </div> <?php mysql_free_result($rs_tbl_birthdays); ?> Quote Link to comment https://forums.phpfreaks.com/topic/197698-help-needed-with-if-else-statement/#findComment-1037617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.