Solved

nesting do while loops php/mysql

Posted on 2010-08-27
5
510 Views
Last Modified: 2013-12-13
Hi,
I have a page that I want to show archive documents I want it so that I have dates in asending order and underneath the date I want the documents to be listed

so the page will have

2009
 1) document 1
 2) document 2

2008
 1) document 1
 2) document 2


 This is what I have at the moment. Dreamweaver wont allow me to nest the while loop

Is there an easier way of doing it?
 
mysql_select_db($database_db, $db);
$query_enable_login = "SELECT disable_login FROM season";
$enable_login = mysql_query($query_enable_login, $db) or die(mysql_error());
$row_enable_login = mysql_fetch_assoc($enable_login);
$totalRows_enable_login = mysql_num_rows($enable_login);

mysql_select_db($database_db, $db);
$query_archives = "SELECT `year` FROM archive ORDER BY `year` ASC";
$archives = mysql_query($query_archives, $db) or die(mysql_error());
$row_archives = mysql_fetch_assoc($archives);
$totalRows_archives = mysql_num_rows($archives);

  <h2><?php echo $row_archives['year']; ?></h2>
        <?php do { ?>
          <hr />
            <a href="archives/<?php echo $row_archive_item['file']; ?>"><?php echo $row_archive_item['archive_title']; ?></a>
            <hr />
          <?php } while ($row_archive_item = mysql_fetch_assoc($archive_item)); ?>
        </p>

Open in new window

0
Comment
Question by:cataleptic_state
5 Comments
 
LVL 10

Expert Comment

by:honestman31
ID: 33543411
is that ok to go with the for loop ? or foreach  loop ?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 33543921
Hi cataleptic_state,

There is an easier way to do the loop using this behavior:

http://www.tom-muck.com/extensions/help/simulatednestedregion/

1. Download and install it

2. Create your repeat region so that everything is inside the loop:


        <?php do { ?>
<h2><?php echo $row_archives['year']; ?></h2>
          <hr />
            <a href="archives/<?php echo $row_archive_item['file']; ?>"><?php echo $row_archive_item['archive_title']; ?></a>
            <hr />
          <?php } while ($row_archive_item = mysql_fetch_assoc($archive_item)); ?>
     
3. Highlight the element that should only repeat once:

<h2><?php echo $row_archives['year']; ?></h2>

4. Apply the Simulated Nested Repeat Region from the Server Behaviors Panel

5. Drink Beer.

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33544046
Looks to me like you're trying to use "$row_archive_item[]" before you have fetched it with "$row_archive_item = mysql_fetch_assoc($archive_item)".  In addition, "$archive_item" is not defined anywhere.
0
 
LVL 4

Author Comment

by:cataleptic_state
ID: 33546715
Hi DaveBaldwin,
The archive_items is another recordset, it is there, just forgot to add it to EE code window.

I will give those examples a try thank you all.
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33546801
This is what I did on a similar problem.  You only have to do one query instead of two.
<?php 
mysql_select_db($database_db, $db);
$query_archives = "SELECT * FROM archive ORDER BY `year` ASC";
$archives = mysql_query($query_archives, $db) or die(mysql_error());
$totalRows_archives = mysql_num_rows($archives);

$current = "";

while ($row_archives = mysql_fetch_array($archives)){
	if($current != $row['year']) {
		$current = $row['year'];
		echo "<h2>$current</h2><hr />";
		} ?>
	<a href="archives/<?php echo $row_archives['file']; ?>"><?php echo $row_archives['archive_title']; ?></a>
<?php 
	}
 ?>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 8 92
EditableGrid how to fetch rows from MySql in php 14 46
PHP function parameters defined 14 40
MySQL-Design Help 12 41
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question