2012-02-07 14:03
浏览 145

多个for循环 - 如何打印数据

i want to make a website something like, but I will use static data stored in MySQL database. Btw I use php/mysql.
In each list i want to show around 10 links (just like on popurls). In that case, if I would have 20 lists, i would need to make 20 'for' loops (for each particular list).
My question is; is there some better way to print that 20 lists instead of using 20 'for' loops in php.

图片转代码服务由CSDN问答提供 功能建议

我想建立一个类似popurls.com的网站,但我会使用存储在MySQL数据库中的静态数据。 顺便说一句,我使用php / mysql。
在每个列表中我想显示10个链接(就像在popurls上一样)。 在这种情况下,如果我有20个列表,我需要为每个特定列表制作20'for'循环。
我的问题是; 是否有更好的方法来打印20个列表,而不是在PHP中使用20'for'循环。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dqnz43863
    dqnz43863 2012-02-07 14:28

    a for loop or a foreach loop will work fine, but it will be a lot less coding if you just create a single for loop and push content into an array of arrays or an array of strings... you can then do whatever you'd like with the actual content (assuming we're grouping by a column category. I'll use an example that uses an array of strings (and the query that I reference is explained here:

    $query = mysql_query([QUERY THAT GETS ALL ITEMS, BUT LIMITS BY EACH CATEGORY]) or die(mysql_error());
    $all_items = array();
       if (!isset($all_items[$row['category']])){ //if it isn't created yet, make it an empty string
           $all_items[$row['category']] = "";
       $all_items[$row['category']] .= "<li><a href='".$row['url']."'>".$row['title]."</a></li>"; //concatinate the new item to this list

    Now we have an array where the block of HTML for each section is stored in an array keyed by the name of the category. To output each block, just:

    echo  $all_items['category name'];
    点赞 评论
  • douchan0523
    douchan0523 2012-02-07 14:15
    点赞 评论
  • dousuowu4610
    dousuowu4610 2012-02-07 14:16

    Depends a lot on your data input but I could imagine something like this:

    $lists = arrray('list1', 'list2', 'list3');
    foreach ($lists as $current) {
        $data = fetch_data_from_mysql($current);
        foreach ($data as $link) {
            echo "<a href=\"$data\">Link</a>";
    function fetch_data_from_mysql($current)
       $list_data = array();
       // do whatever is required to fetch the list data for item $current from MySQL
       // and store the data in $list_data
       return $list_data;
    点赞 评论
  • doudiemei2013
    doudiemei2013 2012-02-07 14:17

    You just need two foreach loops. Assuming that you take the data from a mysql table (like you wrote), this could be like this:

    $list_query = mysql_query("SELECT * FROM lists";)
    while( $list = mysql_fetch_array($list_query) )
        echo "<h1>{$list['title']}</h1>";
        $query = mysql_query("SELECT * FROM entries WHERE list_id = {$list['id']}");
        while( $entry = mysql_fetch_array($query) )
            echo "- {$entry['name']}<br />";
    点赞 评论
  • dongyou9818
    dongyou9818 2012-02-07 14:18

    You can get all the information from the database and parse it into an array, something like

    array[<news type1>] = array( link1, link2, link3, etc);
    array[<news type2>] = array( link1, link2, link3, etc);

    and so on

    and on the layout you can use

    foreach ($newsCategory AS $categoryLinks) {
      foreach ($categoryLinks AS $newsLink) {
           <show the link and / or extra data>
    点赞 评论
  • dongzhong5967
    dongzhong5967 2012-02-07 14:20

    Just store your links in two-dimensional array. That way you'll have to make 1 outer loop (iterating over lists) and 1 inner loop iterating over links in a particular list.

    $links = array(
      'science' => array('link1', 'link2', ...),
      'sports' => array('link1', 'link2'),
      // ... and so on
    foreach ($links as $category => $urls) {
      echo "Links in category: $category
      foreach ($urls as $url) {
        echo $url . "
    点赞 评论