如何拆分mySQL信息以显示在HTML列中 [英] How to split mySQL information to show in HTML columns

查看:171
本文介绍了如何拆分mySQL信息以显示在HTML列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种方法来将mySQL行分成HTML列。对不起,如果有一个更简单的方法来问这个,但我不能封装我的头。



我已经阅读了很多建议,虽然没有一个他们适合我想要的。
我理解这是很容易漂浮的信息左,并允许它自己堆栈。我的问题是,我用两个不同大小的卡片显示信息的卡形式,我想他们所有堆叠无间隙垂直。



显示这个,我给每张卡从1到3的数字。每张卡号为1的卡应该放在第一列,等等。 这里的示例



正常浮动左侧我这样的:

  | 1 | 2 | 3 | 
| 4 | 5 | 6 |
| 7 | 8 | 9 |

这是显示信息的正确方法,但它会留下大卡旁边的缺口



如果我在一个表或divs中使用一个foreach循环的php,我得到这样的:

  | 1 | 2 | 3 | 
-------
| 4 | 5 | 6 |
-------
| 7 | 8 | 9 |

不幸的是,这几乎是一样的结果。



我也理解,在第一个visibile行之后,最近的div可能被推送在一列而不是另一列。例如,如果列1具有6个特征或大卡,则将需要12个卡来填充其旁边的空间。我不担心这个。



我现在的基本代码是:

 < div class =container> 
<?PHP
$ qry_questions = mysql_query(SELECT STATEMENT **);
$ count = 0;
while($ row = mysql_fetch_array($ qry_questions)){
$ count ++;
/ * GATHER INFORMATION * /
?>
< div class =HTML-formatting-here-with-php-echos>
< / div>
<?PHP
}
?>
< / div>

现在我只是想弄清楚如果我能在php中做一个语句,
if $ count == 1,将此信息附加到列1中,如果$ count == 2,则将其附加到col 2等等。



有没有任何类型的函数,我可以做拆分这样的信息,附加信息,而不完全建立一个新的行?



ps我尝试了array_chunk()没有正确的结果。



编辑:这里是while循环

 < div id =collective> 
< div class =container>
<?PHP
//查找问题及其相关信息
$ qry_questions = mysql_query(SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC/ * LIMIT 0,10* / ); // LIMIT 0,20-20是一次显示的数量
$ num_rows = mysql_num_rows($ qry_questions);
$ max_col = 3;
$ count = 0;
// START THE LOOPING
while($ q_row = mysql_fetch_array($ qry_questions)){
$ q_id = $ q_row ['q_id'];
$ q_votes = $ q_row [' q_votes'];
$ q_answers = $ q_row ['q_answers'];
$ q_title = $ q_row ['q_title'];
$ q_description = $ q_row ['q_description'];
$ q_tags = $ q_row ['q_tags'];
$ q_user_id = $ q_row ['user_id'];
$ q_created = $ q_row ['q_created'];
$ q_modified = $ q_row ['q_modified'];
$ q_featured = $ q_row ['q_featured'];
if($ q_featured == 0){
$ q_status =normal;
} else {
$ q_status =featured;
}
//获取用户USERNAME
$ qry_username = mysql_query(SELECT user_name FROM mbr_user_name WHERE user_id ='$ q_user_id');
$ q_user_name = mysql_result($ qry_username,0);
//查找用户点
$ qry_points = mysql_query(SELECT point_amount FROM mbr_qa_points WHERE user_id ='$ q_user_id');
$ q_user_points = mysql_result($ qry_points,0);
//如果你分配了它,找到它
$ qry_flagged = mysql_query(SELECT * FROM mbr_qa_flagged WHERE q_id ='$ q_id'AND user_id ='$ user_id');
$ flagged = mysql_num_rows($ qry_flagged);
if($ flagged> = 1){
$ flaggedDiv =< div class ='q_flagged'>& nbsp;< / div>;
} else {
$ flaggedDiv =< div class ='flagInnapropriate'>& nbsp;< / div>;
}
//查找多个答案
$ qry_answers = mysql_query(SELECT * FROM mbr_answers WHERE q_id ='$ q_id');
$ q_answers = mysql_num_rows($ qry_answers);
//查找多个投票
$ qry_votes = mysql_query(SELECT * FROM mbr_votes WHERE q_id ='$ q_id');
$ q_votes = mysql_num_rows($ qry_votes);
//每个输入一个列号
$ count ++;
if($ count> = 4){
$ count = 1;
}
< div class =card<?= $ q_status?>>
< div class =top>
< div class =tag>
< div class =title>投票< / div>
< div class =votes>
< div class =number>
<?= $ q_votes?>
< / div>
< / div>
< div class =plus> +< / div>
< / div>
< div class =question>
< p><?= $ count / *。 $ q_title * /?>< / p>
< / div>
< / div>
< div class =middle>
< div class =elapsed>
<?PHP

$ time_since = $ q_created;
$ time_now = time();
$ timeElapsed = $ time_now - $ time_since;

?>
提问<?= elapsedTime($ time_since)?>由
< / div>
< a class =profilehref =./ profile.php?profile_name =<?= $ q_user_name?>>
< div class =avatar>
< img src =pieces / avatars / avatar.php?profile_id =<?= $ q_user_id?>& size = xsmall/>
< / div>
< div class =userName>
<?= $ q_user_name?>
< / div>
< div class =points>
<?= $ q_user_points?> pts
< / div>
< / a>

< / div>
< div class =bottom>
< div class =answer>
< div class =title>
Answers
< / div>
< div class =numAnswers>
<?= $ q_answers?>
< / div>
< div class =answersText>
Answers
< / div>
< / div>
< div class =bestAnswer>
< div class =title>
最佳答案
< / div>
< div class =description>
< p>需要在这里获得最高评分的答案< / p>
< / div>
< / div>
< / div>
< / div>
}
?>

