SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Enthusiast Davve's Avatar
    Join Date
    Jan 2002
    Location
    Norrkoping, SWEDEN
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Divine way of listing?

    I'm a PHP newbie. How can you tell? Well, I have this easy (stupid?) question for you...

    I have my MySQL database with a table with four fields. ID, field1, field2 and field3. Now I want to just list these fields in a nice HTML table. I'm trying to come up with a good while, do..while or for to do this. But my own tries generate so much code. There's got to be one divine way of doing this.

    What's the divine way?

    I could of course use my own code, but I feel that because I'm still a newbie it would be better to look at this super code and use it!




    Edit: my "bad" code
    Which, by the way, isn't working. My for loop works, but my echoes show only: Resource id #3, Resource id #4 and Resource id #3. Why is that?
    PHP Code:
    <table border=0 cellspacing=2 cellpadding=2 width=600>
        <tr>
        <td align=center width=200>
            table_variable        
            </td>
                <td align=center width=200>
                table_innehall
                </td>
                    <td align=center width=200>
                    table_innehall_flash
                    </td>
        </tr>

    <?php
    $i 
    1;

    $table_variabel mysql_query("SELECT variabel FROM text WHERE ID = '$i'");
    $table_innehall mysql_query("SELECT innehall FROM text WHERE ID = '$i'");
    $table_innehall_flash mysql_query("SELECT innehall_flash FROM text WHERE ID = '$i'");

    for (
    $i 1$i <= 3$i++) {

    ?>
        <tr>
            <td align=center>
            <?php echo $table_variabel ?>            
            </td>
                <td align=center>
                <?php echo $table_innehall ?>            
                </td>
                    <td align=center>
                    <?php echo $table_innehall_flash ?>            
                    </td>
        </tr>


    <?php

    }

    ?>

    </table>
    Last edited by Davve; Jun 19, 2002 at 01:47.
    David Andersson Davve
    -MSc in Media Technology and Engineering:
    Working at AdLibris

  2. #2
    SitePoint Zealot .[:] : paVel : [:].'s Avatar
    Join Date
    May 2002
    Location
    Australia
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey

    PHP Code:
    <?
    $db
    =mysql_connect("HOST""USERNAME""PASSWORD");
    if (!
    $db)
    {
       echo 
    "Error: Could not connect to database!"
       
    exit;
    }
    mysql_select_db("YOU_DB_NAME");
    $query="select * from TABLE_NAME ....condisions...";
    $result=mysql_query($query);
    $number_of_results=mysql_num_rows($result);

    echo 
    "<p>$number_of_results number of records found</p>";

    for (
    $i=0$i<$number_of_results$i++)
    {
    $row=mysql_fetch_array($result);
    echo 
    "<p>ID</p>";
    echo 
    "<p>field1</p>";
    echo 
    "<p>field2</p>";
    echo 
    "<p>field3</p>";
    }
    ?>
    I think that helps!

    U can find more stuff on:
    www.phpbuilder.com
    http://www.devarticles.com


  3. #3
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by .[:] : paVel : [:].
    Hey

    PHP Code:
    <?
    $db
    =mysql_connect("HOST""USERNAME""PASSWORD");
    if (!
    $db)
    {
       echo 
    "Error: Could not connect to database!"
       
    exit;
    }
    mysql_select_db("YOU_DB_NAME");
    $query="select * from TABLE_NAME ....condisions...";
    $result=mysql_query($query);
    $number_of_results=mysql_num_rows($result);

    echo 
    "<p>$number_of_results number of records found</p>";

    for (
    $i=0$i<$number_of_results$i++)
    {
    $row=mysql_fetch_array($result);
    echo 
    "<p>$row['ID']</p>";
    echo 
    "<p>$row['field1']</p>";
    echo 
    "<p>$row['field2']</p>";
    echo 
    "<p>$row['field3']</p>";
    }
    ?>
    I think that helps!

    U can find more stuff on:
    www.phpbuilder.com
    http://www.devarticles.com

    Just a minor correction with the field values.
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM Email NEW! Carrier Pigeon

  4. #4
    SitePoint Enthusiast Davve's Avatar
    Join Date
    Jan 2002
    Location
    Norrkoping, SWEDEN
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    I've changed to my values and done a bit of stowing:
    PHP Code:
    <?php
    mysql_select_db
    ("davve2");
    $query="select * from text";
    $result=mysql_query($query);
    $number_of_results=mysql_num_rows($result);

    echo 
    "<p>$number_of_results number of records found</p>";

    for (
    $i=0$i<$number_of_results$i++)
    {
    $row=mysql_fetch_array($result); ?>

    <p><?php echo $row['variabel'];?></p>
    <p><?php echo $row['innehall'];?></p>
    <p><?php echo $row['innehall_flash'];?></p>
    <?php }
    ?>
    David Andersson Davve
    -MSc in Media Technology and Engineering:
    Working at AdLibris

  5. #5
    SitePoint Enthusiast Davve's Avatar
    Join Date
    Jan 2002
    Location
    Norrkoping, SWEDEN
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would you please care to do some explaining of what it is that's really happening?

    mysql_fetch_array - It takes each column and place it in each entry in an array, yes?

    But I don't get how it "changes row". I mean, sure, there is an for loop but I don't understand how it changes row.
    David Andersson Davve
    -MSc in Media Technology and Engineering:
    Working at AdLibris

  6. #6
    SitePoint Zealot Klav's Avatar
    Join Date
    Mar 2002
    Location
    Huddersfield, UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_fetch_array() places each row into an associative array and then, if you use a for/while loop, it loops through the array one by one until it reaches the end. It's just a nice feature of PHP.

  7. #7
    SitePoint Enthusiast Davve's Avatar
    Join Date
    Jan 2002
    Location
    Norrkoping, SWEDEN
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I understand parts, but not all.


    What I don't understand is what it really is that tells the mysql_fetch_array() to go to the next row! Take a look at my script. The only counter is $i. But mysql_fetch_array() doesn't use $i.

    The script works and I find it strange. My logic tells me that it shouldn't do like this:

    row1 - field1 || row1 - field2 || row1 - field3
    row2 - field1 || row2 - field2 || row2 - field3


    But rather this:

    row1 - field1 || row1 - field2 || row1 - field3
    row1 - field1 || row1 - field2 || row1 - field3


    I want to know what it is that causes the mysql_fetch_array() to understand that it should jump to the next row in each loop. I can't see what it is that does this. Do I make sense?

    Even though the script works fine, I still want to know why it works!
    David Andersson Davve
    -MSc in Media Technology and Engineering:
    Working at AdLibris

  8. #8
    SitePoint Zealot Klav's Avatar
    Join Date
    Mar 2002
    Location
    Huddersfield, UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's simple. The for loop loops through the array until it reaches the number specified in $amount_of_results. $amount_of_results is determined by the amount of rows your select query returned.

    The pointer in mysql_fetch_array() is moved forward one row for everytime it is iterated and, therefore, displays the data a row at a time. Get it?

  9. #9
    SitePoint Enthusiast Davve's Avatar
    Join Date
    Jan 2002
    Location
    Norrkoping, SWEDEN
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Klav
    The pointer in mysql_fetch_array() is moved forward one row for everytime it is iterated
    AHH! I checked the PHP documentation, which is really great btw, but I didn't get that.

    Thanks! I'm really grateful!
    David Andersson Davve
    -MSc in Media Technology and Engineering:
    Working at AdLibris

  10. #10
    SitePoint Zealot Klav's Avatar
    Join Date
    Mar 2002
    Location
    Huddersfield, UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem.


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
  •