While loop +1 gets stuck on 1? [PHP]

Discussion in 'Programming' started by scottlpool2003, Sep 13, 2012.

  1. #1
    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:
     
    Solved! View solution.
    scottlpool2003, Sep 13, 2012 IP
  2. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #2
    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
     
    GMF, Sep 13, 2012 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Thanks. Looked at that code for hours and it didn't even cross my mind that the first while loop would affect it!
     
    scottlpool2003, Sep 13, 2012 IP
  4. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #4
    Well, you learn through your mistakes :p
     
    GMF, Sep 13, 2012 IP
  5. wyfytangsh

    wyfytangsh Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i can help you because i am Just beginning to learn programming
     
    wyfytangsh, Sep 13, 2012 IP
  6. #6
    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):
     
    deathshadow, Sep 13, 2012 IP
  7. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #7
    Thanks.

    Yes I agree, I really need to get up-to-date with modern standards.

    Thanks again.
     
    scottlpool2003, Sep 14, 2012 IP