Jump to content

Help needed with If Else statement


lukedubauskas

Recommended Posts

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);

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.