DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Need help with if else

By: rekha singh | 13 Mar 2010 1:15 pm

I could not get the sortby working. I don't know know what I did wrong. Can Someone look at my code and see what went wrong.

Here is my part of code.
<?php
include ('dbc.php');

if ($db) {

$field = $_GET['sortby' ];

$sql = "SELECT * FROM missionary";

if(isset ($field)){
if ($click==1){
$sql = $sql.' ORDER BY '.$field. ' ASC';
$click=0;
}
else{
$sql = $sql.' ORDER BY '.$field. ' DESC';
$click=1;
}

} 

$result = mysql_query( $sql) or die(mysql_error( ));

echo "<table width='100%' border='0' cellspacing= '0' cellpadding= '0'>
<tr>
<th class='width1' ><a href='viewtest2. php?sortby= sex'>Elder/ Sister</a> </th> 
<th class='width2' ><a href='viewtest2. php?sortby= first_name' >First Name</th>
<th class='width4' ><a href='viewtest2. php?sortby= last_name' >Last Name</th>
<th class='width5' ><a href='viewtest2. php?sortby= month_start' >Month (start)</th>
<th class='width6' ><a href='viewtest2. php?sortby= year_start' >Year (start)</th>
<th class='width7' ><a href='viewtest2. php?sortby= month_end' >Month (end)</th>
<th class='width8' ><a href='viewtest2. php?sortby= year_end' >Year (end)</th>
<th class='width9' >Action</ th>
</tr>";
while($row = mysql_fetch_ array($result) )

//while($row = mysql_fetch_ assoc($result) )
{

echo "<tr>";
echo "<td>" . $row['sex'] . "&nbsp;</td> ";
echo "<td>" . $row['first_ name'] . "&nbsp;</td> ";

if (!empty($row[ 'maiden_name' ])) {
echo "<td>" . $row['last_name' ] . "&nbsp&#40". $row['maiden_ name'] . "&#41 &nbsp;</td>" ; }
else 
{echo "<td>" . $row['last_name' ] . "&nbsp;</td> "; }

echo "<td>" . $row['month_ start'] . "&nbsp;</td> ";
echo "<td>" . $row['year_start' ] . "&nbsp;</td> ";
echo "<td>" . $row['month_ end'] . "&nbsp;</td> ";
echo "<td>" . $row['year_end' ] . "&nbsp;</td> ";

//echo "<td><a href=edit_update. php?id=" . $row['id'] . ">Edit/Update< /a></td>" ;

echo "<td><a href=view.php? id=" . $row['id'] . ">View history</a>< /td>";

echo "</tr>"; 
}
echo "</table>";
}
else {
echo "Database NOT Found ";
}

mysql_close( $link) ;
?>

</table>
</body>
</html>

 

 


 

Comments

What I generally do is check the GET parameter using isset() and then check if the variable is not = "".

For example:

If(isset($_GET[ 'sortby'] )

$field = $_GET['sortby' ];

Else

$field = "";

... and then ..

If($field != ""){

If($click==1) {

..

Your variable $field is set. You need to check if it is blank(NULL) or not, like a check for value.

By: rekha singh | 13 Mar 2010

It did not work. Here is my code

if(isset($_GET[ 'sortby'] == ""){
if ($orderby == 'DESC'{
$sql = $sql.' ORDER BY '.$field. ' DESC';
$orderby = 'ASC';
}
else{
$sql = $sql.' ORDER BY '.$field. ' ASC';
$orderby = 'DESC';
}
}

By: rekha singh | 13 Mar 2010

This line has a bracket missing, should be:

if ($orderby == 'DESC') {

By: rekha singh | 13 Mar 2010

It did not work. Let me post all my codes and take a look what I have done wrong.

<?php
include ('dbc.php');

if ($db) {

$orderby = $_GET['order' ];
$sql = "SELECT * FROM missionary";

if(isset($_GET[ 'sortby'] ) == ""){
if ($orderby == 'DESC'){
$sql = $sql.' ORDER BY '.$field. ' DESC';
$orderby = 'ASC';
}
else{
$sql = $sql.' ORDER BY '.$field. ' ASC';
$orderby = 'DESC';
}
}

$result = mysql_query( $sql) or die(mysql_error( ));

echo "<table width='100%' border='0' cellspacing= '0' cellpadding= '0'>
<tr>
<th class='width1' ><a href='missionary. php?sortby= sex&order =
DESC'>Elder/ Sister</a> </th>

<th class='width2' ><a href='missionary. php?sortby= first_name' >First
Name</th>
<th class='width4' ><a href='missionary. php?sortby= last_name' >Last
Name</th>
<th class='width5' ><a href='missionary. php?sortby= month_start' >Month
(start)</th>
<th class='width6' ><a href='missionary. php?sortby= year_start' >Year
(start)</th>
<th class='width7' ><a href='missionary. php?sortby= month_end' >Month
(end)</th>
<th class='width8' ><a href='missionary. php?sortby= year_end' >Year
(end)</th>
<th class='width9' >Action</ th>
</tr>";

while($row = mysql_fetch_ array($result) )
{
echo "<tr>";
echo "<td>" . $row['sex'] . "&nbsp;</td> ";
echo "<td>" . $row['first_ name'] . "&nbsp;</td> ";
if (!empty($row[ 'maiden_name' ])) {
echo "<td>" . $row['last_name' ] . "&nbsp&#40". $row['maiden_ name'] . "&#41
&nbsp;</td>" ; }
else
{echo "<td>" . $row['last_name' ] . "&nbsp;</td> "; }
echo "<td>" . $row['month_ start'] . "&nbsp;</td> ";
echo "<td>" . $row['year_start' ] . "&nbsp;</td> ";
echo "<td>" . $row['month_ end'] . "&nbsp;</td> ";
echo "<td>" . $row['year_end' ] . "&nbsp;</td> ";

echo "<td><a href=view.php? id=" . $row['id'] . ">View history</a>< /td>";
echo "</tr>";
}
echo "</table>";
}
else {
echo "Database NOT Found ";
}

mysql_close( $link) ;
?>

By: rekha singh | 13 Mar 2010

* if(isset($_GET[ 'sortby'] ) == ""){*

should be

* if(isset($_GET[ 'sortby'] ) && $_GET['sortby' ] != ""){*

do the changes. It should work :-)

By: rekha singh | 13 Mar 2010

print_r($_REQUEST) ; (what does this do?)
Result: array ( [sortby] => first_name [__utma] =>
138964134.875777220 .1253322453. 1256007407. 1256010464. 12 [__utmz] =>
138964134.125332245 3.1.1.utmcsr= (direct)| utmccn=(direct) |utmcmd=( none) )

then I put echo "orderby: $orderby"; and nothing happen.

By: rekha singh | 13 Mar 2010

the $_REQUEST variable is a global variable. if you print it, you will see everything that is going through GET and POST. It will show you what values are in your arrays you pass through get or post. that is all.

If $orderby isnt in the request variable, that means it didnt get sent to the script at all, there is probably a problem before that.

I would STRONGLY suggest you go and google these things for yourself, thats how everyone else does it. PHP.net has a crap-load of information and you will learn a lot more by reading than you will by asking questions here (especially in the user comments on php.net)

 

By: rekha singh | 13 Mar 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move