I can't see any apparent reason why it stops on 1... I'm pulling out most viewed publications and just want to rank them 1, 2, 3. I'm adding the value of 0 outside of the while loop and adding 1 inside the while loop. I've tried $no=$no+1, $no++, $no = $no+1 all of them pull them out as 1, 1, 1 <?php //Get most viewed publications $resultb = mysql_query("SELECT * FROM publication ORDER BY viewcount DESC LIMIT 3"); while($rowb = mysql_fetch_array($resultb)) { $id = $rowb[id]; $viewcount = $rowb[viewcount]; $title = $rowb[title]; //Get publication info $resulth = mysql_query("SELECT * FROM publication_issue WHERE publication_id = '$id' LIMIT 1"); $no = 0; while($rowh = mysql_fetch_array($resulth)) { $no = $no+1; $img = $rowh[img]; $titleb = $rowh[title]; echo "<!-- PUBLICATION -->"; echo '<div class="featcontainer">'; echo " <div class=\"left\">"; echo $no; echo "</div>"; echo " <div class=\"mid\"> <a href=\"#\"><img src=\"/images/uploads/publications/image/big/$img\" height=\"90\" width=\"76\"></a></div>"; echo ' <div class="right1"><a href="#"></a></div>'; echo '</div>'; echo "<hr>"; }} ?> PHP:
Easy fix: You need to declare the $no outside of the 2 whiles. like this <?php //Get most viewed publications $resultb = mysql_query("SELECT * FROM publication ORDER BY viewcount DESC LIMIT 3"); $no = 0; while($rowb = mysql_fetch_array($resultb)) { $id = $rowb[id]; $viewcount = $rowb[viewcount]; $title = $rowb[title]; //Get publication info $resulth = mysql_query("SELECT * FROM publication_issue WHERE publication_id = '$id' LIMIT 1"); while($rowh = mysql_fetch_array($resulth)) { $no = $no+1; $img = $rowh[img]; $titleb = $rowh[title]; echo "<!-- PUBLICATION -->"; echo '<div class="featcontainer">'; echo " <div class=\"left\">"; echo $no; echo "</div>"; echo " <div class=\"mid\"> <a href=\"#\"><img src=\"/images/uploads/publications/image/big/$img\" height=\"90\" width=\"76\"></a></div>"; echo ' <div class="right1"><a href="#"></a></div>'; echo '</div>'; echo "<hr>"; }} ?> PHP: If it is INSIDE the while loop, it will always reset to 0, therefore you only get 1, 1, 1
Thanks. Looked at that code for hours and it didn't even cross my mind that the first while loop would affect it!
some advice: 1) learn about pre and post increment/decrements. ++$no; is far simpler than $no=$no+1; 2) do not waste memory and execution time copying values that already exist into new variables. 3) if you use single quotes you don't have to escape your doubles, and it executes a smidgin faster. 4) don't waste time saying echo over and over when you can do it with a single echo. 5) Tab key, USE IT! Makes the code clearer and easier to follow. 6) don't forget that it's no longer 'proper' to index an associative array without using quotes. 7) This is 2012, not 2004, you should probably be using mysqli or PDO and not the deprecated mysql_ functions. Cleaning up just some of those issues: <?php // Get most viewed publications $resultb=mysql_query(" SELECT * FROM publication ORDER BY viewcount DESC LIMIT 3 "); $no=0; while ($rowb=mysql_fetch_array($resultb)) { //Get publication info $resulth=mysql_query(" SELECT * FROM publication_issue WHERE publication_id = '".$rowb[id]."' LIMIT 1" ); while ($rowh = mysql_fetch_array($resulth)) { echo ' <!-- PUBLICATION --> <div class="featcontainer"> <div class="left">',++$no,'</div> <div class="mid> <a href="#"> <img src="/images/uploads/publications/image/big/',$rown['img'],'" height="90" width="76" alt="do not forget this is a REQUIRED attribute!" /> </a> </div> <div class="right1"><a href="#"></a></div> </div> <hr>'; } // while $rowh } // while $rowb ?> Code (markup):