开发者

How to display a piece of code only once when the rest is looped using PHP and MySQL?

开发者 https://www.devze.com 2022-12-10 14:19 出处:网络
I\'m trying to display the following code only once while the rest of the code loops threw. How should my code look like and where should I put it. As of right now the bottom code will not disp开发者_

I'm trying to display the following code only once while the rest of the code loops threw. How should my code look like and where should I put it. As of right now the bottom code will not disp开发者_StackOverflow中文版lay but if I put it in the loop it will display multiple times (not what I want).

    //Display only once.
    if (!empty($row['category']) && !empty($row['url'])){
            echo '<h2>List of Links</h2>';
    }

Here is the full code.

// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*, categories.*, users_categories.* FROM users_categories INNER JOIN users ON users_categories.user_id = users.user_id INNER JOIN categories ON users_categories.category_id = categories.id WHERE users_categories.user_id=3");

if (!$dbc) {
    // There was an error...do something about it here...
    print mysqli_error($mysqli);
}   
    //Display only once.
    if (!empty($row['category']) && !empty($row['url'])){
            echo '<h2>List of Links</h2>';
    }

$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
    if (!empty($row['category']) && !empty($row['url'])) {
        if ($ctr%3 == 0) {
            echo '<div>';
        }
        $ctr ++;
        echo '<p"><a href="' . $row['url'] . '" title="' . $row['category'] . ' Category Link">' . $row['category'] . '</a></p>';
        if ($ctr%3 == 0) { 
            echo '</div>';
        }
    }

}
if ($ctr%3 != 0) { 
  echo '</div>';
}


Perhaps you're looking for:

if (mysql_num_rows($dbc) > 0){
    echo '<h2>List of Links</h2>';
}

By replacing the code above with your current if statement, the header will be displayed when rows are returned in the query, and not when the query returns nothing.

Thus, the full code would be:

// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*, categories.*, users_categories.* FROM users_categories INNER JOIN users ON users_categories.user_id = users.user_id INNER JOIN categories ON users_categories.category_id = categories.id WHERE users_categories.user_id=3");

if (!$dbc) {
        // There was an error...do something about it here...
        print mysqli_error($mysqli);
}       
        //Display only once.
        if (mysql_num_rows($dbc) > 0){
            echo '<h2>List of Links</h2>';
        }

$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
        if (!empty($row['category']) && !empty($row['url'])) {
                if ($ctr%3 == 0) {
                        echo '<div>';
                }
                $ctr ++;
                echo '<p"><a href="' . $row['url'] . '" title="' . $row['category'] . ' Category Link">' . $row['category'] . '</a></p>';
                if ($ctr%3 == 0) { 
                        echo '</div>';
                }
        }

}
if ($ctr%3 != 0) { 
  echo '</div>';
}


The standard way of doing this is to add a boolean variable, initialize it to one value, check it before running that code, and flip it when it runs. Not very fancy, but it works.

// Query member data from the database and ready it for display
$displayHeading = true;
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*, categories.*, users_categories.* FROM users_categories INNER JOIN users ON users_categories.user_id = users.user_id INNER JOIN categories ON users_categories.category_id = categories.id WHERE users_categories.user_id=3");

if (!$dbc) {
        // There was an error...do something about it here...
        print mysqli_error($mysqli);
}       
        //Display only once.
        if (!empty($row['category']) && !empty($row['url']) && $displayHeading){
                 $displayHeading = false;
                 echo '<h2>List of Links</h2>';
        }

$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
        if (!empty($row['category']) && !empty($row['url'])) {
                if ($ctr%3 == 0) {
                        echo '<div>';
                }
                $ctr ++;
                echo '<p"><a href="' . $row['url'] . '" title="' . $row['category'] . ' Category Link">' . $row['category'] . '</a></p>';
                if ($ctr%3 == 0) { 
                        echo '</div>';
                }
        }

}
if ($ctr%3 != 0) { 
  echo '</div>';
}

Note that it's been a little while since I've written PHP, so I assume I have the right literals for true and false, but maybe they have to be in all caps or something like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消