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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help figuring this while loop. Please help! 2

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
I have this code
Code:
$CommRowTable = "";
$CommQuery = 'select * FROM `snipscomments` WHERE `snipscomments`.`snipID` = ' . $_GET['Code'];
$CommData = mysql_query ($CommQuery);
$CommRow = mysql_fetch_array($CommData);

if (sizeof($CommRow) > 0) {
	$CommRowTable = '<table border="1" cellpadding="0" cellspacing="0">';
//	for ($i = 0; $i < sizeof($CommRow); $i++) {
	while ($crow = mysql_fetch_array($CommData)) {
		$aSnipComment = explode("<br />",nl2br($crow['snipComment']));
		$vSnipComment = Array2String($aSnipComment);
		$CommRowTable = $CommRowTable . '
			<tr><td bgcolor="#CCCCCC"><span style="float: left;">Posted By:&nbsp;' . $crow['snipCommBy'] . '</span>&nbsp;<span style="float: right;">On:&nbsp;' . $crow['snipCommDate'] . '</span></td></tr>
			<tr><td>' . $vSnipComment . '</td></tr>
		';
		}  // Close while loop
	$CommRowTable = $CommRowTable . '</table>';
} else {
	$CommRowTable = '<table border="0" cellapdding="0" cellspacing="0"><tr><td>No Comments Posted!</td></tr></table>';
}  // Close if sizeof()

I have looked at it for hours and cannot figure out why it does not work. If I use the for/each loop, the loop is true and it produces the table showing the same record 10 times. This is because I have one record with five fields (and I guess that phpMyAdmin adds a blank record) plus a blank record thus 10 is the number of fields in all records combined.

Just above this, I have code where I open a different mySQL table from same database. The idea is to put in a variable ($CommRowTable) a table/rows/columns formatting data extracted from mySQL table. The variable will then be printed like so
Code:
echo '
<table name="tabTable" border="0" cellpadding="0" cellspacing="0">
<tr><td>
<div class="tabber">
     <div class="tabbertab">
	  <h2>General</h2>
	  <p>
	  <table name="tabGeneral">
		  <tr>
		  	<td>Posted By</td>
			<td>' . $vAuthor . '</td>
		  </tr>
		  <tr>
		  	<td>Posted Date</td>
			<td>' . $vSince . '<td>
		  </tr>
		  <tr>
		  	<td colspan="2">&nbsp;</td>
		  </tr>
		  <tr>
		  	<td colspan="2">' . $vSubject . '</td>
		  </tr>
	  </table></p>
     </div>
     <div class="tabbertab">
	  <h2>Description</h2>
	  <p>' . $vDesc . '</p>
     </div>
     <div class="tabbertab">
	  <h2>The Code</h2>
	  <p>' . $vCode . '</p>
     </div>
     <div class="tabbertab">
	  <h2>Comments</h2>
	  <p>' . $CommRowTable . '</p>
     </div>
</div>
</tr></td>
';

You see, for each entry, there might be any number of comments attached to it (same as here when a thread is created).

This link may give you an idea of what I am working on
Code:
[URL unfurl="true"]http://www.fpgroups.com/index.php?Target=code&Action=read&Code=1[/URL]

Thank you all for your assistance!


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Change [tt]mysql_fetch_array[/tt] to [tt]mysql_fetch_assoc[/tt].

 
Sorry, I did not provide enough detail:

Code:
$CommRowTable = "";
$CommQuery = 'select * FROM `snipscomments` WHERE `snipscomments`.`snipID` = ' . $_GET['Code'];
$CommData = mysql_query ($CommQuery);
$CommRow = mysql_fetch_array($CommData);

