SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Current/previous logic in a loop

    What I'm trying to do is output rank using PHP by checking if the rating/number of votes has changed (from the last loop). I've been told I can do this by way of current/previous logic in a loop, but I don't know how to do this. Does anyone have an answer?

    http://www.sitepoint.com/forums/showthread.php?t=620653 - see this thread for my original question.

  2. #2
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to do a current/previous logic in a loop, you simply store the value in a variable, which will live untill the next iteration.
    example:
    Code PHP:
    <?php
    $result = mysql_query("SELECT value FROM table ORDER BY value");
    $previous = '';
    while($row = mysql_fetch_assoc($result))
    {
    	if($row['value'] == $previous)
    	{
    		// do something...
    	}
    	$previous = $row['value'];
    }
    ?>

    To do a current/next logic, you need to do a litte work first..
    Iterate through the SQL result, storing it in a new array, then use a for-loop to process the data

    Example:
    Code PHP:
    $result = mysql_query("SELECT value FROM table ORDER BY value");
    $data = array();
    while($row = mysql_fetch_assoc($result))
    {
    	$data[] = $row;
    }
     
    $size = sizeof($data);
    for($i = 0; $i < $size; $i++)
    {
    	// look at the previous item in the data array...
    	// since the "first previous" doesnt exist
    	// we only look at the previous item when 
    	// the $i is greater than 0
    	if($i > 0)
    	{
    		if($data[$i]['value'] == $data[$i-1]['value'])
    		{
    			// do something..
    		}
    	}
     
    	// look at the next item in the data array..
    	// when $i equals the total size of the data array
    	// there wont be any next item,
    	// so no need to look at it when we hit the last item
    	if($i < $size)
    	{
    		if($data[$i]['value'] ==  $data[$i+1]['value'])
    		{
    			// do something..
    		}
    	}
    }
    zalucius

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh thank you so much! It all makes sense now

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by zalucius View Post
    Iterate through the SQL result, storing it in a new array...
    that works, but not if the result set doesn't fit into memory

    another way to approach it is to save the current row into memory, get the next row, compare to the current row (which was saved), then output the current row (from memory) and make the next row the current row

    this is very much like current/previous except it's next/current, and all you need to do is store one row -- not the entire result set!!

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by zalucius View Post
    Example:
    Code PHP:
    $result = mysql_query("SELECT value FROM table ORDER BY value");
    $data = array();
    while($row = mysql_fetch_assoc($result))
    {
    	$data[] = $row;
    }
    Tighter is

    PHP Code:
    while ($data[] = mysql_fetch_assoc($result)) {} 

  6. #6
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys,

    Having a bit of a cross discussion here: http://www.sitepoint.com/forums/show...=1#post4281710

    Just wondering if anyone over this side of the forums has an answer?

    Thanks,
    Theo.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •