Jump to content

Displaying mysql data using php - alternate row color and pagination


howster2011

Recommended Posts

Hi There!

Hopefully someone can help. I'm looking for some php script that allows me to display mysql table data in alternating row colors (eg 5 columns and  500 rows). I would also like to add pagination and limit number of rows displayed at a time to e.g. 50. If possible I'd like to be able to sort asc/desc using titles(links) at the top of the displayed data. Hopefully someone may have a script like this that also shows the mysql connection code (sample host, pass, user etc...) - a working php file so to speak that I can edit.

Would really appreciate the help. If you could recommend some software or a wizard that could generate the php code for me I would really appreciate it too!

 

THANKS!  :D

Link to comment
Share on other sites

is a lot of examples here for that... just search the forum.

 

the implementation is going to depend on your expertize, now for code generators... also a search in google will give you tons of alternatives.

 

for grids for example here is an alternative  (which again is going to depend in your knowledge level or time to learn)

http://dev.sencha.com/deploy/dev/examples/

Link to comment
Share on other sites

Should be pretty simple, I would do it like in a while loop:

 

$color = true;

while ( $loops < $amountOfRows )
{
    if ( $color )
    {
        $color = false;
        // echo div start ..
    }
    else
    {
        $color = true;
        // echo div start..
    }   
    // MYSQL code here..
    // echo div end..
}

 

And for about how the connecting works.. check out w3schools? I assume you know how to do all this, and I am not going to paste a copy & paste code here. A help forum is meant to give you help, not to give you copy and paste code really..

 

For the amount of row maxing ... simply change the while to something like this:

while ( $loops < $amountOfRows && $loops< $maxEachPage )

 

Now, for the 'browsing' through these pages, I am going to give you some hints, and if you really can't figure it out, I'll explain it in detail, I would just like you to think a bit amount it first.

 

Hints:

- Try using $_GET variables to tell you the page.

- You are going to need to add another while loop after this one, which will echo a few href's which link to the next / previous pages.

- Change the while loop once more, taking into account which page the user is on.

 

Good luck!

 

 

Link to comment
Share on other sites

An easier way to alternate colors:

<?php
$color = '#0C0C0C';  //initial color
while ( ... ) { // loop code
   echo '<span style="background-color:' . $color . '">information</span><br>';
   $color = ($color == '#0C0C0C')?'#C0C0C0':'#0C0C0C'; // set to second color if already initial color, otherwise set back to initial color
}
?>

 

Ken

Link to comment
Share on other sites

Thanks a millions for the helpful responses guys! I'm starting out really with php code so need the basics of how to do the above. I saw lots of examples on the web but just wanted to see what yourselves with lots of experience would recommend.

Will investigate all the above and again thanks for time. Have a great day! :)

Link to comment
Share on other sites

Thanks a millions for the helpful responses guys! I'm starting out really with php code so need the basics of how to do the above. I saw lots of examples on the web but just wanted to see what yourselves with lots of experience would recommend.

Will investigate all the above and again thanks for time. Have a great day! :)

 

The tutorial and examples are pretty solid but if you have specific questions be sure to ask ;)

Link to comment
Share on other sites

Hi,

if i have a database called mydatabase with host = localhost, user = user1 and pass- pass1

 

and i have a table called people with the fields: name, age and location

 

I'd like to achieve the below in the output:

 

eg:

####################

name asc|desc    age asc|desc    location asc|desc

john                        33                new york        gray color row

mary                        54                london          white color row

alex                        32                france          gray color row

and so on...

 

pagination* <= previous 1|2|3 next page=>

######################

 

*List 50 rows at a time

 

 

 

...would anybody be able to advise of the entire code that would display the above.if i saw code that would do the above it would make more sense to me working backwards.

 

I checked out the basic pagination tutorial but it has me a little bamboozled.

 

Cheers Guys!

Link to comment
Share on other sites

...would anybody be able to advise of the entire code that would display the above.

 

It's been about three hours since your last post and the one before. Are you saying you have read through the tutorials that were provided and don't understand them? If so, what - specifically - do you not understand?

 

12. All request for code to be written for you should be posted under the freelance section. No exceptions
Link to comment
Share on other sites

Hi,

In http://www.phpfreaks.com/tutorial/basic-pagination the code appears to pull all the data from the table 'numbers'. I would like to know how to pluck out field data from the table.

 

..so if the table numbers had fields like: decimals, integers, fractions I would like to display the data in eg decimals and integers. Was hoping to see a link for the output in this specific example. If you could recommend a link where i could see working examples with output I'd really appreciate it.

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.