using pagination to output array
this is my code
//select all the members id
$result = mysql_query("select id from member");
//create array of ids
$id = array();
// page is the number of my pagination numbering and its from 1 to 10
$dis = $page;// for example $page = 1
// $page get its value every time a user click my pagination numbering i.e $page = $_GET[page];
// guess you understand
while($u_id = mysql_fetch_assoc($result)){
//id is my field name
$id[] = $u_id[id];
}
ec开发者_开发问答ho $id[$dis];
my problem is that this above code works for only 4 records out of 10records. my question is, why is it not working outputin other ids when the array index is 4,5... and howw do i amke it work
secondly the echo part is just the test of my idea but what i wanted to do is to pass the return id to a function for query. please any help
Well not sure if this is your problem but you might want to correct this if its not just a type in your post:
while($u_id = mysql_fetch_assoc($result)){
$id[] = $u_id['id']; // this should be 'id' not $id
}
I think you want to have:
while($u_id = mysql_fetch_assoc($result)){
$id[] = $u_id['id']; // changed from $id inside brackets
}
For starters, as you're using mysql_fetch_assoc()
, your $u_uid
variable will not contain any numeric indices. In fact, it will only ever have an 'id'
key so your while loop should really look like this
$id[] = $u_uid['id'];
This will build the $id
array with every id in your member
table which is probably not what you want. As you mention pagination, I expect you actually want something like this
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$page = max(1, $page); // ensure page is 1 or greater
$rowCount = 5; // you can set this to whatever you want
$offset = ($page - 1) * $rowCount;
$result = mysql_query(sprintf('SELECT id FROM member ORDER BY id LIMIT %d, %d',
$offset, $rowCount));
精彩评论