开发者

bad planing with mysql_fetch_assoc?

I am making a page where people can make posts. All of those posts are then shown in a table of 24 cells. I can have the last 24 posts shown with no problem, but now I don't know how to show the prior group(s) of posts. How can I fix my code to do that? I actually have this:

(I'm removing lines to make it easy to read)

$sql = "SELECT
    topics.topic_id,
    topics.topic_subject
ORDER BY
    topics.topic_id DESC";

//   ---check everything is fine----   //

function retrieve_info($result)
{
 if($row = mysql_fetch_assoc($result))
     {echo $topic_if; echo $topic_subject; //and what I want in every cell
     }
}

<table width="100%" height="75开发者_Go百科1" >
<tr><td><?php retrieve_info($result);?></td>
    <td><?php retrieve_info($result);?></td>
    <td><?php retrieve_info($result);?></td>
    <td><?php retrieve_info($result);?></td></tr>
<!-- repeat a few more times :-) -->
</table>

I though that by changing the variable $row with a number before the if statement would alter the output, but I still see the same data printed on screen. What should I do to be able to show next group of posts? Thanks!!!


At some point when you have hundreds or thousands of records, you are going to want to paginate the results and not just select all records from the table.

To do this you will run one query per 24 records, your sql would be more like this:

$sql = "SELECT
    topics.topic_id,
    topics.topic_subject
ORDER BY
    topics.topic_id DESC
    LIMIT 0, 24
";

and for the next 24,

LIMIT 24, 24

then

LIMIT 48, 24

and so on.

You would then make next/previous buttons to click which would refresh the page and dispay the next 24, or you would get the next results with an AJAX request and append the next 24 through the DOM.

This suggests having to take a slightly different approach then calling the same function from each table cell.

More like get the relevant 24 results based on the page number you are on, then loop through the results array and print out the table code with values inside it. Based on if the iterator of the loop is divisible by 4 (looks like your grid is 4x6), you print out new tags for the new row, and that sort of thing.

Search around a bit for pagination in php and mysql to get a sense of how this all fits together.


function retrieve_info($result)
{
  while($row = mysql_fetch_assoc($result))
  { 
     $topic_id = htmlspecialchars($row['topic_id']);
     $topic_subject = htmlspecialchars($row['topic_subject']);
     echo '<td>';
     echo $topic_if;
     echo $topic_subject; //and what I want in every cell
     echo '</td>';
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