< / div>
< / div>

包含的newAnswerLayout.php只是每张卡的html格式。



另一个编辑:



我相信我只需要做三个mySQL查询选择每第n行。是的,行上的ID是唯一的,但有可能一些行将移动到另一个表,这将留下空白。



我现在想的是查询表,拉第三行,将其放在第一列,等等,从第一个条目开始。



编辑3:
我要找的是做一个SQL查询,从第1行开始抓取每第3行的所有信息,然后我需要

解决方案

使用 float:left;

  http://beta.mybattlereport.com/ styles / global.css 

82 br>
class container



编辑:



第一:卡号为1,2,3,他们都有相同的类卡普通所以如果你应用更改一张卡,它会改变所有的卡



解决方案:尝试使用表格而不是div。



秒:

  card1 card2 card3特色卡,card1卡2 card3特色卡



解决方案:,也可以通过表格进行修改,然后订购
1列将用于card1
2列将用于card2
3列将用于card3
,然后您可以使用此下的其他表格作为特色卡或div。



EDIT2>



您的问题是在sql中获取卡片生成的卡和特色
你应该先按顺序,然后投票



请尝试此

  $ qry_questions = mysql_query FROM mbr_qa_questions ORDER BY q_featured,q_votes DESC/ * LIMIT 0,10* /); 

数字1和2和3你只是给每张卡的数字。所以我没有看到这些数字的任何目的,为什么你是做他们。


I am trying to figure out a way to split mySQL rows into HTML columns. I'm sorry if there is an easier way to ask this, but I can't wrap my head around it.

I have read through very many suggestions though none of them fit what I am looking for. I understand it is very easy to just float the information left and allow it to stack by itself. My problem is that I am showing information in a "card" form with two different sized cards and I would like them all to stack without gaps vertically.

For the purpose of showing this, I've given every card a number from 1-3. Every card numbered 1 should fit in the first column, and so on. example here

Normal floating left gives me something like this:

|1|2|3|
|4|5|6|
|7|8|9|

This is the correct way to display the information, but it leaves gaps where large cards are next to small ones like in the example.

if I use a foreach loop in the php with a table or divs, I get something like this:

|1|2|3|
-------
|4|5|6|
-------
|7|8|9|

Unfortunately, this is pretty much the same outcome. The larger cards push down the row leaving a large gap where a smaller card is.

I also understand that after the first visibile row, the most recent divs could potentially be pushed farther down in one column rather than another. for example, if column 1 has 6 'featured' or large cards, it would take 12 cards to fill the space beside it. I'm not worried about this.

The basic, stripped down code that I have right now is this:

<div class="container">
    <?PHP
    $qry_questions = mysql_query("SELECT STATEMENT**");
    $count = 0;
    while($row = mysql_fetch_array($qry_questions)){
        $count++;
        /*GATHER INFORMATION*/
    ?>
        <div class="HTML-formatting-here-with-php-echos">
        </div>
    <?PHP
        }
    ?>
</div>

Now I'm just trying to figure out if I can make a statement in php that basically says: "if $count == 1, append this information into column 1, if $count == 2, append this into col 2, etc."

Is there any type of function that I can do to split this information like this and append the information without completely building a whole new row?

p.s. I've tried array_chunk() without the proper outcome.

EDIT: Here is the while loop

