Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making list from MySQL query 1

Status
Not open for further replies.

expert01

Technical User
Jul 19, 2002
26
US
I'm having a problem with this script. It works fine for the first category, but not the second.

Output:
Script
Code:
<?PHP
$connect = mysql_connect('test.perfora.net', 'test118443323', 'test');
if (!$connect) {
   die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('db118443323', $connect);
if (!$db_selected) {
   die ('Can\'t use foo : ' . mysql_error());
}

$sql1 = mysql_query("SELECT * FROM checklist ORDER BY CATEGORY, NAME") or die("Can't get checklist");
$sql2 = mysql_query("SELECT * FROM checklist_categories ORDER BY NAME") or die("Can't Get Categories");
?>
<html>
<head>
<title>Move Out List</title>
</head>
<body>
<?PHP
$i = 1;
while($categories = mysql_fetch_assoc($sql2))
{
print("<h2>" . $categories['NAME'] . "</h1>\n");

while($checklist = mysql_fetch_assoc($sql1))
{
if($checklist['CATEGORY'] == $i)
{
print("<h4>" . $checklist['NAME'] . "</h3>\n");
}
}
$i++;
}
?>
</body>
</html>
<?PHP
mysql_close($connect);
?>

DB:
Code:
-- 
-- Table structure for table `checklist`
-- 

CREATE TABLE `checklist` (
  `ID` tinyint(3) NOT NULL auto_increment,
  `NAME` varchar(40) NOT NULL default '(none)',
  `CATEGORY` tinyint(3) NOT NULL default '0',
  `DETAILS` varchar(255) NOT NULL default '(none)',
  `USER` enum('(none)','User1','User2') NOT NULL default '(none)',
  `CHECKED` enum('Yes','No') NOT NULL default 'No',
  KEY `ID` (`ID`),
  FULLTEXT KEY `DETAILS` (`DETAILS`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;

-- 
-- Dumping data for table `checklist`
-- 

INSERT INTO `checklist` VALUES (1, 'Boxframe/Mattress/Futon', 1, 'Need at least a mattress or futon, maybe a frame', '(none)', 'Yes');
INSERT INTO `checklist` VALUES (2, 'Floor Lamp', 1, '(none)', '(none)', 'No');
INSERT INTO `checklist` VALUES (3, 'Couch', 2, 'Hideaway and end piece, will need to reupholster', 'User1', 'Yes');

-- 
-- Table structure for table `checklist_categories`
-- 

CREATE TABLE `checklist_categories` (
  `ID` tinyint(3) NOT NULL auto_increment,
  `NAME` varchar(255) NOT NULL default '(none)',
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

-- 
-- Dumping data for table `checklist_categories`
-- 

INSERT INTO `checklist_categories` VALUES (1, 'Bedroom');
INSERT INTO `checklist_categories` VALUES (2, 'Living Room');
 
(quote from first post)

It works fine for the first category, but not the second.

The second category should contain "Couch".
 
it appears that the $i++ is outside the first while loop.
 
try the following:

Code:
$i = 1;
while($categories = mysql_fetch_assoc($sql2)):
	print("<h2>" . $categories['NAME'] . "</h1>\n");

	while($checklist = mysql_fetch_assoc($sql1)):
		if($checklist['CATEGORY'] == $i):
			print("<h4>" . $checklist['NAME'] . "</h3>\n");
		endif;
	endwhile;
$i++;
endwhile;
 
no, same output... it looks exactly the same as what was there, just different format.
 
i'm sorry. i'm talking rubbish.

the problem is that you are not resetting the internal pointer on the second while clause. if you really want to use two intersecting loops then it will be better to use a for loop for the internal.

apologies again - too early in the morning.
 
try
Code:
while ($categories = mysql_fetch_assoc($sql2) )
{
	print("<h2>" . $categories['NAME'] . "</h1>\n");			
	mysql_data_seek($sql1,0);
	while($checklist = mysql_fetch_assoc($sql1))
	{
			if ($checklist['CATEGORY'] == $categories['ID']):
				echo "<h4>".$checklist['NAME']."</h4><br/>";
			endif;			
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top