How Can I make numbering on mySQL results. I was trying to use $_SESSION[], But I am not able. Please help me
<?php
$id=$_POST['id'];
function showmember($id){
$int=$_SESSION['int']+1;
$query = mysql_query("SELECT id,name,sponsorid,node FROM ".memberlogtbl." WHERE locationid='".$id."' ORDER BY join_time DESC LIMIT 500");
$total=mysql_num_rows($query);
while($sql=mysql_fetch_array($query)){
echo "<tr style='border:1px solid #777'>
<td> $int</td>
<td> ".$sql['id']."</td>
<td> ".$sql['name']."</td>
<td> ".$sql['sponsorid']."</td>
<td> ".$sql['node']."</td>
<td> ".(date('d/m/y : h:iA', $sql['join_time']))."</td>
</tr>";
showmember($sql["id"]);
}
mysql_free_result($query);
}
showmember($id);
}
unset($_SESSION['int']);
?>
I was trying to do like below:
1 H. Andri 17667 left 12/12/2011
2 A. Kumar 32321 left 12/12/2011
3 D. Das 12121 right 1/1/2012
….. so on numbering
First of all, sanatise your inputs. Use:
This way you’ll avoid a SQL injection attack.
Secondly, you have a weird thing going on here… You are using a recursive function instead of just preparing a nice SQL statement… I’m not sure what you’re trying to do there but it looks really weird. Why are you doing it recursively? You are theoretically executing a large number of queries instead of just one, plus you have a weird structure going on there (you get
idfrom the table and then prepare a statement where you use that value and compare it withlocationid, what is your key/index in your table? Shouldn’t it be unique?)Thirdly, you can simply have a variable like
$iwhich you increase ($i++) at the end of yourwhileloop. At the moment you’re displaying some$_SESSIONvariable that I’m not sure what you’re using for. It will not work because you are not passing that variable in your recursive function, so it will always be the same. You can use$_GLOBALvalues instead. But anyway, the best way is to simple prepare a variable for your while loop. So before yourwhileclause add:and then at the end of your while loop add
and display that variable in your first table cell.
Finally I assume that
memberlogtblhas been set as a constant as you are referring to it as such (i.e. you are not treating it as a variable,$memberlogtbl).Overall your code is really, really confusing and potentially has a lot of mistakes in it. I would consider rewriting it.