Forum Moderators: coopster

Message Too Old, No Replies

Slight issue with loop

         

doodlebee

2:18 pm on May 10, 2007 (gmt 0)

10+ Year Member



I didn't know how else to describe this, sorry for being so vague.

I've written a piece of code to extract some information from a database and display it on a site. Part of the function of this is to number the extracted data - similar to an ordered list (but I cannot use an ordered list).

At one point, I need to look for a certain piece of data and have it appended. I've got all of this working fine *except* for the numbering: the appended bit has the same number as the item previous to it. I've tried many methods of changing this, but I can't seem to find the right bit to make the number on the appended part flow with the rest of the data.

As an example, the data displays like so:

1. link 1
2. link 2
2. link 3
4. link 4
5. link 5

The "2" is the one that is looked for (in the below code, you'll note that it looks for "$pausetitle", which is currently Link 2) Link 3 is what is appended - but you'll note that, instead of "3." it's displaying a "2.".

I find it peculiar that Link 4 recognizes that third one - even though it's numbered incorrectly, and continues on like nothing has happened, keeping the correct number format.

Anyway, if someone could take a peek at this code and help me figure out how to fix it so that "Link 3" has the number "3." next to it, I would really appreciate it.


<?php
function custom_links() {

$default_sort = 'ID';
$allowed_order = array ('ID', 'page_id', 'post_title');

if (!isset ($_GET['order']) ¦¦
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}

$query = "SELECT * FROM wp_posts, wp_postmeta where meta_key = 'main_nav_description' AND post_id = wp_posts.ID ORDER by $order" or die(mysql_error());
$result = mysql_query ($query);

$currenttitle = "";
$pausetitle = "What we do";
$line_num = 1;

while ($row = mysql_fetch_assoc($result)) {

if ($row['post_title']!= $currenttitle) {

// test line, to be sure everything is getting pulled out correctly.
//echo $line_num . "\n" . $row['post_title'] . "\n" . $row['meta_value'] . "<br />";

echo "<li class=\"clear\"><a href=\"" . $row['post_name'] . "\" alt=\"" . $row['post_title'] . "\" >" . "\n";
echo "<span class=\"sidebox\"><strong>" . $line_num . "</strong></span>" . "\n";
echo "<span class=\"linkname\"><span class=\"sidetitle\">" . $row['post_title'] . "</span>" . "\n";
echo "<span class=\"sidedesc\">" . $row['meta_value'] . "</span></span></a>" . "\n\n";

}

if ($row['post_title'] == $pausetitle) {
echo "<li class=\"clear\"><a href=\"#\" alt=\"Industry Articles\" >" . "\n";
echo "<span class=\"sidebox\"><strong>" . $line_num . "</strong></span>" . "\n";
echo "<span class=\"linkname\"><span class=\"sidetitle\">Industry Articles</span>" . "\n";
echo "<span class=\"sidedesc\">Useful Information</span></span></a>" . "\n\n";
}

$currenttitle = $row['post_title'];
$line_num++;

}

}

?>

Thank you!

Scally_Ally

4:03 pm on May 10, 2007 (gmt 0)

10+ Year Member



It looks like both of your if staements are being satisfied, that is why the $line_num isn't incrementing along correctly.

so on that second row
($row['post_title']!= $currenttitle) is satisfied
and also
($row['post_title'] == $pausetitle) is satisfied

Then on the next loop both of the if statements might not be satisfied, that is why $line_num jumps back to where it should be - because it increments along without actually displaying anything..

At least that is what it looks like to me.

Ally

doodlebee

4:15 pm on May 10, 2007 (gmt 0)

10+ Year Member



Thank Ally! What you said kinda cleared it up for me. I added an "$line_num++;" after the " if ($row['post_title'] == $pausetitle) {" and it fixed the issue. It's now numbering correctly.

Thank you!