Jump to content

sort by???


nec9716

Recommended Posts

nothing is easy ...but I'm going to learn it slowly ...but at least it 's on is way...>!!!

 

I a small script where I show name, team,chassis and engine data in column sort by name

 

If I want to be able to (directly on the web page ) sort it by chassis or team or engine ....I'm not sure where i will have to insert those link ..?

 

function list_users()

{

$y = 0; //counter

 

$sql = 'SELECT *FROM `pilote` ORDER BY `pilote`.`name` ASC LIMIT 0, 30 ';

// $sql = "select * from pilote ";

$result = conn($sql);

 

echo "<table width='60%' align='center' cellpadding='0' cellspacing='0'>

<tr><td colspan='2' align='center' style='font-size:28px; font-weight:bold;'>Manage pilote Data</td></tr>

<tr><td colspan='2'> </td></tr>

<tr><td colspan='2' align='center' ><a href='".$_SERVER['PHP_SELF']."?action=add'>Add A new Pilote</a></td></tr>

<tr><td colspan='2'> </td></tr>";

echo "<tr><td colspan='2' align='center'><b>Click A Pilote name To Edit </b></td></tr>";

if (mysql_num_rows($result)){

 

while($rows = mysql_fetch_array($result)){

 

//change row background color

(($y % 2) == 0) ? $bgcolor = "#99ff66" : $bgcolor=" #33ff00";

 

$qname = $rows['name'];

$qteam =$rows['team'];

$qchassis =$rows['chassis'];

$qengine =$rows['engine'];

$id = $rows['id'];

 

//echo out the row

echo "<tr style='background-color:$bgcolor;'><td><a href='".$_SERVER['PHP_SELF']."?id=$id'>$qname</a></td>

<td>$qteam</td><td>$qchassis</td><td>$qengine</td></tr>";

$y++; //increment the counter

}//end while

echo "</table>";

}else{

//handle no results

echo "<tr><td colspan='2' align='center'><b>No data found.</b></td></tr>";

}//endif

}

thank you

Link to comment
Share on other sites

Yeah make a link that goes page.php?sortby=whatever

 

Then use the $_GET['sortby'] and change your SQL query accordingly

<?php
$sortby = $_GET['sortby'];

if ($sortby == 'name') { 
$sql = "SELECT * FROM `pilote` ORDER BY `name` ASC LIMIT 0, 30";
} elseif ($sortby = 'chasis') {
$sql = 'SELECT *FROM `pilote` ORDER BY `chasis` ASC LIMIT 0, 30 ';
} 
elseif 
// etc. etc
}
?>

 

 

Hope that helps some.

Link to comment
Share on other sites

I have insert your script at the beginning of mine

 

when the page load the first time it's sort in name like I want

 

I think to have GET working I need this:

<a href='".$_SERVER['PHP_SELF']."?sortby=chassis'>sort by chassis</a>

 

but when I click this link it bring me to a blank page

 

 

Link to comment
Share on other sites

Yes to set sortby you would just do

 

<a href="".$_SERVER['PHP_SELF']."?sortby='chassis'>Chassis</a>

 

You can fix my code up a little bit. It shouldn't be blank, don't add any other code to it, put this close to your mysql_query because all you're using 'sortby' for is to set a variable.

 

<?php
$sortby = $_GET['sortby'];

if (isset($sortby)) {
if ($sortby == 'name') { 
$sql = "SELECT * FROM `pilote` ORDER BY `name` ASC LIMIT 0, 30";
} elseif ($sortby = 'chasis') {
$sql = 'SELECT *FROM `pilote` ORDER BY `chasis` ASC LIMIT 0, 30 ';
} elseif {
// etc. etc
}
}
}
?>[code]

[/code]

Link to comment
Share on other sites

still have blank screen

 

I have write :::

 

$sortby = $_GET['sortby'];

if ($sortby == 'team') {

$sql = 'SELECT * FROM `pilote` ORDER BY `team` ASC ';//should be by team when I press the link

} elseif ($sortby = 'chasis') {

$sql = 'SELECT * FROM `pilote` ORDER BY `chassis` ASC ';//should be by chassis when I press the link

} elseif ($sortby = 'engine') {

$sql = 'SELECT * FROM `pilote` ORDER BY `engine` ASC ';//should be by engine when I press the link

}

$sql = 'SELECT * FROM `pilote` ORDER BY `pilote`.`name` ASC ';//sort by name when loaded first time

 

 

just below

 

$y = 0; //counter

 

maybe I should write it some where else?

Link to comment
Share on other sites

