phpsycho Posted June 20, 2011 Share Posted June 20, 2011 I got a major brain cramp. I can't seem to organize my thoughts very much anymore lol so what I am trying to do is display entries from my db using pagination. First I didn't use pagination and my code looked like this: $numrow = mysql_num_rows(mysql_query("SELECT * from `messages` WHERE (`from`='{$_SESSION['id']}' OR `to`='{$_SESSION['id']}') AND `mail_id`='$id'")); if($numrow > 3){ $lim = $numrow-3; }else{ $lim = 0; } $ms=mysql_query("SELECT * from `messages` WHERE (`from`='{$_SESSION['id']}' OR `to`='{$_SESSION['id']}') AND `mail_id`='$id' ORDER BY `id` ASC LIMIT $lim,3"); so that worked the way I liked it to. it would display 3 entries and the newest entry would display last. now that I have pagination it displays last, but last meaning the last page. I want the newest entries on the first page but show the newest entry last but on the first page. $per_page = 3; $page=clean_up($_GET['page']); $id=clean_up($_GET['id']); $start = ($page-1)*$per_page; $ms=mysql_query("SELECT * from `messages` WHERE (`from`='{$_SESSION['id']}' OR `to`='{$_SESSION['id']}') AND `mail_id`='$id' ORDER BY `id` DESC LIMIT $start,$per_page"); Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/ Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 now that I have pagination it displays last, but last meaning the last page. I want the newest entries on the first page but show the newest entry last but on the first page. what i meant was that I want to display entries and order them so the newest one is displayed last on each page there is. so the last page has the oldest entries but the oldest entry of them all should be displayed first on the last page. possible? Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232427 Share on other sites More sharing options...
monkeytooth Posted June 20, 2011 Share Posted June 20, 2011 Try changing DESC to ASC Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232430 Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 that doesn't do it. look at my original code before the pagination. that worked but now that there is pagination its kinda messed up. Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232433 Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 I think I have to do something with the $start.. just don't know what.. does anyone understand how the first code I had worked and why it doesn't now that I have pagination? is it possible to do what I want for it to do? Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232474 Share on other sites More sharing options...
dcro2 Posted June 20, 2011 Share Posted June 20, 2011 Why would you want to list it like that? o_o Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232491 Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 because I have a reply box below it and its jquery so when it is submitted it add under the last message. So I dont want the recent message being at the top of the page away from the reply section. just like facebooks messaging system Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232495 Share on other sites More sharing options...
gizmola Posted June 20, 2011 Share Posted June 20, 2011 Stay with your DESC query. Fetch all the rows in a loop into an array called rows. Set $rows = array_reverse($rows); foreach ($rows as $row) -- output $row. Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232499 Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 hmm did that.. $ms=mysql_query("SELECT * from `messages` WHERE (`from`='{$_SESSION['id']}' OR `to`='{$_SESSION['id']}') AND `mail_id`='$id' ORDER BY `id` DESC LIMIT $start,$per_page"); $rows = array_reverse($ms); foreach ($rows as $msg){ $mamessage=clean_up($msg['message']); $from=clean_up($msg['from']); and it doesn't display anything.. Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232506 Share on other sites More sharing options...
dcro2 Posted June 20, 2011 Share Posted June 20, 2011 Ohh yeah.. sorry, I was in the middle of class I think he meant: $ms=mysql_query("SELECT * from `messages` WHERE (`from`='{$_SESSION['id']}' OR `to`='{$_SESSION['id']}') AND `mail_id`='$id' ORDER BY `id` DESC LIMIT $start,$per_page"); while($row = mysql_fetch_array($ms)) { $rows[] = $row; } $rows = array_reverse($rows); Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232515 Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 oh.. alrighty. so where does the content go? inside the while I assume right? so like.. $ms=mysql_query("SELECT * from `messages` WHERE (`from`='{$_SESSION['id']}' OR `to`='{$_SESSION['id']}') AND `mail_id`='$id' ORDER BY `id` DESC LIMIT $start,$per_page"); while($row = mysql_fetch_array($ms)) { $rows[] = $row; $message=clean_up($row['message']); $from=clean_up($row['from']); echo "$from $message"; } $rows = array_reverse($rows); hmm maybe not.. I don't think that makes sense.. but if I put it in after the array_reverse it wouldn't display each row of data.. it would only display one, right? idkk.. im totally confused now. with what I had before from gizmola spit out an error, just forgot I didn't have error reporting off so I had to turn it on. it said: Warning: array_reverse() expects parameter 1 to be array, resource given in /var/www/satanicempire/pages/message_data.php on line 33 Warning: Invalid argument supplied for foreach() in /var/www/satanicempire/pages/message_data.php on line 34 Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232518 Share on other sites More sharing options...
phpsycho Posted June 20, 2011 Author Share Posted June 20, 2011 nevermind I got it. thanks guys!! Quote Link to comment https://forums.phpfreaks.com/topic/239922-brain-cramp/#findComment-1232520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.