<div id="collective">
    <div class="container"> 
    <?PHP
        //FIND THE QUESTIONS ASKED AND INFORMATION ON THEM
        $qry_questions = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_votes DESC" /* LIMIT 0, 10"*/);  //LIMIT 0, 20 - 20 being how many to show at a time
        $num_rows = mysql_num_rows($qry_questions);
        $max_col = 3;
        $count = 0;
        //START THE LOOPING
        while($q_row = mysql_fetch_array($qry_questions)){
            $q_id = $q_row['q_id'];
            $q_votes = $q_row['q_votes'];
            $q_answers = $q_row['q_answers'];
            $q_title = $q_row['q_title'];
            $q_description = $q_row['q_description'];
            $q_tags = $q_row['q_tags'];
            $q_user_id = $q_row['user_id'];
            $q_created = $q_row['q_created'];
            $q_modified = $q_row['q_modified'];
            $q_featured = $q_row['q_featured'];
            if($q_featured == 0){
                $q_status = "normal";   
            } else {
                $q_status = "featured"; 
            }
            //GET THE USERS USERNAME
            $qry_username = mysql_query("SELECT user_name FROM mbr_user_name WHERE user_id =                         '$q_user_id'");
            $q_user_name = mysql_result($qry_username, 0);
            //FIND THE USERS POINTS
            $qry_points = mysql_query("SELECT point_amount FROM mbr_qa_points WHERE user_id = '$q_user_id'");
            $q_user_points = mysql_result($qry_points, 0);
            //FIND OUT IF YOU FLAGGED IT
            $qry_flagged = mysql_query("SELECT * FROM mbr_qa_flagged WHERE q_id = '$q_id' AND user_id = '$user_id'");
            $flagged = mysql_num_rows($qry_flagged);
            if($flagged >= 1){
                $flaggedDiv = "<div class='q_flagged'> &nbsp; </div>";
            } else {
                $flaggedDiv = "<div class='flagInnapropriate'> &nbsp; </div>";
            }
            //FIND HOW MANY ANSWERS
            $qry_answers = mysql_query("SELECT * FROM mbr_answers WHERE q_id = '$q_id'");
            $q_answers = mysql_num_rows($qry_answers);
            //FIND HOW MANY VOTES
            $qry_votes = mysql_query("SELECT * FROM mbr_votes WHERE q_id = '$q_id'");
            $q_votes = mysql_num_rows($qry_votes);
            //GIVE EACH ENTRY A COLUMN NUMBER
            $count++;
            if($count >= 4){
                $count = 1; 
            } 
<div class="card <?= $q_status ?>">
    <div class="top">
        <div class="tag">
            <div class="title">Votes</div> 
            <div class="votes">
                <div class="number">
                    <?= $q_votes ?>
                </div>
            </div>
            <div class="plus">+</div>
        </div>
        <div class="question">
            <p><?= $count/* . $q_title*/ ?></p>
        </div>
    </div>
    <div class="middle">
        <div class="elapsed">
            <?PHP

                $time_since = $q_created;
                $time_now = time();
                $timeElapsed = $time_now - $time_since;

            ?>
            Asked <?= elapsedTime($time_since) ?> ago by
        </div>
        <a class="profile" href="./profile.php?profile_name=<?= $q_user_name ?>">
            <div class="avatar">
                <img src="pieces/avatars/avatar.php?profile_id=<?= $q_user_id ?>&size=xsmall" />
            </div>
            <div class="userName">
                <?= $q_user_name ?> 
            </div>
            <div class="points">
                <?= $q_user_points ?>pts
            </div>
        </a>

    </div>
    <div class="bottom">
        <div class="answer">
            <div class="title">
                Answers
            </div>
            <div class="numAnswers">
                <?= $q_answers ?>
            </div>
            <div class="answersText">
                Answers
            </div>
        </div>
        <div class="bestAnswer">
            <div class="title">
                Best Answer
            </div>
            <div class="description">
                <p>Need to get highest rated answer here</p>
            </div>
        </div>
    </div>
</div>   
                }
                ?>

            </div>
        </div>

The included newAnswerLayout.php is just the html formatting of each card.

ANOTHER EDIT:

I believe I just need to make three mySQL queries that select every nth row. Yes, the IDs on the rows are unique, but there is a possibility that some rows will be moved to another table which will leave gaps.

What I'm thinking now is to query the table, pull every third row, place that in column 1 and so forth, starting at the first entry.

EDIT 3: What I am looking for is to do a SQL query that grabs all of the information on every third row, starting on row 1, then I need to do another query of every third row, starting on row 2, then the same for row 3.

解决方案

use float:left; in your file

   http://beta.mybattlereport.com/styles/global.css

line 82
class container

EDIT :

first: those cards with numbers 1,2,3 they all have same class card normal so if you apply changes to one card it will aplly changes to all cards

solution : try using table instead of divs.

second : your cards are ordered like that

 card1 card2 card3  featured card , card1 card2 card3 featured card

solution : this can also be fixed by table and you order them 1 column will be for card1 2 column will be for card2 3column will be for card3 and then you can use other table under this for featured cards or a div.

EDIT2>

your problem is in the sql to get those cards generated by number of cards and featured you should order them first by featured and then by votes

try this

   $qry_questions = mysql_query("SELECT * FROM mbr_qa_questions ORDER BY q_featured ,q_votes DESC" /* LIMIT 0, 10"*/); 

the numbers 1 and 2 and 3 you are just giving numbers to each card . so i dont see any purpose from those numbers why you are making them.

这篇关于如何拆分mySQL信息以显示在HTML列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