Craig Jones
Craig Jones

Reputation: 15

PHP While Loop and DIV's

I have an issue with my while loop that I'm using. I'm trying to display several modules on my webpage, each of them are stored within my database.

I run the following mysql query.

$modules = mysql_query("SELECT * FROM category WHERE active = '1' ORDER BY ordering ASC"); and then while($allmodules = mysql_fetch_array($modules)) {

This then returns the relevant data that I'm looking for, all that works fine.

I am able to display these 'modules' on the webpage easily, but I'm trying to store them in other DIVs which will then create rows. So I can have 3 rows of modules on the webpage to help positioning and so on.

I'm using <div id="module-row"> 3 times over. When I'm not using PHP I can get it to work. I want to insert the first module I fetch from the while statement in to the first 'module-row' DIV and the second one in the second 'module-row' DIV and the third module, in the third DIV.

That way I'll have one module in each row. I then want to continue it, so the fourth module will then be placed in the first 'module-row' DIV and so on.

Upvotes: 0

Views: 1979

Answers (3)

gadinkid
gadinkid

Reputation: 1

If your table has an id and data column, it'd look something like this:

while($allmodules = mysql_fetch_assoc($modules)) {
    echo '<div class="module-row'.$allmodules['id'].'">'.$allmodules['data'].'</div>';
}

the answer on above is true but i suggest use php query, use require_once function before you have to include insert_db.php belong to [head] so become just use

echo [div class="require_one $database_name"] *please change symbol [div] be <...> ... is div tag

note : remeber if you use sql query to your page it can risk for security system.

Upvotes: 0

user1454661
user1454661

Reputation:

If your table has an id and data column, it'd look something like this:

while($allmodules = mysql_fetch_assoc($modules)) {
    echo '<div class="module-row'.$allmodules['id'].'">'.$allmodules['data'].'</div>';
}

Upvotes: 1

jackwanders
jackwanders

Reputation: 16020

Your question is a little fuzzy, but it sounds like you're having trouble inserting the fetched data into the appropriate DIV tags.

ID attributes are assumed to be unique, i.e. once an ID is used, it is not used again on that page. Try replacing the ID attribute with a class attribute, like so <div class="module-row">, then access the divs by class name instead of ID.

Upvotes: 2

Related Questions