Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Best Practices For Sortable Columns


I have several forms that display information from the database after
users log in. I would like the column titles to be sortable so that
when the user clicks on a column heading, the data re-displays in
sorted order of that column. Next time they click on it, it toggles
the order from ascending to descending, and then back to ascending
again.

Since I have many forms and each form has different columns, I was
wondering if there was a method that would be better than a brute force
approach. What are the best practices for this functionality?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Sortable Columns
Hi there, I am new to this Forums...I am currently learning PHP and I was wondering if any of you guys knows how to make columsn in a table sortable. What would I have to change in the code? Thanks!

Sortable Columns
Does anybody know here how to make columns in a table sortable? I know that with a sql query I can accomplish that. PHP Code:

Making Dynamic Table Sortable
I'm doing a small project right now where I create a table from a
database. This is the code, which Dreamweaver, for the most part,
has written:

***

<?php
mysql_select_db($database_tdream, $tdream);
$query_rstdream = "SELECT * FROM albums ORDER BY `year` ASC";
$rstdream = mysql_query($query_rstdream, $tdream) or die
(mysql_error());
$row_rstdream = mysql_fetch_assoc($rstdream);
$totalRows_rstdream = mysql_num_rows($rstdream);
?>

***

<table cellspacing="0">
<thead>
<tr>
<td>Album</td>
<td>Year</td>
<td>Era</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php do { ?>
<?php
$class = ($class == 'odd') ? 'even' : 'odd'
?>
<tr class="<?php echo $class ?>">
<td><?php echo $row_rstdream['Name']; ?></td>
<td><?php echo $row_rstdream['Year']; ?></td>
<td><?php echo $row_rstdream['Era']; ?></td>
<td><?php echo $row_rstdream['Type']; ?></td>
</tr>
<?php } while ($row_rstdream = mysql_fetch_assoc($rstdream));
?>
</tbody>
</table>

***

Sorry for the length of the examples. I have a basic
understanding of what the code is doing, so I should be able to
modify it if I knew I was heading in the right direction. My
object is to make this table sortable, by clicking on the column
headers. I have an additional column in my database table which
is an auto_increment counter. Would anyone have some ideas or
code that could help me figure this out? I'm not sure how to
approach it, and was hoping that there was a simple answer to the
problem.

Display 5 Columns From 10 Columns
How to the display 5 columns from 10 columns in a page using for loop reading data from txt file.

PHP Best Practices
I have a basic design question, in terms of what is "better"
programming.

I have a web site, and am using php pages as includes. some of the php
pages have php tags, and "echo" or "print" the php variables and html.

Like this:
<div id="body">
<?php include("phpincludes/server-nav.php"); ?>
</div>

the php page has code like this:
<div id="header">
<ul>
<li class="tab" <?php if ($thisPage=="Home")
echo " id="currentpage""; ?>>
<a href="http://abf966/LBA/index.php">Home</a>
</li>
<li class="tab" <?php if ($thisPage=="Introduction")
echo " id="currentpage""; ?>>
<a href="http://abf966/LBA/intro.php">Introduction</a></li...
etc

Some PHP Best Practices
I'm starting a new project, and I was wondering what is considered best
practise in the following cases:

* Connecting to multiple databases using the PEAR library.
* Triggering your archive script from within the project without cron on
the server
It doesn't looks right to start archiving each time a user visits a
page.
* Providing some sort of login authorization over XML-RPC.
If you work on a user-based permissions site, how do you continue that
to XML-RPC.

General OOP Practices..
I'm newer to the OOP in php world, but my questions revolve around good practices when programming OO in PHP rather than can this be done. So, I have a class with 7 functions in it, and I set in my constructor 7 variables. Is it better to pass the variables as needed through the functions for example:

Bad PHP Coding Practices?
I'm just wondering what are some bad (inefficient / resource intensive) PHP coding practices. For example, in Visual Basic string concatenation in a loop can really slow things down since a new string is created each time. Object creation has overhead and only so many objects can be created before it bogs down. Any significant problems with $a .= $a + "text" or objects in PHP? Any other bottlenecks?

Best Practices - GUI Standards
I hope this is on topic this looks like a pretty generic PHP group. If not
I'm a nooB so be gentle.

