Results 1 to 5 of 5

Thread: help with arrays -- how do they work and syntax.

  1. #1
    Join Date
    Mar 2007
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with arrays -- how do they work and syntax.

    ok, i made a post last week about sorting on 2 varibles. i guess my code was too ugly to get a fix for so i went back to the drawing board. i think i know the concept i need, but do not know much coding.

    i have a list of members. i need to sort them on date attending a class, and then be able to filter out of that list only the ones that are approved to come. would i need to use an array here to store the date? currently i use a while statment like this

    PHP Code:
     while($u mysql_fetch_array($users)) { 
    echo 
    ' <table><tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr></table>';

    This gets me my list. would i need to have those results go to an array and then do another while that pulled from that array? what is the process for that. i know i am asking alot, sorry i am new to this. i appreciate the help
    Last edited by mdcloud; 04-30-2007 at 04:46 PM.

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mdcloud View Post
    i have a list of members. i need to sort them on date attending a class, and then be able to filter out of that list only the ones that are approved to come. would i need to use an array here to store the date?
    That depends on what information is in the database. In principle, the DBMS itself can do all of this and return a sorted list of approved members.

    PHP Code:
     while($u mysql_fetch_array($users)) { 
    echo 
    ' <table><tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr></table>';

    A new table for every entry?

    Mike

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    fix for that "new table every entry" is

    Code:
    echo "<table>";
    while($u = mysql_fetch_array($users)) { 
      echo '<tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr>';
    }  
    echo "</table>";
    f
    where you wrap the whole thing in a table.

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by boogyman View Post
    fix for that "new table every entry" is
    It may not need fixing; the posted code could be a simplification and a new table really is justified. That's why I just queried the code.

    Code:
    echo "<table>";
    while($u = mysql_fetch_array($users)) { 
      echo '<tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr>';
    }  
    echo "</table>";
    As the query may also return and empty result set, it would be a good idea to check that there are actually results to write before including table start- and end-tags:

    PHP Code:
    <?php
    /* ... */
    if (mysql_num_rows($users) > 0) {
    ?>
    <table>
    <?php
        
    while (($row mysql_fetch_array($users))) {
    ?>
        <tr><td><?php echo $row['FName']; ?></td></tr>
        <tr><td><?php echo $row['LName']; ?></td></tr>
    <?php
        
    }
    ?>
    </table>
    <?php
    }
    /* ... */
    ?>
    Mike

  5. #5
    Join Date
    Nov 2006
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I know similar code has been posted ahead of me. But I have used this code for many projects, and it has not failed yet. So I will post if for anyone to use. Its pretty simple.

    PHP Code:
    $server "[SERVER]"//url for the mysql server
    $DBuser "[USERNAME]"//database username
    $DBpass "[PASSWORD]"//database password
    $DB "[DATABASE NAME]"//actual database name
    $table "[TABLE NAME]"//database table name

    mysql_connect($server,$DBuser,$DBpass); //connect to server
    mysql_select_db($DB); //select database
    $info mysql_query("SELECT * FROM ".$table.""); 

    //change the row names to fit your needs/wants
    echo '<table>';
    while (
    $qry mysql_fetch_array($info)) {
    echo 
    '<tr><td>'.$qry[Lname].'</td><td>'.$qry[Fname].'</td></tr>; 
    }
    echo '
    </table>'; 
    It is important to have the periods "." after a ' or " and right before a ' or "
    ie.
    Code:
    echo '<tr><td>'.$qry[Lname].'</td><td>'.$qry[Fname].'</td></tr>;

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
  •