Jump to content

Get row number


avo

Recommended Posts

Hi All

 

Can anyone help me out with this please

 

what i would like to do is be able to do is get a count from a mysql db lets say i have 3 rows that match what my select statment is asking for i would like to know the order that thay was recived in

 

------------------

id ¦ int ¦test

50¦ 71 ¦ 84

50¦ 74 ¦ 43

50¦ 83 ¦ 43

 

ok that is my db i requested SELECT * FROM this_table WHERE id='50'

but i dont want the int or test information i want the order say 74 from int was selected if running from top to bottom would be 2

if 83 it would be 3

i need to output his requested sequence from where it was selected in the list

 

hope this makes sence

 

thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/46633-get-row-number/
Share on other sites

well how do you want it to be stored? would you just like it to be printed to the browser, or stored in an array? what?

 

<?php
        $sql = "SELECT * FROM your_table WHERE id = '$val'";
        $query = mysql_query($sql) OR die(mysql_error());

        $i = 1;
        while($row = mysql_fetch_array($query)){
                echo $i ." ". $row['id'] ." ". $row['int'] ." ". $row['test'] ."<br />\n";
                $i++;
        }
?>

Link to comment
https://forums.phpfreaks.com/topic/46633-get-row-number/#findComment-227083
Share on other sites

Hi

Thank you that would work but if i was to only what the second row outputed (50¦ 74 ¦ 43)

 

it would still read / echo out 1 as when it goes throught the loop its counting that way and not from the selected portion of the db

 

help greatly appriciated.

Link to comment
https://forums.phpfreaks.com/topic/46633-get-row-number/#findComment-227093
Share on other sites

Here is my code

 

	db_connection ();//db connection

	$sql = mysql_query( "SELECT page_id,page_number FROM sur_pages WHERE sur_id='".$_REQUEST['survey_id']."'ORDER BY page_number ASC" ) or die (mysql_error());

	echo "<option value='all'>Show all pages</option>";

	while ($row = mysql_fetch_array($sql) ) 
	{	
		$page_id 	    = $row['page_id'] ;
		$page_number 	= $row['page_number'] ;

		#assign session to always keep selected highlighted in dropdown
		#dont duplicate the selected row
		if ($eliminate_row == $page_id ){
			$_SESSION['design_view'] =  $eliminate_row ; 
		} else {
		 echo "<option value='$page_id'>Show only page: $page_number</option>";
		}
	}	

Link to comment
https://forums.phpfreaks.com/topic/46633-get-row-number/#findComment-227097
Share on other sites

Hi

 

Not realy no

 

id ¦ int ¦test

50¦ 71 ¦ 84

50¦ 74 ¦ 43

50¦ 83 ¦ 43

 

i want the order they was received in say the statement pulls out 71 from int this would now echo out 1 if it pulls out 83 this would echo 3

 

ect ect

 

the reason why i need it this way is that a number is created from other code but these lines can be deleted and need to be deleted also new lines will be created but the id will allways stay the same this will not be deleted so this is the only common line i can use to pull the information but i need the results from the database to display links which it does but the links in the dropdown box are refferencing the int collum but i need to refference them in a way of order.

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/46633-get-row-number/#findComment-227107
Share on other sites

Hi Thanks

 

Think i have sorted it out well all is working good now

 

i used the keys from an array rather than the loop count which worked well

 

all help appriciated

 

this is what i ended up with :-

	db_connection ();//db connection

	$sql = mysql_query( "SELECT page_id,page_number FROM sur WHERE sur='".$_REQUEST['sur_id']."'ORDER BY page_number ASC" ) or die (mysql_error());

	echo "<option value='all'>Show all pages</option>";

	$x=1 ; //added
	while ($row = mysql_fetch_array($sql) ) 
	{	
		$page_id 			= $row['page_id'] ;
		$real_number 			= $row['page_number'] ;
		$page_number["$real_number"]    = $x++ ;#use keys as numbering //added


		if ($eliminate_row == $page_id ){
			$_SESSION['design_view'] = $eliminate_row ; 
			$selected_row = $page_number["$real_number"];//added
		} else {
		 echo "<option value='$page_id'>Show only page: ".$page_number["$real_number"]."</option>";//added$page_number["$real_number"]
		}
	}return $selected_row ;

Link to comment
https://forums.phpfreaks.com/topic/46633-get-row-number/#findComment-227209
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.