Question:
Could anyone recommend a "best practice" for maintaining commonality of what
the user sees on a medium scale PHP application? For example: button size,
color, font, alignment, labels, etc.

My intent is to build an application that has been pre-designed in phases,
piece by piece and outsource most of the coding. There must be a common
look and feel. Is there a better way then me simply listing them out? Ex:
all buttons are 10px by 20px, all font arial 10pt, etc.

Programming Practices
Which are considered to be the best programming practices? for example i prefer to not store sessions into files (as done by default by php), but in a database as this gives me the possibility to use the same session on multiple servers, this is possible because of the session_set_save_handler() function! So which do you use (non-oop)?

To enlarge the topic a bit: which threads are all out there and how do you solve them (e.g.: cross site scripting)?

Include() & Paths - Best Practices
I have a question regarding paths and the include() statement in PHP.

I develop in a Windows environment and will be publishing to a Linux
server. I would like to do the following:

1. Setup my include references in such a way that I don't have to
change them all every time I have to publish to the production server

2. Setup above in such a way that won't involve php.ini (& LInux
equivalent), as I have access to edit this file locally, but won't be
able to do so in the production environment

My file structure is such:

wwwroot
subdir
global
inc

I tried Windows-styled virtual references such as "/" to refer to the
wwwroot, but that didn't work in the production evironment. I switched
to relative paths (eg include("global/inc/config.inc") or include
("../global/inc/config").

I guess what I'm looking for here is a best practice for how to set
the includes up for maximum portability across servers with different
file systems as well as different platforms. Can you, for instance,
incorporate a variable that defines the root path into the include
statement above? If so, where do you define this variable globally
without having to add to every file? (b/c you can't use an include
statement if you haven't gotten the includes to work) Is there an
equivalent of global.asa for the PHP world?

I'm just raising some issues with my experience thus far. I know
there's a solution and a best practice that I'm just missing to the
point--I would appreciate any wisdom that could be shared.

PHP Default Host Best Practices
I've got my dev and production environments all setup, and the mysql
DBs working fine. What need to do is set the mysql.default_host option
in php.ini to be one thing on dev, and something different on
production. Unfortunately, my web host doesn't allow me to mess with
php.ini.

So, what's the best way to specify a different host to mysql_connect()
on dev and production. Yes, I could manually edit the variable
everything I FTP, but I'd rather not.

Should I use a global variable in a separate include file? Other
options?

Best Practices For Bundling PHP Runtime Libs?
My company makes a tool which generates (among other things) PHP
client-side bindings to WebServices. We have also implemented a
collection of PHP-code required by these bindings ("runtime
libraries"). Our tool creates a file containing the PHP-bindings, but
the runtime libraries should also be placed somewhere where it will be
picked up by the PHP-engine.

Is there a common/standard way or to do this in the PHP-community? I do
not like the idea of installing the runtime libraries in system-folders
or something like that. Would someone offer their recommendations?

UPDATE Columns With PHP
So you know I'm going through Kevins book.

I'm updating data in a test table. I'm adding names to each joke, say I have 7 jokes and 3 of them go to the same person. I can update each one seperately but I can't find a way of bulk updating. In this case it's not a problem.

I've looked though Kevins book and a couple of others but can't find anything. Tried several sites to but I may just be searching wrong.

I can do this:
mysql> UPDATE Jokes SET AID="1" WHERE ID=2;

but I'd like to update WHERE ID equals 2,3,4,5 all at once.

How To Print All The Columns In A Row?
I want to display all the columns in a row without have to write the code for them all, is that possible?

Columns In Html
I am trying to make a table with two columns. I am really close. The problem is the table's first row has three TD's. ie

one two there
four five
six seven

PHP Code:

Grouping The Columns
Let's say I have a very simple Table:

+---------------------+
| id |  name  | level |
+---------------------+
|  1 |  Adam  |   1   |
|  2 |  Jack  |   7   |
|  3 |  Bill  |   1   |
|  4 |  Pete  |   3   |
|  5 |  Ross  |   7   |
+---------------------+

but I want the PHP to output their id's and names, but group their Levels together like so:

Level 1:
Adam (id: 1)
Bill (id: 3)

Level 3:
Pete (id: 4)

Level 7:
Jack (id: 2)
Ross (id: 5)

And then afterwards, If I create someone with a new level, PHP will automatically create a new heading (e.g. Level 10) and put the corresponding names under it.

How should I approach this? Should my PHP contain two "while"s? Or do I use the SQL <group by> function?

Displaying Columns With A Value
what is the most efficient way to display column names from a database that only have the value '1' in its fields??

I cant seem to work it out, could someone give me a code example??

Creating Columns
I've been using this code to split records from a db into columns when it displays the data. Items are shown with 3 on a row and then a new row starts. It works really well but if ever there is only one item on a row it causes me a problem in this instance.

I have far left and far right columns which display a background image, which is a border. The problem is this only works if there are three columns visible in a row.

Is it possible to manipulate the following code so that there are always three columns present, they're just empty if there's no more records to fill them? Code:

Count The Number Of Columns
I was wondering if there is a way to calculate the total of columns in a table? (something like $totalRows= mysql_num_rows($Recordset1); , but than for columns)

Query Results In 2 Columns?
I'm struggling to figure out how to do this properly. I have done a mySQL
query to extract a list of names, and done a mysql_num_rows to determine how
many results I've gotten. I want to split the list in to two columns. I've
created $percol = $mysql_num_rows/2 but I'm not quite sure how to "loop"
through the first half of the list, echo </td><td> and then list the
balance. Can anyone give me some guidance on how best to structure this?

Printing Columns And Its Values
What would be the best way to get the columns of a table and the values I tried following code:

Index On A Combination Of 2 Columns ?
on a Mysql table i have 2 columns:

- banner views
- banner clicks

now i want an index on the MySQL table so that i can make an overview with the order
set to bannerclicks/bannerviews so i need an index on (bannerclicks / bannerviews )
is there a way to do this in MySQL without the need of an extra column ?

Changing Values In Columns
how do i change the value of a cell in a row in a table?

Searching Several Columns In MYSQL
I wonder if anyone can point me in the right direction on this, I am trying to search multiple columns on a single database and display the info.

I have this set up but it only queries one.

$query_rsSearch = sprintf("SELECT * FROM database WHERE column1 LIKE '%%%s%%'", $colname_rsSearch);

The above works fine for one column but I am having difficulty making it search more than one, I have been trying this method.

$query_rsSearch = sprintf("SELECT * FROM database WHERE column1,column2 LIKE '%%%s%%'", $colname_rsSearch);

But continually get error messages?

Combining Two Columns In A Select
I have two tables with the same fields, and I wish to select one field from them. However, i don't want two columns in the result, i want one column which is the first table's column and the second table's column combined. (this is so I can order them and have the order include both tables) Code:

How To Sort 32 Photos Into 8 Columns?
i want to sort a collection of photos into 8 columns...but i don't know how to do it?
Let's say a user inserts 26 photos and i want to sort it in 8 columns and 4 rows, how can it be done?

Database Tables And Columns
Is there an easy way to display in my browser window all of the tables and their corresponding column names for a selected mysql database?

How To Make Table's Columns?
I have some pictures in database and I want to show it on my webpage. The problem is, I want to make those pictures are shown on a table with 3 or 5 columns before new rows.

For example, I insert this code

<?php 
while ($data = mysql_fetch_array($show){
echo"<table>
echo"<tr><td><img src='$url/thumbnails/$data[image]'></td></tr></table>";

to show the pictures from database, the pictures will be shown vertically with only one columns. Could anybody tell me how to get some columns before new row? I hope you understand my question. I have no idea how to find the keywords for googling.

Split Text Into Two Columns
I want to be able to split the contents of a text field into two or maybe
three columns. The text field contains text AND HTML mark-up.

My initial thought was to find the middle character and then go to the
nearest space and split the text that way, but it sometimes splits in the
middle of an HTML tag: not pretty!

My next idea was to strip the HTML tags from the text and then split it up
and then reapply the HTML tags, but I am not sure this would work either.

Has anyone come across this problem and do you have a solution or know of
one? I would really appreciate it.

Total Table Columns
I have a report that pulls data from a mysql table and generates a html table with the data. I need to sum up the columns into 2 separate  fields  I think I need to access the result from the query but do not know how. Code:

ORDER By (dividing Two Columns)
Im making a gallery which allows users to rate the images. I have two columns noOfRatings (which hold the no of tiems an image has been rated) and totalRatingScore (which holds the total of all the ratings for that particular image)

I want to make a sort option e.g 'sort by highest rated'

Is there a quick way to return the recordset ORDERED by highest rated.. i wa thinking somethign along the lines of

SELECT * FROM TABLE ORDER BY noOfRatings/totalRatingScore

Can i do it this way or is some special techinque needed?

Putting Results Into Columns
I am creating a directory of businesses for a web site. There are 8 main categories (dining, bars, entertainment, etc) and when a user clicks on a category it takes them to a page that lists all the businesses (and phone numbers) in that category in alphabetical order. The problem I have is that it does so in one big long column.

Can anyone help me alter the code to display the results in 3 columns? I do want to keep them in alphabetical order, however if that is straight across or down does, it does not really matter to me. And I am fine with losing the Character (A, B, C, etc) header above each set if needed. Code:

Combining A Few Columns Into One Column
I am transferring data from one script to another script and the new software that I downloaded requires that I have multiple items separated by | in one field. The way I have it now is in 4 different fields. The way it is set up is like this:

Table  Freelancers
Field1  expertise1
Field2  expertise2
Field3  expertise3
Field4  expertise4

Each of these fields have one integer in each of them representing a specific occupation. Each freelancer had 4 occupations that they could choose. This is the data from the old site. On the new site, there is also a list of occupations (in a text file) that is different from the old list. For example, web design = 4 on the old site, and web design = 24 on the new site.

So I want to take the 4 occupations (expertise1, expertise2, expertise3, and expertise4) and combine them in a 5th field.  I set up a 5th field called specif_occupation to combine the data in ... the information should look something like this when combined |4|5|8|14|76|

I only need to know how to combine the 4 fields into 1 field. I could figure out the rest (probably, I'm out of practice).

How Do I Split Into 3 Columns With Array?
The script below works great, however it displays 396 thumbnails. What I would like to do is organize the look and feel better. I want o be able to display the thumbnails in 3 columns rather then scrolling down for ever looking at all the pictures. So how would I make it to break into 3 columns? .....

Select Distinct Values In Only 2 Columns
I have a table where a customer may have multiple accounts.

I want to eliminate duplicate values for Customer_ID and Account_ID -- regardless of their state or region.

I tried this,

SELECT (DISTINCT Acct_ID, Cust_ID), Address, City,State, Zip,Country, Contact, State_ID,Region_ID,
FROM t_table

but it doesn't work.

This (below) does work, but it gives 2 records. One record when a person is listed once in a target state, and a second record if that person is listed in an overlapping region.

SELECT DISTINCT Acct_ID, Cust_ID, Address, City,State, Zip,Country, Contact, State_ID,Region_ID,
FROM t_table

(this is the same as)
SELECT DISTINCTROW Acct_ID, Cust_ID, Address, City,State, Zip,Country, Contact, State_ID,Region_ID,
FROM t_table

Is there any other way to select distinct values in only 2 columns?

Tabular Columns In Select Options?
Anyone have the foggiest idea on how to create tabular columns in select options so that the displays are aligned under each other?

Sorting A Multidimensional Array On Two Columns
I'm working on some low-tech sort of content managing system. It uses textfiles as a kind of database so the users don't have to install SQL on their server. Now I want to perform some sorting on my files. These are the contents of one of those files.

PageName;Template;Menu;Sub;
Introductie;1;1;1;
Opnemen;1;2;0;
Voorbereidingen;1;2;2;
Kwaliteit;1;1;0;
Blabla;1;2;1;

I first read this file into $someArray with file() and then I parse each row with explode() into columns. So $someArray[2][2] contains the value 2 and $someArray[0][2] contains "Sub".

I want to sort this array on both Menu and Sub and write it back to the file. It should look like this.

PageName;Template;Menu;Sub;
Kwaliteit;1;1;0;
Introductie;1;1;1;
Opnemen;1;2;0;
Blabla;1;2;1;
Voorbereidingen;1;2;2;

I want to make a list of menus and their listed submenus in the proper order, as you can see .

What is the clever way to do this? At this moment I use a loop that sorts the submenu in a loop that finds the right menu in a loop that sorts the menus. No, it's not you, I don't understand it myself anymore. That's because I don't understand most of the array principles and functions.

Equal Height Columns With 1px Border
I need content boxes equal height with 1 px border, i tried this one http://www.positioniseverything.net...ple/equalheight , but i think it doesnt work standard for a 1px border, is it a good idea to measure the text with php to solve this?

Split Mysql Results Into Two Columns
Trying to develop php catalog, but can't split the results into two columns, PHPcode:        

Sort 2D Arrayby Multiple Columns?
is there an easy function/ class for sorting a 2D array multiple
fields. I would like to specify a primary sort and secondary sort.

Displaying Rows Of Data As Columns?
Does anyone know of an efficient way to retreive data from a mysql db
and display the record rows as collumns?

I have been doing this:
<display headers>
<while ($row = mysql_fetch_array($result)) { >
<display row 1 of result>
<display row 2... } >
<end table>

but I want to do this instead:
<col 1 header > <row 1 col 1> <row 2 col1> <row3 col1>
<col 2 header> <row 1 col 2> <row 2 col2> <row3 col2>
<col 3 header> <row 1 col 3> <row 2 col3> <row3 col3>

Columns In A Form Select Element
Can this be done. I am wanting to have columns in my forms select element. I
have data of varied length from three fields. Can this be done, or should I
try and stuff spaces into the select element to asume columns

LEFT JOIN, Two Similar Columns
In my left join there are two columns that have the same name.. How can I get their different values when rgetting them through an array?

Searching Multiple Mysql Columns
I'm currently building a database of local businesses, I'm quite new to using php and mysql. While I'm mostly managing to stumble my through it, a couple of things are confusing me. This is one of the things. If anyone can help, that would be really great.

Ok, so I've build a search page, and a results page and it works great but there is one problem. Currently it will only search through the 'name' column, but I'd also like it to search through the 'type' column at the same time.

This is the code I'm using to fetch the results
$colname_Businesses = "-1";
if (isset($_POST['search_par'])) {
  $colname_Businesses = $_POST['search_par'];
}
mysql_select_db($database_HBBA_DATABASE, $HBBA_DATABASE);
$query_Businesses = sprintf("SELECT * FROM Businesses WHERE name LIKE %s", GetSQLValueString("%" . $colname_Businesses . "%", "text"));
$query_limit_Businesses = sprintf("%s LIMIT %d, %d", $query_Businesses, $startRow_Businesses, $maxRows_Businesses);
$Businesses = mysql_query($query_limit_Businesses, $HBBA_DATABASE) or die(mysql_error());
$row_Businesses = mysql_fetch_assoc($Businesses);

I'm not sure what to add in to make it also search through the 'type' column or where to put that. Can anyone point me in the right direction?

Dynamically Create A Table's Columns
What I'd like to do, is take the selected rows from my database, generate columns dynamically to reflect the "size" column, and then dump the data respectively into the table that is generated. Here's what I've got so far:
Using this database:

What I've done is given each row/column a designation by assigning a X,Y value (tag_no, size).  I created an array that uses a key in the same X,Y format.  I loop through the rows to dump the array value that corresponds to the key. 

The data doens't display correctly if I there are multiple tag_no's that have the same sizes, with different quantities. Code:

Displaying Database Information In Columns
I have a database with products in and I want iT to automatically show every product on the page. Now I am able to do this easy enough but what I am struggling to do is that I want the products to be displayed in 3 columns. I can get them to display one under the other easy enough but I want them to be in rows with 3 columns instead.

Math Between Table Columns And Updating Values
Basically what i want to do is simple math. I want to pull the value of one column add a percentage to it and update another column with that new value.

column a + 55% = column b updated.

I have 12k prices to update, other wise this would probably be easy.

Display Data In Columns Of 4 And Repeat Rows
What I would really like for it to do is display the results across 4 cells and then start on the next row and so on. Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved