Jump to content

hitman6003

Members
  • Posts

    1,807
  • Joined

  • Last visited

Posts posted by hitman6003

  1. Warning ahead of time...I haven't tested any code........

     

    $pet_names = array('bob', 'sue', 'larry', 'bill');
    
    // find pets who are in the database as adopted and in your array of names
    $result = mysql_query('SELECT * FROM pets WHERE name IN("' . implode("\",", $pet_names) . '")');
    
    // put the adopted pets into their own array
    while ($row = mysql_fetch_assoc($result)) {
      $adopted[] = $row['name'];
    }
    
    // determine the unadopted pets from the full list using array_diff
    $not_adopted = array_diff($pet_names, $adopted);

  2. arguably the simplest way....

     

    $total = 100;
    $values = array();
    for ($i = 1; $i <= 4; $i++) {
      $values[] = rand(1, 20);
    }
    
    $values[] = $total - array_sum($values);
    
    echo "<pre>Values are:\n" . print_r($values, true) . "</pre>";

     

    No excessive amount of looping involving a where loop or anything which could iterate thousands of times before it hits the correct numbers.  Only caveat being the last number will be very different....ranging from 20 to 96 versus 1 to 20 for the others.  A little more logic could solve that though....

  3. JOIN the tables and use a "GROUP BY" statement...

     

    SELECT fid, forum, desc, COUNT(fid)
    FROM forum_titles ft
      LEFT JOIN forum_questions fq ON fq.fid = ft.fid
    GROUP BY fid

  4. You're missing basic html...

     

    This

     echo "<input type="text" name="ausername" maxlength="12" size=30 background-color=#FA1B00 value="<?=$_POST['ausername']?>";

     

    should be

     echo "<input type="text" name="ausername" maxlength="12" size=30 style="background-color: #FA1B00;" value="<?=$_POST['ausername']?>";

     

     

  5. Is there some advanced php template producer?

     

    You could use any one of the many frameworks.  I'm most familiar with Zend (http://framework.zend.com) and I know that their form class(es) can do what you want.

     

    They use an especially nice decorator format that makes things easy to change.  Padriac Brady wrote a nice tutorial about ZF a while back: http://blog.astrumfutura.com/archives/367-Example-Zend-Framework-Blog-Application-Tutorial-Parts-1-8-Revisited.html

  6. There are two reference points that you can use...

     

    The first is the "document root".  This is the directory that is the base for all the webpages on the server.  For example, if you install the apache RPM on RHEL/CentOS it defaults to /var/www/html.  When you have a link in your html and you put "/" in the link, for example:

     

    <a href="/some/random/page.php">test</a>

     

    The browser will look to the document root.

     

    The other reference point is the file system.  When you include a file, using php's include or require functions, it references the file system.  So the path you use references the "root" of the file system.

     

    This means that doing

     

    include("/web/includes/somefile.php");

     

    will look for a file outside of the tree of documents that the web server references.  In the above example for a link, the actual referenced file would be at a location similar to "/var/www/html/some/random/page.php".

     

    This is important when you are including documents in your php scripts because sometimes you want to put a file to be included outside of the document tree that the webserver can access.  For example, if you have a php file that contains your MySQL credentials, you don't want just anyone to be able to use their browser and point to "/some/random/mysql_credentials.php" to get the connection information.

     

    For the majority of files though, they are simply code that contains nothing sensitive.  Using the $_SERVER['DOCUMENT_ROOT'] variable is a simple way of providing the absolute path for including a file, while that path still remaining flexible (because not everyone has the same document root).

  7. Cookies store data only on the client side, every page visit the data is sent to the server. 

     

    Sessions store data on the server.  A client cookie is used to identify which user is accessing the page in order to determine which session data file to read.  The actual session data is not stored in a cookie, only the session identifier.

     

    However, with sessions the cookie is optional.  When the cookie behavior is turned off the session identifier will be passed to the server in the url (you most often see this in search engine results, when the url has something like "sessionid=......" in it)

  8. Use a for loop...

     

    <?php
    for ($i = 0; $i < 10; $i ++) {
      $entry = $sxml_twitter->channel->item[$i];
      $news_title = $entry->description;		
      $date = $entry->pubDate;
    
      echo "	
        <ul>						
          <li class='tweet'>$news_title</li>
          <li class='date_twitter'>$date</li>
        </ul>";
    }
    ?>

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.