简体   繁体   中英

HOWTO: get data from MYSQL database and then use/show this data in a php page in a while/foreach?

I figured out how to get the data, how to use it in a while loop, but I am stuck on the last part: a loop in a loop.

I have the following code:

<div id="tab1" class="tab_content"> 
            <ul class="columns"> 
            <?php 
                $result = mysql_query("SELECT * FROM TabContent WHERE TabID = 1");
                while($row = mysql_fetch_array($result))
                  {
                  echo '<li><a href="#"><img src="images/layouts/'. $row['LayoutName'] .'.png" alt="" /></a>
                            <div class="info"> 
                                <h2>'. $row['LayoutName'] .'</h2> 
                                <p>'. $row['LayoutTiles'] . 'Tiles, '. $row['LayoutLayers'] . 'Layers</p>
                            </div> 
                        </li>';
                  }
            ?>  
            </ul>
            <div class="clear"></div>
            <div class="bottom">
                <div class="clearfix">
                    <div class="lfloat"></div>
                    <div class="rfloat"><a class="hide" href="#" onclick="return false">Cancel</a></div>
                </div>
            </div>  
        </div> 

        <div id="tab2" class="tab_content"> 
        etc etc same as above

The problem I have is with the div with id="tab1" and the next one with id="tab2" etc. I can get an array of tabs using:

<?php 
                $result = mysql_query("SELECT * FROM Tabs");
                while($row = mysql_fetch_array($result))
                  {
                  echo 'tab'. $row['TabID'] .;
                  }
            ?>

Giving me: Tab1 Tab2 Tab3 etc. Now the hard part, how do I use these so I can make a loop using this to replace my html div ids?

Here's a couple of tutorials that might help you in your struggle:

http://www.freewebmasterhelp.com/tutorials/phpmysql

http://www.homeandlearn.co.uk/php/php.html

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