Page 1 of 1

[SOLVED] Wierd looping problem I'm having

Posted: Sat Sep 11, 2004 1:54 pm
by JCMNetmedia
feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Basically, I have a problem I've been working on for a while that seems really simple, but I am not able to get it going. Basically I have a table in mysql with a set of tools, then comments on those tools. Ex:

Call Monitor 
   -> Comment about Call Monitor
   -> Another Comment about Call monitor

Stats Tool 
   -> Comment about stats tool
   -> Another Comment stats tool


Basically, I have 25 tools which are in their own table, which is joined to the comments table. I want to create a loop that counts the amount of tools (right now its 25, but it will change) and loops that amount of times, and displays the name of those tools, something like this:

Code: Select all

$sql = "SELECT * FROM types ORDER BY tdesc";

$result = $dbi->ex($sql);

mysql_fetch_array($result);

$count = mysql_numrows($result);

// then loop based on the number of rows

// and loop for each comment for each tool :thumbsup:


it sounds simple but doesnt seem to work well.

Here is what I have now (which does not work)

Code: Select all

echo "<table width="700" border="1" class="fbtable">";

echo "<br><strong>$ary[name]</strong><br>";

echo "<tr>	
		<td width="10%" class="fbtabletop">Type</td>
        <td width="70%" class="fbtabletop">Comment</td>
  	    <td width="10%" class="fbtabletop">Submitter</td> 
		<td width="10%" class="fbtabletop">Date</td>
	 </tr>";

// this is the part I want to loop 

echo "<tr>
		  <td class="fbtablecell"><strong>$ary[tdesc]</strong></td>
	      <td class="fbtablecell"><strong>$ary[text]</strong></td>
	 	  <td class="fbtablecell"><strong>$ary[who]</strong></td>
	      <td class="fbtablecell"><strong>$ary[date]</strong></td>
	 </tr><br>\n";

} // end of while loop

} // end of view section
Basically this creates a new table for every comment given. I want to draw one table, and have the rows repeated, for each tool.

Any ideas?


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Sep 11, 2004 2:46 pm
by Christopher
You showed everything but your while loops. How about:

while ($row = mysql_fetch_array($result)) {
// build table HTML here
}

OR

$count = mysql_numrows($result);
for ($i=0; $i<$count; ++$i) {
$row = mysql_fetch_array($result);
// build table HTML here
}

________________________________________
http://andromeda.rutgers.edu/~jlynch/Writing/b.html

Posted: Sat Sep 11, 2004 2:55 pm
by feyd
your html is broken, badly.

Posted: Sat Sep 11, 2004 3:49 pm
by Christopher
feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

echo "<br><strong>$ary[name]</strong><br>";

echo "<table width="700" border="1" class="fbtable">";

echo "<tr>   
      <td width="10%" class="fbtabletop">Type</td>
        <td width="70%" class="fbtabletop">Comment</td>
         <td width="10%" class="fbtabletop">Submitter</td>
      <td width="10%" class="fbtabletop">Date</td>
    </tr>";

// LOOP

echo "<tr>
        <td class="fbtablecell"><strong>$ary[tdesc]</strong></td>
         <td class="fbtablecell"><strong>$ary[text]</strong></td>
         <td class="fbtablecell"><strong>$ary[who]</strong></td>
         <td class="fbtablecell"><strong>$ary[date]</strong></td>
    </tr>\n";

// END LOOP

echo "</table>\n";

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Sep 11, 2004 4:29 pm
by JCMNetmedia
yeah, my html is broken, I just kind of copied and pasted. Conceptually I would like to do this:


while there are tools in tool table
{ // first loop

display tool names as a heading (only once)

{ // second loop

display entries from comment table (joined to tool table)
until there are no more

}
}


It seems really simple, but I've used combinations of while loops, do while, for, if, and foreach and none of them has given me the results I want. I get a lot of endless loops, or the tool name is diplayed 25 times with it its respective comments :D

sorry about the PHP tags, won't happen again

Posted: Sat Sep 11, 2004 4:40 pm
by feyd
post the table structures and how they are to "join"

Posted: Sat Sep 11, 2004 4:58 pm
by JCMNetmedia
I'm not sure what format you want these in ,but I'll give it a shot.

