SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    PHP Code:
    <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?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?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>";

    }

    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dan,

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

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What do you mean?

    PHP Code:
    <?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>";

        }

    }

    ?>

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For some reason I thought that I had to echo all of that. That works perfectly! You definitely know your PHP and MySQL!!!

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

    PHP Code:
    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>";


  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •