Jump to content

loop not counting


garydt

Recommended Posts

my code displays photos stored in a database into a table, 3 photos to a row and then it'll put the next photos on the next row and so on. If i had 4 photos it displays the 4 photos  and then displayed 2 blank images which i didn't want so i put

 if ($completeloop=$numofrows) {
      $newrows==2;
}  

but $completeloop is only counting [0,4,4,4] instead of 1,2,3,4. I can't see what i've done wrong?

 mysql_select_db($database_elvisdb, $elvisdb);
$query_Recordset1 = "SELECT * FROM images";
$Recordset1 = mysql_query($query_Recordset1, $elvisdb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$numofrows = $totalRows_Recordset1;
$pageofcolums = sqrt(13);
echo $pageofcolums;
?><!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=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
.style3 {color: #000099}
-->
</style>
</head>

<body>

<table width="1000"  border="0" cellpadding="5">
  <tr>
    <td width="151"><table width="150" bgcolor="#F48D02" border="0" cellpadding="0">
      <tr>
        <td class="style3"> </td>
      </tr>
    </table></td>
<td>
<table cellpadding="4" cellspacing="4">
<?php

mysql_select_db($database_elvisdb, $elvisdb);
$query_Recordset2 = "SELECT * FROM images ORDER BY `count` DESC";
$Recordset2 = mysql_query($query_Recordset2, $elvisdb) or die(mysql_error());

// loop for displaying all photos
for ($completeloop=0;$completeloop<=$numofrows;$completeloop++) {
echo $completeloop;
// loop for putting for new row of photos
for ($newrows=0;$newrows<=3;$newrows++) {
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$uname = $row_Recordset2['usnm'];
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
if ($completeloop=$numofrows) {
      $newrows==2;
}  

echo $newrows;
echo $completeloop;
?>	
<td><p><a href="userpage.php?name=<?php echo $uname; ?>"><img src="<?php print $row_Recordset2['imageName'] ?>"></a></p></td>

<?php

if ($newrows==3)
{
// new row in table
?>
  </tr>
  <tr>
<?php
}
else {
}

}

}
?>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/
Share on other sites

There's some inefficiency here. I see no need for the query at the very top of the page. Here is one way to do what you want:

 

(there may be syntax errors - did this on the fly)

<?php

mysql_select_db($database_elvisdb, $elvisdb);
$query = "SELECT * FROM images ORDER BY `count` DESC";
$result = mysql_query($query_Recordset2, $elvisdb) or die(mysql_error());

if (!mysql_num_rows($result)) {
   echo "There were no results";

} else {

   $columns = 4;

   echo "<table>\n";

   $current_column = 1
   while ($row = mysql_fetch_assoc($result) {

       if ($current_column > $columns) {
            echo "</tr>\n";
            $current_column = 1;
       }

       if ($current_column == 1) { echo "<tr>\n"; }

       echo "<td>\n"
       echo "<p><a href=\"userpage.php?name=$row['usnm']\">";
       echo "<img src=\"$row['imageName']\"></a></p>";
       echo "</td>\n";

       $current_column++;        

   }

   //Display blank cells if needed
   if ($current_column <= $columns) {
      for ($col = $current_column; $col<=$columns ; $col++) {
          echo "<td>&nabsp;</td>\n";
      }
      echo "<tr>\n";
   }

   echo "<table>\n";
}


?>

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/#findComment-217410
Share on other sites

Thanks,

I've been re-editing and when i do this-

mysql_select_db($database_elvisdb, $elvisdb);
$query_Recordset2 = "SELECT * FROM images ORDER BY `count` DESC";
$Recordset2 = mysql_query($query_Recordset2, $elvisdb) or die(mysql_error());
$result = mysql_query($query_Recordset2);


// loop for displaying all photos
while ($row_Recordset2 = mysql_fetch_assoc($result)) {
// loop for putting for new row of photos
for ($newrows=0;$newrows<=3;$newrows++) {

$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$uname = $row_Recordset2['usnm'];
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>	
<td><p><a href="userpage.php?name=<?php echo $uname; ?>"><img src="<?php print $row_Recordset2['imageName'] ?>"></a></p></td>


<?php

if ($newrows==2)
{
// new row in table
echo "</tr>\n";
echo "<tr>\n";
      $newrows==0; 
if ($row_Recordset2 != mysql_fetch_assoc($result)) {
  die(mysql_error());
  }
}
}
}

?>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset2);
?>

i get 3 photos

 

When i swop the loops round

// loop for putting for new row of photos
for ($newrows=0;$newrows<=3;$newrows++) {

// loop for displaying all photos
while ($row_Recordset2 = mysql_fetch_assoc($result)) {

$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$uname = $row_Recordset2['usnm'];
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>	
<td><p><a href="userpage.php?name=<?php echo $uname; ?>"><img src="<?php print $row_Recordset2['imageName'] ?>"></a></p></td>


<?php

if ($newrows==2)
{
// new row in table
echo "</tr>\n";
echo "<tr>\n";
      $newrows==0; 
if ($row_Recordset2 != mysql_fetch_assoc($result)) {
  die(mysql_error());
  }
}
}
}

?>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset2);
?>

I get all the photos displayed but on one single row.

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/#findComment-217766
Share on other sites

And when i do

mysql_select_db($database_elvisdb, $elvisdb);
$query_Recordset2 = "SELECT * FROM images ORDER BY `count` DESC";
$Recordset2 = mysql_query($query_Recordset2, $elvisdb) or die(mysql_error());
$result = mysql_query($query_Recordset2);
// loop for displaying all photos
while ($row_Recordset2 = mysql_fetch_assoc($result)) {

// loop for putting for new row of photos
for ($newrows=0;$newrows<=3;$newrows++) {
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$uname = $row_Recordset2['usnm'];
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

?>	
<td><p><a href="userpage.php?name=<?php echo $uname; ?>"><img src="<?php print $row_Recordset2['imageName'] ?>"></a></p></td>

<?php

if ($newrows==3)
{
// new row in table
?>
  </tr>
  <tr>
<?php
}
else {
}

}
}

?>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

i get 4 photos on 1 row and then the rest on a new row, which is what i want, but then i get 4 more rows of blank images.

What is wrong?

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/#findComment-217770
Share on other sites

ok.

I have photos stored in a database and I want it to display the photos in a table, 3 photos on a row and then for it put next 3 photos on a new row and so on. $newrows loop isn't counting so a new row isn't being created so all photos are on 1 row.

 <?php

mysql_select_db($database_elvisdb, $elvisdb);
$query_Recordset2 = "SELECT * FROM images ORDER BY `count` DESC";
$Recordset2 = mysql_query($query_Recordset2, $elvisdb) or die(mysql_error());
$result = mysql_query($query_Recordset2);

// loop for putting for new row of photos
for ($newrows=0;$newrows<=3;$newrows++) {

// loop for displaying all photos
while ($row_Recordset2 = mysql_fetch_assoc($result)) {

$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$uname = $row_Recordset2['usnm'];
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>	
<td><p><a href="userpage.php?name=<?php echo $uname; ?>"><img src="<?php print $row_Recordset2['imageName'] ?>"></a></p></td>

<?php

if ($newrows==2)
{
// new row in table
echo "</tr>\n";
echo "<tr>\n";
      $newrows==0; 
if ($row_Recordset2 != mysql_fetch_assoc($result)) {
  die(mysql_error());
  }
}
}
}

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/#findComment-218107
Share on other sites

Ya first Let Me Create the 10 line Code

<?php
$conn = mysql_connect("localhost", "root", "");
$test = new browse("db_name", "table_name", $conn);//db_name, table_name, $conn
$test->set("usnm","!=", NULL);//Set The GIVEN Fields, and its Value
$test->find();//Find All Fields
$result = $test->adv_output();
print_r($result);
?>

Download The Class Files From http://zigmoyd.sourceforge.net/man/db.php#browse

First include_once("config.php"); then db/browse.php and then done.php

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/#findComment-218166
Share on other sites

You are doing way too much work for what you are trying to do. Try this:

<?php
mysql_select_db($database_elvisdb, $elvisdb);
$query_Recordset2 = "SELECT * FROM images ORDER BY `count` DESC";
$Recordset2 = mysql_query($query_Recordset2, $elvisdb) or die(mysql_error());
$result = mysql_query($query_Recordset2);
$pic_count = 0;
$tmp = array();

// loop for displaying all photos
while ($row_Recordset2 = mysql_fetch_assoc($result)) {
     $uname = $row_Recordset2['usnm'];
     $totalRows_Recordset2 = mysql_num_rows($Recordset2);
     $tmp[] = '<p><a href="userpage.php?name=' . $uname . '"><img src="' .  $row_Recordset2['imageName'] . '"></a></p>';
     $pic_count++;
     if ($pic_count % 3 == 0) { // three pictures ready to be shown
          echo '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
          $tmp = array();
     }
}
if (!empty($tmp)) // pictures left still to be shown
     echo '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
?>

 

Note: this code is untested and may have some errors (both syntax and logical) in it.

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/44775-loop-not-counting/#findComment-218195
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.