lwaters5 Posted January 15, 2013 Share Posted January 15, 2013 Using this code: <form id="form1" name="form1" method="post" action="test4.php"> <?php do { ?> <p> <input name="UID[]" type="checkbox" id="UID" value="<?php echo $row_applicants['UID']; ?>" /> <label for="UID"></label> <?php echo $row_applicants['FNAME']; ?> <?php echo $row_applicants['LNAME']; ?> </p> <?php } while ($row_applicants = mysql_fetch_assoc($applicants)); ?> <p> <input type="submit" name="button" id="button" value="Submit" /> </p> </form> I pass the checkbox field to another page that displays the profiles for each UID. I am getting the content boxes to repeat but, the information in the boxes are not repeating. Any advice? Here is the results page: mysql_select_db($database_connect, $connect); foreach ($_POST['UID'] as $value) { $SQLquery = $query_your_scholarships = "SELECT * FROM users WHERE UID = $value"; $your_scholarships = mysql_query($query_your_scholarships, $connect) or die(mysql_error()); $varlist = mysql_fetch_array ($your_scholarships); echo $varlist['UID'] . ": " . $varlist['FNAME'] . " " . $varlist['LNAME'] . "<br />"; } date_default_timezone_set('America/New_York'); ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title><br /> <style> .break { page-break-before: always; } </style> </head> <body><?php foreach ($_POST['UID'] as $value) do { ?> <p class="break"><strong><?php echo $varlist['FNAME']; ?> <?php echo $varlist['LNAME']; ?></strong></p> <fieldset id="el11"><legend><strong>Section 1: Applicant Information</strong></legend> <table width="100%" border="0" cellspacing="1" cellpadding="6"> <tr bgcolor="#c2bdaf"> <td width="26%" valign="top"><strong>Name: </strong><?php echo $varlist['FNAME']; ?> <?php echo $varlist['LNAME']; ?></strong></font></strong></td> <td valign="top"><strong>Gender: </strong><?php echo $varlist['GENDER']; ?></strong></font></strong></td> <td valign="top"><strong>SID: </strong><?php echo $varlist['SID']; ?></strong></font></strong></td> </tr> <tr> <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Address: </strong><?php echo $varlist['ADDRESS']; ?></strong></font></strong></td> <td valign="top" bgcolor="#FFFFFF"><strong>Homeroom: </strong><?php echo $varlist['HR']; ?></strong></font></strong></td> </tr> <tr bgcolor="#c2bdaf"> <td valign="top"><strong>Telephone: </strong><?php echo $varlist['TEL']; ?></strong></font></strong></td> <td valign="top"><strong>LC: </strong><?php echo $varlist['LC']; ?></strong></font></strong></td> <td valign="top"><strong>GC: </strong><?php echo $varlist['GC']; ?></strong></font></strong></td> </tr> <tr> <td valign="top" bgcolor="#FFFFFF"><strong>Email: </strong><?php echo $varlist['UNAME']; ?></strong></font></strong></td> <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Elementary School:</strong> <?php echo $varlist['ELEM']; ?></td> </tr> </table> </fieldset> <?php } while ($varlist = mysql_fetch_array($your_scholarships)); ?> </body> </html> <?php mysql_free_result($your_scholarships); ?> Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/ Share on other sites More sharing options...
Christian F. Posted January 16, 2013 Share Posted January 16, 2013 Please use the [code][/code] tags around your code, as it helps make both your post and your code a lot easier to read. Also, from what I was able to read of your code, you're running queries inside loops. Don't do this, it is completely unnecessary and wastes time like nothing else. Instead you should be using JOINs, to retrieve all the data in one query. Then you loop through the results, doing whatever you want to do with them. Anyway, repost your code with the code tags, and I'm sure someone will help you out (if not me). Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/#findComment-1406040 Share on other sites More sharing options...
lwaters5 Posted January 16, 2013 Author Share Posted January 16, 2013 Sorry about that, here is my code for the results page. I have it repeating each table now but, it is not repeating the data within the table. mysql_select_db($database_connect, $connect); foreach ($_POST['UID'] as $value) { $SQLquery = $query_your_scholarships = "SELECT * FROM users WHERE UID = $value"; $your_scholarships = mysql_query($query_your_scholarships, $connect) or die(mysql_error()); $varlist = mysql_fetch_array ($your_scholarships); echo $varlist['UID'] . ": " . $varlist['FNAME'] . " " . $varlist['LNAME'] . "<br />"; } date_default_timezone_set('America/New_York'); ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title><br /> <style> .break { page-break-before: always; } </style> </head> <body><?php foreach ($_POST['UID'] as $value) do { ?> <p class="break"><strong><?php echo $varlist['FNAME']; ?> <?php echo $varlist['LNAME']; ?></strong></p> <fieldset id="el11"><legend><strong>Section 1: Applicant Information</strong></legend> <table width="100%" border="0" cellspacing="1" cellpadding="6"> <tr bgcolor="#c2bdaf"> <td width="26%" valign="top"><strong>Name: </strong><?php echo $varlist['FNAME']; ?> <?php echo $varlist['LNAME']; ?></strong></font></strong></td> <td valign="top"><strong>Gender: </strong><?php echo $varlist['GENDER']; ?></strong></font></strong></td> <td valign="top"><strong>SID: </strong><?php echo $varlist['SID']; ?></strong></font></strong></td> </tr> <tr> <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Address: </strong><?php echo $varlist['ADDRESS']; ?></strong></font></strong></td> <td valign="top" bgcolor="#FFFFFF"><strong>Homeroom: </strong><?php echo $varlist['HR']; ?></strong></font></strong></td> </tr> <tr bgcolor="#c2bdaf"> <td valign="top"><strong>Telephone: </strong><?php echo $varlist['TEL']; ?></strong></font></strong></td> <td valign="top"><strong>LC: </strong><?php echo $varlist['LC']; ?></strong></font></strong></td> <td valign="top"><strong>GC: </strong><?php echo $varlist['GC']; ?></strong></font></strong></td> </tr> <tr> <td valign="top" bgcolor="#FFFFFF"><strong>Email: </strong><?php echo $varlist['UNAME']; ?></strong></font></strong></td> <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Elementary School:</strong> <?php echo $varlist['ELEM']; ?></td> </tr> </table> </fieldset> <?php } while ($varlist = mysql_fetch_array($your_scholarships)); ?> </body> </html> <?php mysql_free_result($your_scholarships); ?> Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/#findComment-1406104 Share on other sites More sharing options...
DavidAM Posted January 16, 2013 Share Posted January 16, 2013 mysql_select_db($database_connect, $connect); foreach ($_POST['UID'] as $value) { $SQLquery = $query_your_scholarships = "SELECT * FROM users WHERE UID = $value"; $your_scholarships = mysql_query($query_your_scholarships, $connect) or die(mysql_error()); $varlist = mysql_fetch_array ($your_scholarships); echo $varlist['UID'] . ": " . $varlist['FNAME'] . " " . $varlist['LNAME'] . "<br />"; } You are overwriting the $varlist each time through that loop, so you are not actually collecting the information on any but the last ID. You are also still running that query in a loop. A better approach would be: // ASSUMING UID IS A NUMBER NOT A STRING $idList = implode(',', $_POST['UID']); $sql = 'SELECT * FROM users WHERE UID in (' . $idList . ')'; $res = mysql_query($sql); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row; } Now you have all of the data in an array $varlist. The key to that array is the UID of the record, and the value of each element is an array containing the database data. By the way, you should not use SELECT *, you should specifically list the fields you actually need. In the body of the document, you are trying to retrieve data from the last query, again. You should just be using the data you already retrieved: foreach ($varlist as $UID => $row) { ... } This all assumes that there will be only one record per UID. If there could be more than one, you need to add another level to the $varlist array, and add another foreach inside the one in the body. Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/#findComment-1406110 Share on other sites More sharing options...
lwaters5 Posted January 16, 2013 Author Share Posted January 16, 2013 Thanks so much for your quick response and help. As you can see I am a newbie to PHP . So I replaced the code with your code and I am still getting some errors. Here is the new code: <?php date_default_timezone_set('America/New_York'); ?> <?php require_once('../../Connections/connect.php'); ?> <?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_connect, $connect); $idList = implode(',', $_POST['UID']); $sql = 'SELECT * FROM users WHERE UID in (' . $idList . ')'; $res = mysql_query($sql); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row; } date_default_timezone_set('America/New_York'); ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title><br /> <style> .break { page-break-before: always; } </style> </head> <body><?php foreach ($varlist as $UID => $row) do { ?> <p class="break"><strong><?php echo $varlist['FNAME']; ?> <?php echo $varlist['LNAME']; ?></strong></p> <fieldset id="el11"><legend><strong>Section 1: Applicant Information</strong></legend> <table width="100%" border="0" cellspacing="1" cellpadding="6"> <tr bgcolor="#c2bdaf"> <td width="26%" valign="top"><strong>Name: </strong><?php echo $varlist['FNAME']; ?> <?php echo $varlist['LNAME']; ?></strong></font></strong></td> <td valign="top"><strong>Gender: </strong><?php echo $varlist['GENDER']; ?></strong></font></strong></td> <td valign="top"><strong>SID: </strong><?php echo $varlist['SID']; ?></strong></font></strong></td> </tr> <tr> <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Address: </strong><?php echo $varlist['ADDRESS']; ?></strong></font></strong></td> <td valign="top" bgcolor="#FFFFFF"><strong>Homeroom: </strong><?php echo $varlist['HR']; ?></strong></font></strong></td> </tr> <tr bgcolor="#c2bdaf"> <td valign="top"><strong>Telephone: </strong><?php echo $varlist['TEL']; ?></strong></font></strong></td> <td valign="top"><strong>LC: </strong><?php echo $varlist['LC']; ?></strong></font></strong></td> <td valign="top"><strong>GC: </strong><?php echo $varlist['GC']; ?></strong></font></strong></td> </tr> <tr> <td valign="top" bgcolor="#FFFFFF"><strong>Email: </strong><?php echo $varlist['UNAME']; ?></strong></font></strong></td> <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Elementary School:</strong> <?php echo $varlist['ELEM']; ?></td> </tr> </table> </fieldset> <?php } while ($varlist = mysql_fetch_array($your_scholarships)); ?> </body> </html> <?php mysql_free_result($your_scholarships); ?> I think my error is in the line: <?php } while ($varlist = mysql_fetch_array($your_scholarships)); ?> at the bottom of the code and where I tell it to show the data using this type of code: <?php echo $varlist['HR']; ?> Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/#findComment-1406113 Share on other sites More sharing options...
lwaters5 Posted January 16, 2013 Author Share Posted January 16, 2013 Ok, I think I am getting real close now. Here is my code: <?php date_default_timezone_set('America/New_York'); ?> <?php require_once('../../Connections/connect.php'); ?> <?php if (!isset($_SESSION)) { session_start(); } $MM_authorizedUsers = "0,2,3,4,5,6,7"; $MM_donotCheckaccess = "false"; // *** 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 == "") && false) { $isValid = true; } } return $isValid; } $MM_restrictGoTo = "error2.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($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) $MM_referrer .= "?" . $_SERVER['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; } } mysql_select_db($database_connect, $connect); $idList = implode(',', $_POST['UID']); $sql = 'SELECT *, scholarships.NAME as scholarshipname FROM scholarships, gc_codes, users INNER JOIN stu_app2008 ON users.UID = stu_app2008.UID INNER JOIN activities ON users.UID = activities.UID INNER JOIN coll_exp ON users.UID = coll_exp.UID INNER JOIN employ ON users.UID = employ.UID INNER JOIN fin_app ON users.UID = fin_app.UID INNER JOIN finaid ON users.UID = finaid.UID INNER JOIN stu_special ON users.UID = stu_special.UID WHERE gc_codes.GCID = users.GC and scholarships.ID = stu_special.ID and UID in (' . $idList . ')'; $res = mysql_query($sql); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row; } ?> This is the error message that I get: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Scholarship\admin\printable_version2_test3.php on line 87 Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/#findComment-1406149 Share on other sites More sharing options...
DavidAM Posted January 16, 2013 Share Posted January 16, 2013 That indicates that the query failed. You need to take a look at the query (echo $sql;) and see what is wrong with it. You can also add if (! $res) echo mysql_error(); to get the error message from the database server. Quote Link to comment https://forums.phpfreaks.com/topic/273202-recordset-repeat-issue/#findComment-1406155 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.