Results 1 to 10 of 10

Thread: Gridview in PHP

  1. #1

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Gridview in PHP

    Hi,
    How to populate a gridview from database? Is there any inbuilt functions? I used this coding, but i dont know how to populate headers to enable sorting.
    Code:
    while($userdata=mysql_fetch_array($result,MYSQL_ASSOC))
    		{
    			$userid = $userdata['id'];
    			$username = $userdata['username'];
    			$active = $userdata['active'];
    			if($active == 0)
    			{$act="checked='checked'";}
    			else
    			{$act="";}
    		?>
    			<table border="0">
    				<td>
    					<tr>
    						<td width="30px"><?PHP echo($userid); ?></td>
    						<td width="50px"><?PHP echo($username); ?></td>
    						<td width="3px"><input type="checkbox" <?PHP echo($act); ?> /></td>
    					</tr>	
    				</td>
    			</table>
    		<?PHP	
    		}

  2. #2
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Gridview in PHP

    what is it that you don't get, could you explain it a bit more? like don't you know how to add the <table> so it doesn't repeat itself multiple times or?

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Gridview in PHP

    PHP is not ASP; ASP.NET is not anywhere close to PHP. the .NET framework allows for tons of little shortcuts -- that's what a framework does. if you were to try doing the things you're trying to do with just ASP, you would be doing just as much work. there is no such thing as a "GridView" in PHP, and if you wish to have table headings that allow you to sort your data, you'll have to implement that all yourself. a table heading is just another HTML tag and adding it won't automatically create a sorting system for you.

    anyway, let's look at the structure of a table:
    HTML Code:
    <table>
      <!-- headings -->
      <tr>
        <th>Field 1</th>
        <th>Field 2</th>
        <th>Field 3</th>
      </tr>
      <!-- /headings -->
    
      <!-- data -->
      <tr>
        <td>Data 1.1</td>
        <td>Data 1.2</td>
        <td>Data 1.3</td>
      </tr>
      <tr>
        <td>Data 2.1</td>
        <td>Data 2.2</td>
        <td>Data 2.3</td>
      </tr>
      <!-- ... -->
      <!-- /data -->
    </table>
    what can we get from this? the only part of this table that needs to be repeated is the part within the "data" block; one problem you're having is that you're A: re-creating the table every time you return data from your loop (meaning, define the table first, then you start looping through when you're trying to display your data), and you B: aren't even using table headings. in my example above, the <th> tags are for headings.

    however, even if you get as far as fixing the two problems I've illustrated above, you've got to come up with the logic to be able to actually allow a sort on clicking one of the headings. luckily, I can give you all of that logic in plain text.

    you'll need to enclose each heading in an anchor tag (<a>) so that when you click on it, it will link back to itself but with a query string (possibly named 'sort') defining which field you're sorting by. in my example, I might do something like:
    HTML Code:
      <!-- headings -->
      <tr>
        <th><a href="?sort=field1">Field 1</a></th>
        <th><a href="?sort=field2">Field 2</a></th>
        <th><a href="?sort=field3">Field 3</a></th>
      </tr>
      <!-- /headings -->
    then, when you're creating your SQL query, you need to check if this query string is set and then append the SQL query to add an "ORDER BY" clause. the syntax for a query with such a clause might look something like:
    Code:
    SELECT * FROM table WHERE field1='value1' ORDER BY field2 ASC
    of course, looking at the above query, you could also add some logic into your table heading definitions to "toggle" between ascending sorting (ASC) and descending sorting (DESC) in your SQL query.

    ask questions if you need help!

  4. #4

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Gridview in PHP

    add the <table> so it doesn't repeat itself multiple times or?
    A: re-creating the table every time you return data from your loop (meaning, define the table first, then you start looping through when you're trying to display your data),
    ya thankyou, i want those columns to be displayed as such in the coding. With the repeating table only i am getting 3 columns are in a different row and again with the new data, it is again displaying as 3 columns in 3 different rows. Without table repeating structures i am getting all the columns printed in the same row.
    Is there any way to change this?

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Gridview in PHP

    ... yes, I said exactly how in my post. you're using invalid syntax for a table. you can't have a <td> without a <tr>. look at the way I've structured my table in my example. that's how a table should be structured. copy that.

  6. #6
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Gridview in PHP

    well since you run the table inside the "while" loop, it keeps adding <table> try adding the beginning of the table and the data you need of it before the loop, so the contents of the database will stay inside the loop and the end of the table under the while loop.

    something like this... please look at the code since i don't have all of it i can't say if it works or not, but you should really try to see if you can find a better way of doing it...

    and 2 other things:
    1, does the checkboxes really have checked=checked? i thought it was only to add checked in the checkbox and it was checked...
    2, i'm sure after <table> there starts <tr> not <td>, but i don't know (just a tip)

    PHP Code:
    <?php

    print '<table border="0">
           <tr><td>User ID</td><td>Username</td><td>Active</td></tr>'
    ;

    while(
    $userdata=mysql_fetch_array($result,MYSQL_ASSOC))
            {
                
    $userid $userdata['id'];
                
    $username $userdata['username'];
                
    $active $userdata['active'];
                if(
    $active == 0) {
                
    $act="checked='checked'";
                } else {
                
    $act="";
                }
        print 
    '<tr>
                <td width="30px">' 
    $userid '</td>
                <td width="50px">' 
    $username '</td>
                <td width="3px"><input type="checkbox"' 
    $act '/></td>
            </tr>'
    ;
            }

        print 
    '</table>';    
    ?>
    edit: more information

    for sorting i suggest you read what kows says,
    lets say field1 is where you have active in and field2 is some type of auto increasing "id"
    Code:
    SELECT * FROM tablename 
    WHERE field1='active' 
    ORDER BY field2 ASC
    now there is ASC and DESC to sort by, in your select statement (where you gather the information from your code) just add order by and you choose which type

    now you could copy this statement and add something around it that will allow you to change the sorting...
    like if you put a link in the row active(for example if this is what you want to sort by) then the link should be something like ?sorting=desc and ?sorting=asc

    so in this link you make
    PHP Code:
    <a href="<?php
    if ($sorting == "asc") {
    echo 
    '?sorting=desc';
    } elseif (
    $sorting == "desc") {
    echo 
    '?sorting=asc';
    } else {
    echo 
    '?sorting=desc';
    }
    ?>">Active</a>
    now to get the sorting just in the top of your code just have something like

    PHP Code:
    $sorting $_GET['sorting']; 
    ps: if you have an important code in the top of the document like session or header then put it under that...

    so in this way you could get the sorting link, now you need to actually make it sort like it says, just put an if statement around the select query where it says
    PHP Code:
    if ($sorting == "asc") {
    //make the query order by asc
    } else {
    //make the query order by desc

    this would be one way to do the sorting, but i suggest you should try the coding yourself because otherwise you won't learn much of it.
    Last edited by Justa Lol; Feb 9th, 2010 at 06:14 PM.

  7. #7
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Gridview in PHP

    1, does the checkboxes really have checked=checked? i thought it was only to add checked in the checkbox and it was checked...
    "checked" is an attribute, paired with a value. The proper way to do this is always 'attribute="value"', though HTML doctypes allow attribute minimization, which is what you see when it's just "checked." This is not allowed by XHTML, however.

  8. #8
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Gridview in PHP

    Quote Originally Posted by SambaNeko View Post
    "checked" is an attribute, paired with a value. The proper way to do this is always 'attribute="value"', though HTML doctypes allow attribute minimization, which is what you see when it's just "checked." This is not allowed by XHTML, however.
    i did not know this, at least i learned something today xD thanks.

  9. #9
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Gridview in PHP

    if you're suggesting to "code it all yourself," then I would not suggest that you post a bunch of random examples of how to do things for someone who .. doesn't know how to do any of it.

    anywho, you're repeating code too much. for example, you first check if $sorting is equal to "desc," and then you check if it's equal to "asc," and then if it isn't set you echo out "desc." this is wasteful; check if it's set to "asc," otherwise echo "desc." and, because you have multiple columns and all of them would be sorted, this would also be repeating a bunch of code. use a variable; store the query string you're going to tack onto the links and display that on all of the table headings instead of repeating the same logic over and over.

    and, as always, I'm here to keep suggesting that you do not echo out HTML. this is especially true when you're using a lot of attributes rather than styling properly with CSS.

  10. #10

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Gridview in PHP

    Thankyou all.. I try this all and update my status..

Posting Permissions

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



Click Here to Expand Forum to Full Width