简体   繁体   中英

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:

在此处输入图片说明

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. 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.

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 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. 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. The easiest way is floating them to the left, and setting the width for 1/3 of the parent. If you want 4 columns, set the with to 1/4 of the parent, and so on.

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

Also as other answers mentioned, don't use duplicated ids. 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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