tags:

views:

66

answers:

3

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 display 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>';
}
+3  A: 

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>';
}
BraedenP
A: 

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.

qid
I'm sorry, disregard this, I misread the question/code.
qid
WTF?! who did vote up this answer?!
DaNieL
A: 
// 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);
}       


//ADDED
$row = mysqli_fetch_assoc($dbc)
if(!$row) {
       echo "error"; 
       return;
}
//END ADDED



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


$ctr = 0;
//ADDED
do {
//END ADDED
        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>';
                }
        }
//ADDED
}while ($row = mysqli_fetch_assoc($dbc));
//END ADDED

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

didn't get to test this, but your problem stems from the fact that in your code, you didn't initialize $row before you called your //display only once code, so empty always returned true when you called it on $row['anything']. If you initialized $row before hand, and make sure $row actually got initialized (i.e. mysqli_fetch_assoc() returned something), then call your //Display only once. code, then change your loop to a do/while so it doesn't repeat the fetch, it should work.

Try it out

Adam Georgiou