Don't know how you're getting a blank screen. I even went out and made a table on my database named cars and inserted some values. Works fine for me. Here's what I used. Let me know if you still have issues.

 

<?php

$connection = mysql_connect('host', 'user', 'password');
mysql_select_db('database', $connection);

function list_users() {
   global $connection;
    echo "<table width='60%' align='center' cellpadding='0' cellspacing='0'>
    <tr><td colspan='2' align='center' style='font-size:28px; font-weight:bold;'>Manage pilote Data</td></tr>
    <tr><td colspan='2'> </td></tr>
    <tr><td colspan='2' align='center' ><a href='".$_SERVER['PHP_SELF']."?action=add'>Add A new Pilote</a></td></tr>
    <tr><td colspan='2' align='center' ><a href='".$_SERVER['PHP_SELF']."?sortby=engine'>Sort by Engine</a></td></tr>
    <tr><td colspan='2' align='center' ><a href='".$_SERVER['PHP_SELF']."?sortby=chassis'>Chassis</a></td></tr>
    <tr><td colspan='2' align='center' ><a href='".$_SERVER['PHP_SELF']."?sortby=team'>Team</a></td></tr>
    <tr><td colspan='2'> </td></tr>";

    $sortby = $_GET['sortby'];
    if (!empty($sortby)) {
        if ($sortby == 'team') {
            $sql = "SELECT * FROM cars ORDER BY team ASC"; //should be by team when I press the link
        } elseif ($sortby = 'chassis') {
            $sql = "SELECT * FROM cars ORDER BY chassis ASC"; //should be by chassis when I press the link
        } elseif ($sortby = 'engine') {
            $sql = "SELECT * FROM cars ORDER BY engine ASC"; //should be by engine when I press the link
        }
    } else {
        $sql = "SELECT * FROM cars ORDER BY id ASC"; //sort by name when loaded first time
    }

    $result = mysql_query($sql, $connection);
    echo "<tr><td colspan='2' align='center'>Click A Pilote name To Edit </td></tr>";

        $y = 0; //counter
        if (mysql_num_rows($result) != 0) {
            while($rows = mysql_fetch_array($result)){

                //change row background color
                (($y % 2) == 0) ? $bgcolor = "#99ff66" : $bgcolor=" #33ff00";

                $qname = $rows['name'];
                $qteam =$rows['team'];
                $qchassis =$rows['chassis'];
                $qengine =$rows['engine'];
                $id = $rows['id'];

                //echo out the row
                echo "<tr style='background-color:$bgcolor;'><td><a href='".$_SERVER['PHP_SELF']."?id=$id'>$qname</a></td>
                <td>$qteam</td><td>$qchassis</td><td>$qengine</td></tr>";
                $y++; //increment the counter
            }//end while
        echo "</table>";
        } else {
            //handle no results
            echo "<tr><td colspan='2' align='center'>No data found.</td></tr>";
        }//endif
}


list_users();
?>

Link to comment
Share on other sites

ok here a simple version of what I have presently:

 

function list_users()

{

echo" <a href='".$_SERVER['PHP_SELF']."?sortby=team'>team</a>";

$sortby = $_GET['sortby'];

if ($sortby == 'team') {

$sql = 'SELECT * FROM `pilote` ORDER BY `pilote`.`team` ASC ';

}

$sql = 'SELECT * FROM `pilote` ORDER BY `pilote`.`name` ASC ';

$result = conn($sql);

echo "<table width='60%' align='center' cellpadding='0' cellspacing='0'>";

if (mysql_num_rows($result)){

while($rows = mysql_fetch_array($result)){

$qname = $rows['name'];

$qteam =$rows['team'];

$id = $rows['id'];

echo "<tr><td><a href='".$_SERVER['PHP_SELF']."?id=$id'>$qname</a></td>

<td>$qteam</td></tr>";

}//end while

echo "</table>";

}//endif

}

 

every time I press TEAM ( sort by team ) ...I have a white page "http://blablabla/add_edit_pilote.php?sortby=team"

 

 

Link to comment
Share on other sites

well with your script it work perfectly I will try to find out why mine don't work

 

thank you ;D

 

 

I'm pretty sure it's because you're declaring $sql after checking for $sortby

 

 

What I did was IF $sortby WAS NOT empty, it checked for the names ELSE it set the original sorting feature.

Link to comment
Share on other sites

that the difference between you guy's and me .....

 

I have a limited acknowledge of php... :D

 

thank I will look when I move $sql somewhere else what that do exactly...

 

 

 

No problem. You could always put the main one before the $sortby checking also. And if $sortby is set then it will reset the value of $sql

 

 

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.