if (sizeof($CommRow) > 0) {
    $CommRowTable = '<table border="1" cellpadding="0" cellspacing="0">';
//    for ($i = 0; $i < sizeof($CommRow); $i++) {
    while ($crow = mysql_fetch_assoc($CommData)) {
        $aSnipComment = explode("<br />",nl2br($crow['snipComment']));
        $vSnipComment = Array2String($aSnipComment);
        $CommRowTable = $CommRowTable . '
            <tr><td bgcolor="#CCCCCC"><span style="float: left;">Posted By:&nbsp;' . $crow['snipCommBy'] . '</span>&nbsp;<span style="float: right;">On:&nbsp;' . $crow['snipCommDate'] . '</span></td></tr>
            <tr><td>' . $vSnipComment . '</td></tr>
        ';
        }  // Close while loop
    $CommRowTable = $CommRowTable . '</table>';
} else {
    $CommRowTable = '<table border="0" cellapdding="0" cellspacing="0"><tr><td>No Comments Posted!</td></tr></table>';
}  // Close if sizeof()

 
Also mysql_fetch_array() will not return the quantity of rows selected.

 
Nothing, changing _array to _assoc did not work.

I use this method in several other scripts and data is retrieved just fine. I am surprise it does not here, which is why I think I'm missing something ...

I get no error nor data ... Any other suggestions?

I am starting to think of using a joint query but I do not know enough nor am sure that will be a cure.

Thanks,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Jose

try this

Code:
<?
$CommRowTable = "";
$CommQuery = 'select * FROM `snipscomments` WHERE `snipscomments`.`snipID` = ' . $_GET['Code'];
$CommData = mysql_query ($CommQuery) or die (mysql_error());

if (mysql_num_rows($CommData) > 0) {
    $CommRowTable = '<table border="1" cellpadding="0" cellspacing="0">';
    while ($crow = mysql_fetch_assoc($CommData)) {
        $aSnipComment = explode("<br />",nl2br($crow['snipComment']));
        $vSnipComment = Array2String($aSnipComment);
        $CommRowTable .= '
            <tr><td bgcolor="#CCCCCC"><span style="float: left;">Posted By:&nbsp;' . $crow['snipCommBy'] . '</span>&nbsp;<span style="float: right;">On:&nbsp;' . $crow['snipCommDate'] . '</span></td></tr>
            <tr><td>' . $vSnipComment . '</td></tr>
        ';
    }  // Close while loop
    $CommRowTable .= '</table>';
} else {
    $CommRowTable = '<table border="0" cellapdding="0" cellspacing="0"><tr><td>No Comments Posted!</td></tr></table>';
}  // Close if no records
?>

note that the mysql_fetch functions traverse a recordset. they do not return a multidimensional array of the recordset that you can apply a sizeof() function to. however there is a mysql_num_rows() function that you can use in this case.

if you want your recordset in a multidimensional array you can use this kind of construct
Code:
$query = "select * from table";
$result = mysql_query($query);
while ($row[] = mysql_fetch_assoc($result)){
//wait
}
you now have the recordset in the $row multidimensional array. not a great idea with big recordsets, of course....
 
jpadie,

That did it!

I see you suggest to use _assoc, same as thenewa2x. I also notice that you changed a couple of other things; I will review this syntax and make it my own if you do not mind :)

Thank you both.


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Jose

my apologies: i normally try to comment or highlight changes.

in this case the key changes were

+ to use the mysql_num_rows() function to determine whether any rows were returned.
+ to change to mysql_fetch_assoc() as this is less overhead than *array (which returns both a numeric array and an associative array)
+ to change your $var = $var . 'something else' syntax to use the shorthand form of $var .= 'something else'. this is called the concatenate assignment operator and appends (to the left hand side) 'something else' to the current value of $var.

apart from deleting some (now) redundant lines, I don't believe I made any other changes.
 
jpadie,

I thank you very much for taking the time to detail this for me. Your snip of code solved the problem.

I am not sure what was broken with the original code, but I can live with that. I am now set to use what you posted here as a standard.

You see, since I am learning PHP, I will take whatever I can and make it my own. As I grow into a mature developer in PHP, I am sure to define my comfort zone (but never stop learning) and apply same principles in everything I write.

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top