Jump to content

Showing table with php/mysql


btr4

Recommended Posts

Ok, so here's a code I've been working on using Dreamweaver functions to insert/delete/update a database that contains staff information. This is one of the last hurdles I need to jump over to get the concept working the way it needs to.

 

<?php if ($totalRows_rsStaff > 0) { // Show if recordset not empty ?>
<table border="0" cellpadding="0" cellspacing="0" id="tblrepeat">
    <tr>
      <th>Staff Name</th>
      <th>Remove</th>
      <th>Edit</th>
    </tr>
    <?php do { ?>
      <tr>
        <td><?php echo $row_rsStaff['staffname']; ?></td>
        <td><a href="staff_delete.php?id=<?php echo $row_rsStaff['staffid']; ?>">Remove</a></td>
        <td><a href="staff_edit.php?id=<?php echo $row_rsStaff['staffid']; ?>">Edit</a></td>
      </tr>
      <?php } while ($row_rsStaff = mysql_fetch_assoc($rsStaff)); ?>
  </table>
<br />
<?php } // Show if recordset not empty ?>

 

Now, what happens is, when I go to the staff control page, just the link I have for adding a staff member shows up, even though there is one staff entry in the database. Can I just make do without the "show if recordset is not empty" coding and get along fine? Or are there any other ways to code this that doesn't just involve deleting the function?

 

Thanks!

 

Link to comment
Share on other sites

Let me rephrase your question as it really doesn't make sense.

 

You have one record int he database, but it is not displaying and you are wanting to know if you can remove the condition

<?php if ($totalRows_rsStaff > 0) { // Show if recordset not empty ?>

 

Yes you can, but it won't fix your problem. $totalRows_rsStaff is apparently not being set correctly. I'm not sure if you need that value or not. If so, you need to fix THAT problem.

 

You also have another problem with your usage of a do..while loop. The do...while loop will execute at least one time, then it checks the condition in the while() statement to see if it should run again. The way you have it, the variables for staff name and staff id are not set the first time the block is executed so you would always have a blank row at the top. Here is a better implementation IMHO:

 

<?php if (mysql_num_rows($rsStaff)>0) { // Show if recordset not empty ?>
<table border="0" cellpadding="0" cellspacing="0" id="tblrepeat">
    <tr>
      <th>Staff Name</th>
      <th>Remove</th>
      <th>Edit</th>
    </tr>
    <?php while ($row_rsStaff = mysql_fetch_assoc($rsStaff)) { ?>
      <tr>
        <td><?php echo $row_rsStaff['staffname']; ?></td>
        <td><a href="staff_delete.php?id=<?php echo $row_rsStaff['staffid']; ?>">Remove</a></td>
        <td><a href="staff_edit.php?id=<?php echo $row_rsStaff['staffid']; ?>">Edit</a></td>
      </tr>
      <?php } ?>
  </table>
<br />
<?php } // Show if recordset not empty ?>

Link to comment
Share on other sites

Update, this is the code Dreamweaver automatically put in:

 

<?php require_once('../Connections/hcc.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?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;
}
}

$colname_rsStaff = "-1";
if (isset($_GET['id'])) {
  $colname_rsStaff = $_GET['id'];
}
mysql_select_db($database_hcc, $hcc);
$query_rsStaff = sprintf("SELECT * FROM cms_staff WHERE staffid = %s", ($colname_rsStaff, "int"));
$rsStaff = mysql_query($query_rsStaff, $hcc) or die(mysql_error());
$row_rsStaff = mysql_fetch_assoc($rsStaff);
$totalRows_rsStaff = mysql_num_rows($rsStaff);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/admin_temp.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Link Management System</title>
<!-- InstanceEndEditable -->
<link href="../style/admin_style.css" rel="stylesheet" type="text/css" />
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>
<div id="wrapper">
<div id="header">
  <p>Control Panel</p>
  <p> </p>
</div>
<div id="navigation">
<ul id="mainav">
<li></li>
<li></li>
<li><a href="logout.php">logout</a></li>
<li id="front"><a href="../index.php" target="_blank">front</a></li>
</ul>
</div>
<div id="container"><!-- InstanceBeginEditable name="edit_content" -->
  <p id="ptitle">Manage Staff</p>
  <p> </p>
  <p></p>
  <?php if ($totalRows_rsStaff > 0) { // Show if recordset not empty ?>
    <table border="0" cellpadding="0" cellspacing="0" id="tblrepeat">
      <tr>
        <th>Staff Name</th>
        <th>Remove</th>
        <th>Edit</th>
      </tr>
      <?php do { ?>
        <tr>
          <td><?php echo $row_rsStaff['staffname']; ?></td>
          <td><a href="staff_delete.php?id=<?php echo $row_rsStaff['staffid']; ?>">Remove</a></td>
          <td><a href="staff_edit.php?id=<?php echo $row_rsStaff['staffid']; ?>">Edit</a></td>
        </tr>
        <?php } while ($row_rsStaff = mysql_fetch_assoc($rsStaff)); ?>
    </table>
    <?php } // Show if recordset not empty ?>
  <?php if ($totalRows_rsStaff == 0) { // Show if recordset empty ?>
  <p>Sorry, there are no records matching your searching criteria.</p>
  <?php } // Show if recordset empty ?>
<a href="staff_add.php">Add Staff</a>
<!-- InstanceEndEditable --></div>
<div id="footer"><p>© Web Designer Justin Bell</p></div>
</div>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($rsStaff);
?>

 

Sorry for the length, I just can't seem to isolate what would be causing this.

Link to comment
Share on other sites

Sorry for the quadruple post, but I believe I have solved my own issue. I changed the line:

$query_rsStaff = sprintf("SELECT * FROM cms_staff WHERE staffid = %s", ($colname_rsStaff, "int"));

 

to:

 

$query_rsStaff = sprintf("SELECT * FROM cms_staff WHERE staffid > %s", ($colname_rsStaff, "int"));

 

So, it turns out, the variables and queries were working properly, just that one line that said in order for an entry to appear, it HAD to have whatever value $colname_rsStaff had.

 

Woot! once again, sorry for the quadruple post, and thank you mjdamato for your help!

Link to comment
Share on other sites

So, it turns out, the variables and queries were working properly, just that one line that said in order for an entry to appear, it HAD to have whatever value $colname_rsStaff had.

 

Not quite. The query was looking for a specific staff ID. If you are wanting to show all the staff records, then you should remove the WHERE clause from your query entirely:

 

$query_rsStaff = "SELECT * FROM cms_staff";

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.