btr4 Posted June 16, 2010 Share Posted June 16, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/ Share on other sites More sharing options...
Psycho Posted June 16, 2010 Share Posted June 16, 2010 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/#findComment-1073127 Share on other sites More sharing options...
btr4 Posted June 16, 2010 Author Share Posted June 16, 2010 I changed the code, yet it gives me the same thing: a blank page with no staff members, even if they are in the database. Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/#findComment-1073164 Share on other sites More sharing options...
btr4 Posted June 16, 2010 Author Share Posted June 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/#findComment-1073180 Share on other sites More sharing options...
btr4 Posted June 17, 2010 Author Share Posted June 17, 2010 Hmmm, if I change $colname_rsStaff = "-1"; to have a value of 1, it WILL pull through the first entry in the database. Is there a way to have it start from 1, and cycle to the last entry? Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/#findComment-1073188 Share on other sites More sharing options...
btr4 Posted June 17, 2010 Author Share Posted June 17, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/#findComment-1073194 Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 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"; Quote Link to comment https://forums.phpfreaks.com/topic/204971-showing-table-with-phpmysql/#findComment-1073245 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.