tables are as follows:

here is the structure:

Code: Select all

CREATE TABLE `comments` (
  `cid` int(11) NOT NULL auto_increment,
  `date` text NOT NULL,
  `type` int(11) NOT NULL default '0',
  `tool` int(11) NOT NULL default '0',
  `text` text NOT NULL,
  `ack` text NOT NULL,
  `who` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`cid`)
) TYPE=MyISAM; 

# Host: SSVS
# Database: pcifeedback
# Table: 'tools'
# 
CREATE TABLE `tools` (
  `ToolID` int(11) NOT NULL auto_increment,
  `name` text NOT NULL,
  `description` text NOT NULL,
  `active` text NOT NULL,
  PRIMARY KEY  (`ToolID`)
) TYPE=MyISAM; 

# Host: SSVS
# Database: pcifeedback
# Table: 'types'
# 
CREATE TABLE `types` (
  `TypeID` int(11) NOT NULL default '0',
  `tdesc` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`TypeID`)
) TYPE=MyISAM;
here is the main query I'll be using, that joins the three:

Code: Select all

SELECT comments.*, types.*, tools.* FROM types,comments,tools WHERE comments.type = types.TypeID 
AND tools.ToolID = comments.tool AND TypeID = '$type' AND ToolID = '$tool' ORDER by 'date' DESC
I hope that helps

Posted: Tue Sep 14, 2004 10:11 am
by JCMNetmedia
Here is what I have so far:

Code: Select all

<?php
function drawtable($sql) {

if (!$sql) {
	
	echo "<h1>Error: No SQL Query</h1>";

}
else {	
// this function takes the sql query and returns the html
// to draw up the tables with the data. 

	global $dbi;
	
	$result = $dbi->ex($sql); // dbi function

?>

	<table border="2" cellspacing="2">
	
<?PHP
	
	while ($ary = (mysql_fetch_assoc($result))) {

	if ($current != $ary["name"]) {
			$current = $ary["name"];
			
			echo "<tr><td>$ary[name]</td></tr></table> \n";
			{
			echo "\n<table border="2" cellspacing="2">";
			}

	$current == ''; // set current back to blank
	
			} // end if current loop
		
		} // end while

	echo "</table>";

	} // end else loop

} // end function drawtable

?>
This displays the headings, but I am still having problems with the comments being displayed below. any ideas would be appreciated

Posted: Tue Sep 14, 2004 10:21 am
by m3mn0n
Line 17....."dbi" is the instance of a class, "ex" is the function (proper name being method since it's a class). ;)

Solved

Posted: Tue Sep 14, 2004 4:21 pm
by JCMNetmedia
I got it figured out, and here is the code

Code: Select all

<?php
function drawtable($sql) {
 	
if (!$sql) {
	
	echo "<h1>Error: No SQL Query</h1>";

}
else {	
// this function takes the sql query and returns the html
// to draw up the tables with the data. 

	global $dbi;
	
	$result = $dbi->ex($sql);

	while ($ary = (mysql_fetch_assoc($result))) { // while there are results 

		if ($current != $ary["name"]) {
				$current = $ary["name"];
			
?>
<table border="2" cellspacing="2">
<tr><td><? echo $ary[name]; ?></td></tr></table>
<table border ="0" class="fbtablemain">
<tr><td width="10%" class="fbtabletop">Type</td>
    <td width="70%" class="fbtabletop">Comment</td>
    <td width="10%" class="fbtabletop">Submitter</td> 
	<td width="10%" class="fbtabletop">Date</td></tr>	
</table>
<table border ="0" class="fbtablemain">	
<?
$current == '';
			} // end if current loop
?>

<tr><td width="10%" class="fbtablecell"><strong><? echo $ary['tdesc']; ?></strong></td>
    <td width="70%" class="fbtablecell"><strong><? echo $ary['text']; ?></strong></td>
    <td width="10%" class="fbtablecell"><strong><? echo $ary['who']; ?></strong></td>
	<td width="10%" class="fbtablecell"><strong><? echo $ary['date']; ?></strong></td></tr>
	
<?

		} // end while
?></table><?
	} // end else loop

} // end function drawtable

?>
Hope someone can learn something from it. Thanks tho guys!