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

Do/while problem? 2

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
Hello.

Here is the page in question:
I am creating links from the database results and this is where my problem is. It seems that no matter what I do, I can generate only one link from the database records.

Code:
mysql_select_db($database_connect, $connect);
if(!$type){
	$type = "web";
}
$query_myQuery = "SELECT * FROM folio WHERE type = '".$type."'";
$myQuery = mysql_query($query_myQuery, $connect) or die("Could not access database. ".mysql_error());
$row_myQuery = mysql_fetch_assoc($myQuery);
$totalRows_myQuery = mysql_num_rows($myQuery);
mysql_free_result($myQuery);

Code:
<?
$curRow = 0;
if($row_myQuery){
	do{
		$curRow++;
	?>
	<div id="folioPiece">
		<a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="<? echo $type;?>/images/th_<? echo $row_myQuery['pic'];?>" width="35" height="35" border="0" class="folioThumb" title="<? echo $row_myQuery['name'];?>" /></a><span class="folioThumb"><? echo $row_myQuery['name'];?></span>
		<div id="folioCorner"><a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="../images/foliopiece_corner.gif" border="0" width="14" height="14" title="view project" /></a></div>
	</div>
</div>
<? }while($curRow<$totalRows_myQuery);
}
?>

For the only result displayed, $curRow has a value of 1. However, $totalRows_myQuery has a value of 3.

I suspect it may be the while section of my loop that I am having trouble with.

Thanks everyone.

frozenpeas
 
Why use do..while?
Code:
<?
if($row_myQuery){
    [red]for ($curRow = 0; $curRow<$totalRows_myQuery; $curRow++) {[/red]
    ?>
    <div id="folioPiece">
        <a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="<? echo $type;?>/images/th_<? echo $row_myQuery['pic'];?>" width="35" height="35" border="0" class="folioThumb" title="<? echo $row_myQuery['name'];?>" /></a><span class="folioThumb"><? echo $row_myQuery['name'];?></span>
        <div id="folioCorner"><a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="../images/foliopiece_corner.gif" border="0" width="14" height="14" title="view project" /></a></div>
    </div>
</div>
<?   }
}
?>

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
How about cleaning up a little?
Code:
<?
if($row_myQuery)
{
  for ($curRow = 0; $curRow<$totalRows_myQuery; $curRow++)
  {
    echo '<div id="folioPiece">\n';
    echo '<a href="' . $_SERVER['PHP_SELF'] . '?type=' . $type . '&project=' . $row_myQuery['name'] . '">\n';
    echo '<img src="' . $type . '/images/th_' . $row_myQuery['pic'] . '" width="35" height="35" border="0" class="folioThumb" title="' . $row_myQuery['name'] . '" />\n';
    echo '</a>\n';
    echo '<span class="folioThumb">\n';
    echo $row_myQuery['name'] . '\n';
    echo '</span>\n';
    echo '<div id="folioCorner">\n';
    echo '<a href="' . $_SERVER['PHP_SELF'] . '?type=' . $type . '&project=' . $row_myQuery['name'] . '">\n';
    echo '<img src="../images/foliopiece_corner.gif" border="0" width="14" height="14" title="view project" />\n';
    echo '</a>\n';
    echo '</div>\n';
    echo '</div>\n';
    echo '</div>\n';
  }
}
?>
See anything that doesn't work?

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Still doesn't work. This is a real pickle.

frozenpeas
 
1. You seem to have an extra </div>.
2. Why are these divs all given the same id? Id is supposed to be unique.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Why bother with the loop structure you're trying to use, when a simple While loop will work fine:
Code:
while ($row_myQuery = mysql_fetch_assoc($myQuery))
{    ?>
    <div id="folioPiece">
        <a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="<? echo $type;?>/images/th_<? echo $row_myQuery['pic'];?>" width="35" height="35" border="0" class="folioThumb" title="<? echo $row_myQuery['name'];?>" /></a><span class="folioThumb"><? echo $row_myQuery['name'];?></span>
        <div id="folioCorner"><a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="../images/foliopiece_corner.gif" border="0" width="14" height="14" title="view project" /></a></div>
    </div>
</div>
<? } ?>

I think this is easier to understand and debug.

Ken
 
Chessbot,

I'm fairly new to CSS and it completely slipped my mind that div ids have to be unique.

I switched them to classes but am still having the other problems.

I'll keep working away at it... I'm sure I'll be back.

Thanks.

frozenpeas
 
I cleaned up my code a bit, got rid of that extra </div> and am using classes where (I think) I should be.

Code:
<?
if($row_myQuery){
	do{
	?>
	<div class="folioPiece">
		<a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="<? echo $type;?>/images/th_<? echo $row_myQuery['pic'];?>" width="35" height="35" border="0" class="folioThumb" title="<? echo $row_myQuery['name'];?>" /></a><span class="folioThumb"><? echo $row_myQuery['name'];?></span>
		<div class="folioCorner">
			<a href="<? echo $_SERVER['PHP_SELF']."?type=".$type."&project=".$row_myQuery['name'];?>"><img src="../images/foliopiece_corner.gif" border="0" width="14" height="14" title="view project" /></a>
		</div>
	</div>
<? }while(@$row = mysql_fetch_assoc($myQuery));
}
?>

The way this currently works, is still only one result is shown. I get this error:

Code:
Warning: mysql_fetch_assoc(): 3 is not a valid MySQL result resource in /temp/portfolio/index.php on line 66

which refers to this line:

Code:
while(@$row = mysql_fetch_assoc($myQuery));

(I am surpressing that error for now - I have to present it in class tomorrow[neutral])

Now, when I try this (which to my understanding is what I *should* be doing):

Code:
while($row_myQuery = mysql_fetch_assoc($myQuery))

I get the same singular result, repeatedly.

Am I using the proper while condition? Thanks again, everyone.

frozenpeas
 
Make sure that your call to mysql_free_result($myQuery) is after you have finished with $myQuery, else it will delete the results stored in $myQuery.

If it aint broke, redesign it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top