Using list only once?

I am trying to only show the title one time but want it to be part of the MySQL code so that the title only shows one time. Is there a way this can be done?


<li>Other
      <ul>
<?php
include('config.php');
$result = mysql_query("SELECT * FROM users WHERE reseller_full='da' ORDER BY company ASC") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
echo "<li><a href=/client/" . nl2br( $row['folder']) . "/index.php>" . nl2br( $row['company']) . "</a></li>";
}
?>
      </ul>
    </li>

In other words I only want <ul>Other and <li> to show once and if there are no results in other then nothing would display. Anyone know how to do this?


<?php

include('config.php'); 
$result = mysql_query("SELECT * FROM users WHERE reseller_full='da' ORDER BY company ASC") or trigger_error(mysql_error()); 

//Check that the result set contains at least one row
if (mysql_num_rows($result) > 0) {

  echo "<li>Other<ul>";
 
  while ($row = mysql_fetch_array($result)) {
    echo "<li><a href=/client/" . nl2br($row['folder']) . "/index.php>" . nl2br($row['company']) . "</a></li>"; 
  }

  echo "</ul></li>";

}

?>

Hi Dan,

That works perfectly! Thank you very much for your help!!!

Another question…

What if I wanted to make this code only available if someone is an admin? For example I am going to make a new variable where admin is equal to 1 if they are admin and 0 if they are not. But I cant wrap an if statement around the code because of all of the " and ’ already being used. Then I tried using both EOD and EOT wrapped but then the PHP code doesnt work. Does someone know a solution?

What do you mean?

<?php

if ($admin == 1) {

    include('config.php'); 
    $result = mysql_query("SELECT * FROM users WHERE reseller_full='da' ORDER BY company ASC") or trigger_error(mysql_error()); 

    //Check that the result set contains at least one row
    if (mysql_num_rows($result) > 0) {

        echo "<li>Other<ul>";

        while ($row = mysql_fetch_array($result)) {
            echo "<li><a href=/client/" . nl2br($row['folder']) . "/index.php>" . nl2br($row['company']) . "</a></li>"; 
        }

        echo "</ul></li>";

    }

}

?>

For some reason I thought that I had to echo all of that. That works perfectly! You definitely know your PHP and MySQL!!!

Alright Dan, let me test your skills here. Lets say that I have a drop down list that contains a lot of duplicate values. How would I only get them to display once from the code below?


include('config.php');
$result = mysql_query("SELECT * FROM users ORDER BY reseller_full ASC") or trigger_error(mysql_error()); 
while($row = mysql_fetch_array($result)){ 
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
echo "<option value='" . nl2br($row['reseller_full']) . "'>" . nl2br($row['reseller_full']) . "</option>";
}

SELECT DISTINCT reseller_full

instead of SELECT *

Always ask the database for only the exact information you need, otherwise you’re increasing query processing time, increasing network time sending the data from MySQL to PHP, increasing processing time while PHP stuffs that larger result set in memory, increasing execution time while you loop over results you don’t need, and increasing memory wasted with data you aren’t using.