简体   繁体   English

将MySQL数据格式化为PHP中的下一列的最简单方法

[英]Easiest way to Format MySQL Data to Next Column in PHP

I have my data being output to a span currently... this is how it looks: 我当前正在将数据输出到一个范围...这是它的外观:

作为跨度

在此处输入图片说明

Now, when i remove the span and place a div there i am given this output: 现在,当我删除跨度并放置一个div时,将得到以下输出:

在此处输入图片说明

This is desired, but I want to set a height to my page and have the data show up in as little as 3 columns. 这是理想的,但是我想在页面上设置一个高度并使数据显示在最少3列中。 How would I do this? 我该怎么做? I have searched everywhere online but can't seem to find anything that shows a solution. 我在网上到处搜索,但似乎找不到任何能解决问题的方法。

I did read that some use javascript for the format but i am still clueless on even this option. 我确实读过一些使用javascript的格式,但是即使这个选项我仍然一无所知。

My desired output would look like this: 我想要的输出看起来像这样:

在此处输入图片说明

If you know how many items you want in a column then you can seperate them out into individual divs and then float those divs to the left to get them to be next to each other. 如果知道一列中要包含多少个项目,则可以将其分离为各个div,然后将这些div浮动到左侧以使它们彼此相邻。

<div style='float:left'>
    //Items go here
</div>
<div style='float:left'>
    //Items go here
</div>

etc. If you figure out how many items your query returned, say using mysql_num_rows() and divide by 3 you can tell how many to put in each column. 等等。如果您弄清楚查询返回了多少项目,请说使用mysql_num_rows()并除以3,就可以知道每列中要放入多少项目。 Also be sure to clear the floats afterwards, so like this: 另外,请确保之后清除浮标,如下所示:

<div style="clear:both"></div>

Sometimes this is necessary as there will be random issues if this is not put there. 有时这是必要的,因为如果不将其放置在那里,将会出现随机问题。

What you are describing can be solved with styling only. 您所描述的内容只能通过样式解决。 You have several divs that must be displayed in columns. 您必须在列中显示几个div。 The easiest way is floating them to the left, and setting the width for 1/3 of the parent. 最简单的方法是将它们浮动到左侧,并将宽度设置为父级的1/3。 If you want 4 columns, set the with to 1/4 of the parent, and so on. 如果要4列,请将with设置为父级的1/4,依此类推。

<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>

Also as other answers mentioned, don't use duplicated ids. 另外,正如提到的其他答案一样,请勿使用重复的ID。 Always use classes. 始终使用类。 If you need to target each div individually, give it a unique id, such as "category_1", "category_2", and so on. 如果您需要分别定位每个div,请为其指定一个唯一的ID,例如“ category_1”,“ category_2”等。

This should work 这应该工作

<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
        <?php if($count == 0){ echo '<td>';} ?>
        <span>
                <a href="<?php echo $key; ?>" id="category"><?php echo $key; ?></a>
        </span>
        <?php if($total == $idxcount): ?>
                </td>
        <?php elseif($count == $limit): ?>
                </td>
        <?php $count=0; else: $count++; ?>

        <?php  endif; $idxcount++; ?>

<?php endwhile; ?>
</tr></table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM