1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Row counter

Discussion in 'PHP' started by khan11, Dec 27, 2009.

  1. #1
    Hello guys!


    I want to show row plain row counting in my database preview page. Though i've added and "id" column in my db, but it has an auto increment and i delete some entries, therefore in cannot display counting in consecutive order.

    For example; Following is my database:

    ID - Name - Other
    3 - abc - 44444
    5 - def - 324234
    9 - lem - 232323


    What i want is to show on the page like this:

    Sr. No. - name - other
    1 - abc - 4444
    2 - def - 4444


    and so on..

    I'm using this command to generate my database record:

    
    $query = "SELECT * 
    				FROM seven 
    				ORDER BY `seven`.`id` DESC ";
    $result = mysql_query($query);
    
    while($result_array = mysql_fetch_assoc($result)){
    echo "Name: " . $result_array['name'];
    echo "Other: " . $result_array['other'];
    }
    
    PHP:
    Any help??

    Thanks in advance..
     
    khan11, Dec 27, 2009 IP
  2. Brandon_R

    Brandon_R Peon

    Messages:
    330
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $query = "
    	SELECT *
    	FROM seven
    	ORDER BY id ASC
    ";
    
    $result = mysql_query($query);
    
    while($result_array = mysql_fetch_assoc($result))
    {
    	echo "Name: " . $result_array['name'];
        
    	echo "Other: " . $result_array['other'];
    }
    Code (php):
    or if your row id's are all out of order because of deletion you can implement your own counter.

    $query = "
    	SELECT *
    	FROM seven
    	ORDER BY id ASC
    ";
    
    $result = mysql_query($query);
    
    $counter = 0;
    
    while($result_array = mysql_fetch_assoc($result) AND $counter++)
    {
    	echo "Sr: " . $counter . "<br />";
        
    	echo "Name: " . $result_array['name'] . "<br />";
        
    	echo "Other: " . $result_array['other'] . "<br />";
    }
    Code (php):
     
    Brandon_R, Dec 27, 2009 IP
  3. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    ^ Thanks for your quick reply, but its not working :( I think there is something wrong with "AND" code.
     
    khan11, Dec 27, 2009 IP
  4. Brandon_R

    Brandon_R Peon

    Messages:
    330
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $query = "
        SELECT *
        FROM seven
        ORDER BY id ASC
    ";
    
    $result = mysql_query($query);
    
    $counter = 0;
    
    while($result_array = mysql_fetch_assoc($result))
    {
        $counter++
    
        echo "Sr: " . $counter . "<br />";
       
        echo "Name: " . $result_array['name'] . "<br />";
       
        echo "Other: " . $result_array['other'] . "<br />";
    }
    Code (markup):
     
    Brandon_R, Dec 27, 2009 IP
    khan11 likes this.
  5. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #5
    ^

    Thanks.. Seems to be working.. +rep..
     
    khan11, Dec 27, 2009 IP