SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed desperate

    Hello everyone,

    Right basicaly im coding a site, and i have a database with 38 cities in it. Now what i am wanting to do is take them 38 cities from the database , then im wanting to output them into a table but the table needs 4 columes and obviously the cities need to be divided into them 4 columes ... soo i will have something like this :

    city 1 city 4 city 7
    city 2 city 5 city 8
    city 3 city 6 city 9

    They will also need to be sorted alphabeticaly .... can someone shed some light on this for me ..... i thought maybe something like below :

    PHP Code:

    <?php

    // Database connection info here

    $cities mysql_query("SELECT * FROM cities ORDER BY city_name") or die ("Failed.".mysql_error());

    $cities explode(""$cities);

    ?>
    then echo the $cities[0] variable into the diffrent columes. But then this wont work as between the "" in the explode code .. it needs something there.

    Someone please plese help me

    Thanks in advance to anyone that helps.

    Regards
    Chris

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $cities is what is called a resultset... it needs further work.

    Try this first:
    PHP Code:
      while($row mysql_fetch_row($cities))
      {
        print 
    '<pre>';
        
    print_r($row);
        print 
    '</pre>';
      } 
    A good link for all kinds of php mysql functions:

    http://us2.php.net/manual/en/functio...-fetch-row.php
    John

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heres a function i keep in my library for putting the values of an array into a table.


    PHP Code:
    <?php
    function make_table($array$columns)
    {
        
    $output "<table>\n";

        for (
    $i=0$max=count($array); $i<$max$i++)
        {
            if ((
    $i $columns) == 0)
            { 
    // % is the modulous operator, NOT percentage
                
    $output .= " <tr>\n";
            }

            
    $output .= "  <td>{$array[$i]}</td>\n";

            if ((
    $i $columns) == ($columns 1))
            {
                
    $output .= " </tr>\n";
            }

        }

        
    $remainder $columns - ($i $columns);

        if (
    $remainder 0)
        {
            while (
    $remainder--)
            {
                
    $output .= "  <td>&nbsp;</td>\n";
            }

            
    $output .= " </tr>\n";
        }

        
    $output .= "</table>\n";

        return 
    $output;
    }


    ?>

    PHP Code:
    <?php
    $result 
    mysql_query("SELECT * FROM cities ORDER BY city_name") or die ("Failed.".mysql_error()); 

    $cities = array();
    while (
    $row mysql_fetch_assoc($result)) {
        
    $cities[] = $row['city_name'];
    }


    echo 
    make_table($cities4);
    ?>

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    heres a function i keep in my library for putting the values of an array into a table.


    PHP Code:
    <?php
    function make_table($array$columns)
    {
        
    $output "<table>\n";

        for (
    $i=0$max=count($array); $i<$max$i++)
        {
            if ((
    $i $columns) == 0)
            { 
    // % is the modulous operator, NOT percentage
                
    $output .= " <tr>\n";
            }

            
    $output .= "  <td>{$array[$i]}</td>\n";

            if ((
    $i $columns) == ($columns 1))
            {
                
    $output .= " </tr>\n";
            }

        }

        
    $remainder $columns - ($i $columns);

        if (
    $remainder 0)
        {
            while (
    $remainder--)
            {
                
    $output .= "  <td>&nbsp;</td>\n";
            }

            
    $output .= " </tr>\n";
        }

        
    $output .= "</table>\n";

        return 
    $output;
    }


    ?>

    PHP Code:
    <?php
    $result 
    mysql_query("SELECT * FROM cities ORDER BY city_name") or die ("Failed.".mysql_error()); 

    $cities = array();
    while (
    $row mysql_fetch_assoc($result)) {
        
    $cities[] = $row['city_name'];
    }


    echo 
    make_table($cities4);
    ?>

    wow thanks for that ... im very good in php ... but have never come across this request befor .... strange lol.

    Thankyou very much for the great help.


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
  •