PHP - Sort And Paging With Where Condition
Hello everybody,
i have page called cat.php
$cat_name get the data submited from othe .php page see line 77
this page show records WHERE category='$cat_name' see line 78
i have 2 problems:
first problem: if i dont have this "where" condition, the sort function works fine see line 74
second problem: if i dont have this "where" condition, the paging function works fine see line from 108
here is my code
thank you very much for your help
Rafal
<html> <head> <title></title> </head> <body> <?php> include("config.php"); ?> <?php $verbindung1 = mysql_connect("$hoster", "$nameuser", "$password") or die ("connection error"); mysql_select_db("$basedata") or die ("database error"); $abfrage = "SELECT cat_id, cat_name FROM $table3 ORDER BY cat_name ASC"; $ergebnis = mysql_query($abfrage); $num = mysql_num_rows($ergebnis); ?> <table> <tr> <?php while ($row = mysql_fetch_object($ergebnis)) { ?> <td> <font face="Arial" size="2"> <form action="cat.php" method="post" name="cat_form" id="cat_form"> <input type="hidden" name="inp_cat_name" value="<?php echo $row->cat_name; ?>"> <input type="submit" value="<?php echo $row->cat_name; ?>"> </form> </font> </td> <?php } ?> </tr></table> <?php> include("config.php"); ?> <?Php try { $dbo = new PDO('mysql:host=db.abcdef.fr;dbname='.$basedata, $nameuser, $password); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } $page_name="cat.php"; // page name @$sort=$_GET['sort']; if(strlen($sort)>0 and !ctype_alnum($sort)) { echo "Data Error"; exit; } @$start=$_GET['start']; if(strlen($start) > 0 and !is_numeric($start)) { echo "Data Error"; exit; } $zero = ($start - 0); $limit = 20; // number of rows per page $this1 = $zero + $limit; $back = $zero - $limit; $next = $zero + $limit; $query2=" SELECT destination, stars, price FROM $table1 WHERE vis='yes'"; $count=$dbo->prepare($query2); $count->execute(); $nume=$count->rowCount(); ?> <font face="Arial" size="2">Sortieren nach:<br> <?php echo "<a href='$page_name?sort=destination'>Zielort</a>"; ?> | <?php echo "<a href='$page_name?sort=stars'>Sterne</a>"; ?> | <?php echo "<a href='$page_name?sort=price'>Preis</a>"; ?><br><br> </font> <?php $cat_name = $_POST["inp_cat_name"]; $query=" SELECT * FROM $table1 WHERE category='$cat_name'"; if(isset($sort) and strlen($sort)>0){ $query = $query . " order by $sort"; } $query = $query. " limit $zero, $limit "; foreach ($dbo->query($query) as $row) { ?> <table cellspacing="0" cellpadding="0" border="1" width="600px" class="table"> <tr> <td rowspan="2" valign="top" class="td"> <img src="<?php if ($row[photo1] != '') { ?><?php echo $row[photo1] ?><?php } ?><?php if ($row[photo1] == '') { ?><?php echo 'cloud/500x282.gif' ?><?php } ?>" width="200" height="113"> </td> <th width="400px" class="th"><font face="Arial" size="2"><b><?php echo $row[product] ?></b> <?php if ($row[stars] == "1") { ?><?php echo "<img src='cloud/star.png'>"; } ?> <?php echo $row[destination] ?> (<?php echo $row[category] ?>)</font></th> </tr> <tr> <td valign="top" class="td" style="padding:7px;"> <font face="Arial" size="2"> <?php echo $row[shortdesc] ?><br> from <font size="4"><b><?php echo $row[price] ?> €</b></font> <font face="Arial" size="1">Nr. <?php echo $row[inventar] ?></font><br> <br> </font> </td></tr> </table> <br> <?php } if($back >=0) { echo "<a href='$page_name?start=$back&sort=$sort'><font face='Verdana' size='2'>back</font></a>"; } echo " "; $i=0; $l=1; for($i=0;$i < $nume;$i=$i+$limit){ if($i <> $zero){ echo " <a href='$page_name?start=$i&sort=$sort'><font face='Verdana' size='2'>$l</font></a> "; } else { echo "<font face='arial' size='2' color='black'>$l</font>";} $l=$l+1; } echo " "; if($this1 < $nume) { echo "<a href='$page_name?start=$next&sort=$sort'><font face='Verdana' size='2'>next</font></a>";} ?> </body> </html> Similar TutorialsHi All i am wanting a column list like this http://extensions.joomla.org/extensions basically the database is set up as (id, category, parent) I want the parent to group the category section and list like the the joomla example in three columns search various threads throughout the internet but none seem to cover this entirely can any one please help????? so my database has a page number next to each field. What I need to know how to do is make it so that the paging number, ie being able to select the page, if greater than one, will display 1-whatever the highest page number is. does this make sense? any help is appreciated. Hi Everyone, I need help in paging, i have a form with a lot of text boxes and drop down boxes, i need to use paging "Next..1..2..3", Please someone help me with it, i tried some codes but didn't work out. Thanks RAM I see several great PHP/MySQL paging systems on google, but how can I make a paging system when I'm just showing all images in a directory, not showing all images from a database? I have no idea how I could convert one of those mysql systems to work for my needs. Here is my code: <?php $columns = "4"; $count = "0"; echo '<div style="overflow:auto; width:500px; height:266px;"><table cellpadding="0" cellspacing="10">'; if ($handle = opendir('.')) { while (false !== ($image = readdir($handle))) { if ($image != "." && $image != ".." && $image != "index.php") { if (strlen($image) > 11) { $short = substr($image,0,11); echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$short.'...</div></a></td>'; } else { echo '<td align="center"><a href="'.$image.'" title="'.$image.'"><img src="'.$image.'" width="100" height="100"><br><div style="overflow:auto; width:100px;">'.$image.'</div></a></td>'; } $count++; if ($count == 4) { echo "</tr><tr>"; $count = 0; } } } closedir($handle); } echo "</table></div>"; ?> This code shows all images in a directory, 4 per row. I'd really appreciate some help with adding a paging feature to this. Thank you so much for the help! Hello, everyone. I am having some trouble figuring this out. I am trying to create a paging bar(so to speak) to display results by page number. Trying to get the paging bar to go as follows... Requested Page The resulting paging bar Page 1 1 2 3 4 5 Page 2 1 2 3 4 5 Page 3 1 2 3 4 5 Page 4 1 2 3 4 5 Page 5 4 5 6 7 8 Page 6 4 5 6 7 8 Page 7 4 5 6 7 8 Page 8 7 8 9 10 11 ...... This is what I have right now that I am trying to get to work. I have changed this so many times and been working on it for 2 days. I think I'm just confusing my self. This can't be that hard.. Code: [Select] <?php echo '<html>'; //---these values changed based on query---- $Page=$_GET["page"]; //the page number requested $AvailablePages=5; //number of pages available after the page requested //---calculations--- $StartPage=round($Page/5)*5; $EndPage=ceil($StartPage/5)*5 ; //the last page number to be displayed in paging if($StartPage<5) {$StartPage=1;} //just looking at variables echo 'page is: ' . $Page . '<br>'; echo '<br>Starting @: ' . $StartPage; echo '<br>Ending @: ' . $EndPage . '<br>'; //--create the paging bar-- $PagingBar=''; while($StartPage<=$EndPage) { if($Page==$StartPage) { $PagingBar=$PagingBar . $StartPage . ''; } else { $PagingBar=$PagingBar . ' <a href="test.php?page=' . $StartPage . '">' . $StartPage . '</a> '; } $StartPage++; } //---show the paging bar-- echo $PagingBar; echo '</html>'; ?> Any one no a simple way to create a numbering system for a basic comment system. Code: [Select] <body> <?php $commentsTable = "comments"; $comments = "SELECT * FROM $commentsTable"; $commentResults = mysql_query($comments); $commentRows = mysql_fetch_array($commentResults); ?> <div id="BodyWrapper"> <?php while($commentRows = mysql_fetch_array($commentResults)){?> <div id="comments"> <div id="CommentWrapper"> <div id="comment"> <div id="UserName"><? echo $commentRows['name'];?></div> <div id="UserComment"><? echo $commentRows['comment'];?></div> <div id="UserEmail"><? echo $commentRows['email'];?></div> <div id="PostDateTime"><? echo $commentRows['datatime'];?></div> </div> </div> </div> <?php } mysql_close(); ?> </div> </body> This is what i have, i want to create a limit of 5 comment but with nummber so you can view older posts. Guys i badly need need to add paging in this . 20 records per page how to accomplish ? Code: [Select] <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $http = 'myurl.com/other.php?like=' ; $conn=odbc_connect('apple','',''); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT * FROM mytable "; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} echo "<table border= 1><tr>"; echo "<th>ID</th>"; echo "<th>Like</th>"; echo "<th>title</th></tr>"; while (odbc_fetch_row($rs)) { $ids=odbc_result($rs,"ID"); // $links=odbc_result($rs,"links"); $title=odbc_result($rs,"title"); echo "<tr><td>$ids</td>"; echo "<td><fb:like href=\"{$http}{$ids}\" layout=\"button_count\" show_faces=\"false\" width=\"100\" font=\"tahoma\" colorscheme=\"dark\"></fb:like> </td>"; echo "<td>$title</td></tr>"; } echo "</table>"; odbc_close($conn); ?> Dear all, I have code for paging The problem is, one of them do not work properly This is OK http://www.amec-berca.co.id/abi/index.php?action=past-projects This is not OK http://www.parnaraya.net/beta/index.php?link=news My question: Is there any issues about PHP/Apache version? or something else? I used this script very often, and no problem so far except this time FYI, the host server using Virtualmin version 3.89 Thanks Hi. I'm trying to make a sort of a forum, and this is the part of the code that needs to be changed, only I don't know how. Code: [Select] mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name ORDER BY id DESC"; // OREDER BY id DESC is order result by descending $result=mysql_query($sql); ?> <table width='50%' border="0" align="center" cellpadding="3" cellspacing="1"> <?php $limit=10; $page=0; $numrows = mysql_num_rows($result); $pages = intval($numrows/$limit); if ($numrows % $limit) { $pages++;} $current = ($page/$limit) + 1; if (($pages < 1) || ($pages == 0)) { $total = 1;} else { $total = $pages;} $result = mysql_query("SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $page,$limit"); while($rows=mysql_fetch_array($result)){?> <tr><td><font size="+2"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></font></td> <td width='0*'><? echo $rows['datetime']; ?></td></tr> <tr><td><? echo substr($rows['detail'], 0, 250)."..."; ?> <a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo "Citeste mai mult";} ?></a> </td></tr> <tr><tr><td> <? if ($page != 0) { // Don't show back link if current page is first page. $back_page = $page - $limit; echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a> \n");} for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it. { $ppage = $limit*($i - 1); if ($ppage == $page){ echo("<b>$i</b>\n");} // If current page don't give link, just text. else{ echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");} } if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link. $next_page = $page + $limit; echo(" <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");} ?> </td></tr></tr><?php mysql_close(); ?> </table> I made a database with 4 fields and 11 entries. The script shows the last 10 entries just as it should, but when I click on the 'next' button, nothing happens (it should show the first entry, but it just refreshes the page, and still the last 10 entries/1st page are shown). I think it has something to do with the link, but I can't figure out what exactly. Can someone help, or at least give me a hint? Thanks in advance. Bye. Hello everyone! I'm completely new to PHP and Databases. I've managed to do some nice work by reading, but now I've encountered a problem I can't solve. I made a site with trailers and got tierd of adding them manually. So I created a form which sends the trailer info to my database. There's now trailers in specific organized genres. But the list is beginning to get really big so I wanted to show just 10 trailers per page. And so I found out I could use a paging system which I ALMOST got working. Just one last thing that needs to be fixed and that is when I click on next page "http://www.xxxxxxxx.com/test.php&page=2" it doesn't show the content. While "http://www.xxxxxxxx.com/test.php" does. So I just need that next button to work, I don't know if theres something missing or what it is... Note: I'm new to all this so please excuse my very very bad coding. Here is my code: test.php <link rel="stylesheet" type="text/css" media="all" href="http://www.xxxxxxxxxxxxx.n.nu/createdfiles/trailers/style-twentyten.css" /> <link rel="stylesheet" type="text/css" media="all" href="http://www.xxxxxxxxxxxxx.com/paginate.css" /> <?php $rpp = 10; // results per page $adjacents = 4; $page = intval($_GET["page"]); if(!$page) $page = 1; $reload = $_SERVER['PHP_SELF']; // connect to your DB: $link_id = mysql_connect("server","username","my password"); mysql_select_db("database"); // select appropriate results from DB: $sql = "SELECT * FROM blablabla WHERE Genre LIKE '%Drama%' ORDER BY `blablabla`.`sort` DESC LIMIT 0, 9999"; $result = mysql_query($sql, $link_id); // count total number of appropriate listings: $tcount = mysql_num_rows($result); // count number of pages: $tpages = ($tcount) ? ceil($tcount/$rpp) : 1; // total pages, last page number $count = 0; $i = ($page-1)*$rpp; while(($count<$rpp) && ($i<$tcount)) { mysql_data_seek($result,$i); $query = mysql_fetch_array($result); // output each row: echo "<br />--------------------------------------------------------------------------------------------------------<br /><br />"; ?><strong> <img src=" <?php echo $query['Poster'];?>" /><?php echo "<br /> <br />"; ////////////////////////////// //SHOW TITLE, YEAR AND GENRE// ////////////////////////////// echo "<H1>"; echo $query['Title']; echo "<br /> <br />"; echo "</h1>"; ?></strong> <?php echo "<H2>"; echo $query['Year']; echo "</H2>"; echo "<br />"; echo "<H3>"; echo $query['Genre']; echo "</H3>"; ?> <a href=" <?php ////////////////////////////// //SHOW IMDB, ACTORS AND PLOT// ////////////////////////////// echo $query['IMDB']; ?> " target="_self">Link to IMDb</a> <?php echo "<br /> <br />"; echo "<H2>Stars:</H2>"; echo "<br />"; echo "<H3>"; echo $query['Actors']; echo "</H3>"; echo "<br />"; echo "<H2>Plot:</H2>"; echo "<br />"; echo "<H3>"; echo $query['Plot']; echo "</H3>"; echo "<br /> <br />"; echo "<H2>"; //////////////// //SHOW TRAILER// //////////////// ?> <div id="<?php echo $query['ID'];?>" style="display: none;"> <?php echo $query['Youtube']; echo "</div>"; ?> <a href="javascript:ReverseDisplay('<?php echo $query['ID'];?>')">Click to show/hide trailer.</a> <?php echo "</H2>"; echo "<br /> <br />"; $i++; $count++; } // call pagination function: include("paginate_one.php"); echo paginate_one($reload, $page, $tpages); ?> <?php //////////////////////////////// //SCRIPT FOR SHOW/HIDE TRAILER// //////////////////////////////// ?> <script type="text/javascript" language="JavaScript"><!-- function HideContent(d) { document.getElementById(d).style.display = "none"; } function ShowContent(d) { document.getElementById(d).style.display = "block"; } function ReverseDisplay(d) { if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; } else { document.getElementById(d).style.display = "none"; } } //--></script> paginate_one.php <?php /************************************************************************* php easy :: pagination scripts set - Version One ========================================================================== Author: php easy code, www.phpeasycode.com Web Site: http://www.phpeasycode.com Contact: webmaster@phpeasycode.com *************************************************************************/ function paginate_one($reload, $page, $tpages) { $firstlabel = "First"; $prevlabel = "Prev"; $nextlabel = "Next"; $lastlabel = "Last"; $out = "<div class=\"pagin\">\n"; // first if($page>1) { $out.= "<a href=\"" . $reload . "\">" . $firstlabel . "</a>\n"; } else { $out.= "<span>" . $firstlabel . "</span>\n"; } // previous if($page==1) { $out.= "<span>" . $prevlabel . "</span>\n"; } elseif($page==2) { $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n"; } else { $out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n"; } // current $out.= "<span class=\"current\">Page " . $page . " of " . $tpages . "</span>\n"; // next if($page<$tpages) { $out.= "<a href=\"" . $reload . "&page=" .($page+1) . "\">" . $nextlabel . "</a>\n"; } else { $out.= "<span>" . $nextlabel . "</span>\n"; } // last if($page<$tpages) { $out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $lastlabel . "</a>\n"; } else { $out.= "<span>" . $lastlabel . "</span>\n"; } $out.= "</div>"; return $out; } ?> Here you can view a test of it all in action: http://www.mansgullberg.com/test.php please dont hack my site I'm thankfull for all of your answers! Really nice forum this! /kvickan I have a list generated from an array by foreach as Code: [Select] foreach ($list_array as $item) { echo "$item<br />"; } This is a long list, how can I separate the list to different pages? I mean the simplest way to do so I'm very new to this and really could use some help. I've got a Web app that has one form that collects data from the user and puts it into a mysql database and has another form that allows the user to select critiera to find records in the database and display them on the page. All this is working just fine, but now that my database is getting more data in it, I want to add functionality to display 10 records on a page with results page navigation links so the user can move forward and backward in the results set. This part is not working and I've put in echo statements to figure out what the code is doing. The problem I'm having is that when the selection critiera pulls more than 10 records from the database, the first page of results is correct per the selection criteria entered by the user on the select form. When the 'next' link is selected to review the second page of results, the query is executed again. But this time the form variables have been reset and the results now contains the entire contents of the db. The start record is set to look at the 11th instance of the results set, so the second page starts with the 11th record in the database instead of the 11th record in the original results set. The original select statement is built by determining which criteria is selected using $_POST against each form variable. How can I retain the form variables or the original select statement so the second execution of the select statement results in the same results set as the first? The other option that may be better is to retain the original results set and avoid re-executing the select statement altogether. But I don't know how to do that either. Any suggestions, code samples or adivice is much appreciated! I have this if statement with validations and mysql query Code: [Select] <?php if(isset($_POST['submit'])) { $errors = array(); $required_fields = array('menu_name', 'position', 'visible'); foreach($required_fields as $fieldname) { if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $fieldname; } } $fields_with_length = array('menu_name' => 30); foreach($fields_with_length as $fieldname => $maxlength) { if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; } } if(empty($errors)) { //Perform Update $id = mysql_prep($_GET['subj']); $menu_name = mysql_prep($_POST['menu_name']); $position = mysql_prep($_POST['position']); $visible = mysql_prep($_POST['visible']); $query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible} WHERE id = {$id}"; $result = mysql_query($query, $connection); if(mysql_affected_rows() == 1) { //success $message = "Record has been successfuly updated!"; } else { //failed $message = "The subject update failed"; $message .= "<br>". mysql_error(); } } else { //Error Occured $message = "There were ". count($errors) . " error in the form"; } } ?> I'm having problem on this line Code: [Select] <?php if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) ?> this line is for validation of my radio button which has a 0 and 1 value. Quote && $_POST[$fieldname] != 0 Eventhough the textbox is not set and is empty, it's still bypassing my if statement and still executing the update query... Can someone please tell me what i have got wrong here? I am trying to return value C if conditions a AND B are met or return value D. Not sure if its parenthesis or something else? <?php if (($row_Recordset4['multidirection']=="yes") && ($_POST["widthcheck"] < $row_Recordset4['drop'])) echo $row_Recordset4['width'];else echo $row_Recordset4['drop'] ?> Thanks this code works fine if the conditions are absolute ( pass, fail or empty). im trying to make the fail work if its part of the query. ive tried %fail% but didnt work. $choice = strtoupper($rows['comments']); if($choice == "PASS"){ $bgc = "#ff25f0" ; } elseif($choice == "FAIL"){ $bgc = "#666666" ; }else{ $bgc = "#FF6600" ; } Hi.. I tried this code: Code: [Select] <?php error_reporting(0); date_default_timezone_set("Asia/Singapore"); //set the time zone $con = mysql_connect('localhost', 'root',''); mysql_select_db("mes", $con); if (!$con) { echo 'failed'; die(); } $sql = "SELECT PCODE, total FROM kanban_checker ORDER BY PCODE"; $res = mysql_query($sql, $con) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $PCODE = $row['PCODE']; $total = $row['total']; } $sql = "SELECT P28, P28_max, P28_min, P30, P30_max, P30_min, P32, P32_max, P32_min, P33, P33_max, P33_min, P35, P35_max, P35_min, P35M, P35M_max, P35M_min, P35W, P35W_max, P35W_min, P38, P38_max, P38_min, P41, P41_max, P41_min, P42, P42_max, P42_min, P43, P43_max, P43_min, P46, P46_max, P46_min FROM parameter_settings"; $res_pcode = mysql_query($sql, $con); $row = mysql_fetch_assoc($res_pcode); $P28 = $row['P28']; $P28_max = $row['P28_max']; $P28_min = $row['P28_min']; $P30 = $row['P30']; $P30_max = $row['P30_max']; $P30_min = $row['P30_min']; $P32 = $row['P32']; $P32_max = $row['P32_max']; $P32_min = $row['P32_min']; $P33 = $row['P33']; $P33_max = $row['P33_max']; $P33_min = $row['P33_min']; $P35 = $row['P35']; $P35_max = $row['P35_max']; $P35_min = $row['P35_min']; $P35M = $row['P35M']; $P35M_max = $row['P35M_max']; $P35M_min = $row['P35M_min']; $P35W = $row['P35W']; $P35W_max = $row['P35W_max']; $P35W_min = $row['P35W_min']; $P38 = $row['P38']; $P38_max = $row['P38_max']; $P38_min = $row['P38_min']; $P41 = $row['P41']; $P41_max = $row['P41_max']; $P41_min = $row['P41_min']; $P42 = $row['P42']; $P42_max = $row['P42_max']; $P42_min = $row['P42_min']; $P43 = $row['P43']; $P43_max = $row['P43_max']; $P43_min = $row['P43_min']; $P46 = $row['P46']; $P46_max = $row['P46_max']; $P46_min = $row['P46_min']; $P47 = $row['P47']; $P47_max = $row['P47_max']; $P47_min = $row['P47_min']; if($PCODE = $P28 || $total = $P28_min){ echo $P28_min; } elseif($PCODE = $P30 || $total = $P30_min){ echo $P30_min; } elseif($PCODE = $P32 || $total = $P32_min){ echo $P32_min; } elseif($PCODE = $P33 || $total = $P33_min){ echo $P33_min; } elseif($PCODE = $P35 || $total = $P35_min){ echo $P35_min; } elseif($PCODE = $P35M || $total = $P35M_min){ echo $P35M_min; } elseif($PCODE = $P35W || $total = $P35W_min){ echo $P35W_min; } elseif($PCODE = $P38 || $total = $P38_min){ echo $P38_min; } elseif($PCODE = $P41 || $total = $P41_min){ echo $P41_min; } elseif($PCODE = $P42 || $total = $P42_min){ echo $P42_min; } elseif($PCODE = $P43 || $total = $P43_min){ echo $P43_min; } elseif($PCODE = $P46 || $total = $P46_min){ echo $P46_min; } elseif($PCODE = $P47 || $total = $P47_min){ echo $P47_min; } else{ echo ''; } ?> for display data if total = min. The problem is, in this condition he satisfied if condition even though $total is not equal to $P28_min. Thank you Code: [Select] if($this->color=="Blue" || $this->color=="Green" || $this->color=="Red" || $this->color=="Yellow") { return true; } else { return false; } This is returning false, when it should be returning true because $this->color DOES equal "Red"... I can NEVER EVER get these conditional things to work UNLESS it's &&, but I even tried: !$this->color != "Red" && ......... as well to see if that would work even though that's really confusing. Can someone help me finally understand this stupid OR stuff? Hi.. I have form for leave and now I need to add checkbox for halfday. if checkbox is checked the HOURS_LEAVE = HOURS_LEAVE * 4 because his leave is halfday only. then if checkbox is not check the HOURS_LEAVE = HOURS_LEAVE * 8 . and also if checkbox is check the value is H else W then it also save to database here is my code for LeaveEmp.php <?php include 'config.php'; date_default_timezone_set('Asia/Singapore'); session_start(); $currentEmpID = $_SESSION['empID']; $EMP_NO = $_GET['EMP_NO']; $NAME = $_GET['NAME']; $DATE_LEAVE_FROM = $_GET['DATE_LEAVE_FROM']; $DATE_LEAVE_TO = $_GET['DATE_LEAVE_TO']; $HOURS_LEAVE = $_GET['HOURS_LEAVE']; $Approve = $_GET['Approve']; $TYPE =$_GET['TYPE']; $dateprocess = $_GET['dateprocess']; $DATE_LEAVE = $_GET['DATE_LEAVE']; $EMPNO = $_GET['EMPNO']; function GetDays($StartDate, $EndDate){ $StartDate = date("Y-m-d", strtotime($StartDate)); $EndDate = date("Y-m-d", strtotime($EndDate)); $Days[] = $StartDate; $CurrentDate = $StartDate; while($CurrentDate < $EndDate){ $CurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($CurrentDate))); if(date("w",strtotime($CurrentDate))>0){ $Days[] = $CurrentDate; } } return count($Days); } $HOURS_LEAVE = GetDays($DATE_LEAVE_FROM, $DATE_LEAVE_TO); $smarty->assign('LeaveStatus', array(SickLeave=>'Sick Leave',VacationLeave=>'Vacation Leave',BirthdayLeave=>'Birthday Leave',MaternityLeave=>'Maternity Leave', PaternityLeave=>'Paternity Leave', UnionLeave=>'Union Leave', Holiday=>'Holiday')); $smarty->assign('TYPE', $TYPE); $sql = "SELECT l.EMP_NO, l.DATE_LEAVE_FROM, l.DATE_LEAVE_TO, l.HOURS_LEAVE, l.TYPE_LEAVE, l.STATUS_LEAVE FROM $ADODB_DB.employment em, $PAYROLL.leave_data l WHERE em.EMP_NO = l.EMP_NO AND em.EMP_ID = '$currentEmpID' AND l.DATE_LEAVE_FROM = '$DATE_LEAVE_FROM'"; $result_edit = $conn2->Execute($sql); $DATE_LEAVE_FROM = $result_edit->fields['DATE_LEAVE_FROM']; $DATE_LEAVE_TO = $result_edit->fields['DATE_LEAVE_TO']; $smarty->assign('DATE_LEAVE_FROM', $DATE_LEAVE_FROM); $smarty->assign('DATE_LEAVE_TO', $DATE_LEAVE_TO); $sql = "SELECT l.EMP_NO, p.EMP_ID, CONCAT(LNAME, ', ', FNAME, ' ', MI, '.') AS FULLNAME, l.DATE_LEAVE_FROM, l.DATE_LEAVE_TO, l.HOURS_LEAVE, l.TYPE_LEAVE, l.STATUS_LEAVE FROM $ADODB_DB.PERSONAL p, $ADODB_DB.employment em, $PAYROLL.leave_data l WHERE em.EMP_NO = l.EMP_NO AND p.EMP_ID = em.EMP_ID ORDER BY FULLNAME"; $rs = $conn2->GetAll($sql); $smarty->assign('getleave', $rs); $smarty->display('header_att.tpl'); $smarty->display('LeaveForm.tpl'); $smarty->display('footer.tpl'); ?> here is my code for saving leave: <?php include 'config.php'; date_default_timezone_set('Asia/Singapore'); $currentEmpID = $_SESSION["empID"]; $EMP_NO = $_GET['EMP_NO']; $NAME = $_GET['NAME']; $DATE_LEAVE_FROM = $_GET['DATE_LEAVE_FROM']; $DATE_LEAVE_TO = $_GET['DATE_LEAVE_TO']; $HOURS_LEAVE = $_GET['HOURS_LEAVE']; $TYPE =$_GET['TYPE']; $Approve = $_GET['Approve']; $dateprocess = $_GET['dateprocess']; $today = date("Y-m-d"); function GetDays($StartDate, $EndDate){ $StartDate = date("Y-m-d", strtotime($StartDate)); $EndDate = date("Y-m-d", strtotime($EndDate)); $Days[] = $StartDate; $CurrentDate = $StartDate; while($CurrentDate < $EndDate){ $CurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($CurrentDate))); if(date("w",strtotime($CurrentDate))>0){ $Days[] = $CurrentDate; } } return count($Days); } $HOURS_LEAVE = GetDays($DATE_LEAVE_FROM, $DATE_LEAVE_TO); $HOURS_LEAVE = $HOURS_LEAVE * 8; $smarty->assign('LeaveStatus', array(SickLeave=>'Sick Leave',VacationLeave=>'Vacation Leave',BirthdayLeave=>'Birthday Leave',MaternityLeave=>'Maternity Leave', PaternityLeave=>'Paternity Leave', UnionLeave=>'Union Leave', Holiday=>'Holiday')); $smarty->assign('TYPE', $TYPE); $smarty->assign('DATE_LEAVE_FROM', $DATE_LEAVE_FROM); $smarty->assign('DATE_LEAVE_TO', $DATE_LEAVE_TO); $smarty->assign('EMP_NO', $EMP_NO); $smarty->assign('NAME', $NAME); $smarty->assign('HOURS_LEAVE', $HOURS_LEAVE); $smarty->assign('TYPE', $TYPE); $smarty->assign('dateprocess', $today); $sql = "SELECT EMP_NO, DATE_LEAVE_FROM, DATE_LEAVE_TO, HOURS_LEAVE, TYPE_LEAVE, STATUS_LEAVE, DATE_PROCESS FROM leave_data WHERE EMP_NO = '$EMP_NO' AND DATE_LEAVE_FROM = '$DATE_LEAVE_FROM' AND DATE_LEAVE_TO = '$DATE_LEAVE_TO'"; $RsLeave = $conn2->Execute($sql); $numrowsRsLeave = $RsLeave->RecordCount(); if($numrowsRsLeave > 0){ echo "<script>alert('Transaction has already been process')</script>"; echo "<script>navigate('LeaveEmp.php')</script>"; } else { $sql = "SELECT EMP_NO, DATE_LEAVE_FROM, DATE_LEAVE_TO, HOURS_LEAVE, TYPE_LEAVE, STATUS_LEAVE, DATE_PROCESS FROM leave_data WHERE EMP_NO = '$EMP_NO' AND DATE_LEAVE_FROM = '$DATE_LEAVE_FROM' AND DATE_LEAVE_TO = '$DATE_LEAVE_TO'"; $RsLeave = $conn2->Execute($sql); $saverec['EMP_NO'] = $EMP_NO; $saverec['DATE_LEAVE_FROM'] = $DATE_LEAVE_FROM; $saverec['DATE_LEAVE_TO'] = $DATE_LEAVE_TO; $saverec['HOURS_LEAVE'] = $HOURS_LEAVE; $saverec['TYPE_LEAVE'] = $TYPE; $saverec['STATUS_LEAVE'] = $Approve; $saverec['DATE_PROCESS'] = $today; $insertSQL = $conn2->GetInsertSQL($RsLeave, $saverec); $conn2->Execute($insertSQL); } $sql = "SELECT EMP_NO, DATE_LEAVE_FROM, DATE_LEAVE_TO, HOURS_LEAVE, TYPE_LEAVE, STATUS_LEAVE, DATE_PROCESS FROM leave_log WHERE EMP_NO = '$EMP_NO' AND DATE_LEAVE_FROM = '$DATE_LEAVE_FROM' AND DATE_LEAVE_TO = '$DATE_LEAVE_TO'"; $RsLeaveLog = $conn2->Execute($sql); $numrowsRsLeave = $RsLeaveLog->RecordCount(); if($numrowsRsLeave > 0){ echo "<script>alert('Transaction has already been process')</script>"; echo "<script>navigate('LeaveEmp.php')</script>"; } else{ $sql = "SELECT EMP_NO, DATE_LEAVE_FROM, DATE_LEAVE_TO, HOURS_LEAVE, TYPE_LEAVE, STATUS_LEAVE, DATE_PROCESS FROM leave_log WHERE EMP_NO = '$EMP_NO' AND DATE_LEAVE_FROM = '$DATE_LEAVE_FROM' AND DATE_LEAVE_TO = '$DATE_LEAVE_TO'"; $RsLeaveLog = $conn2->Execute($sql); $saverec['EMP_NO'] = $EMP_NO; $saverec['DATE_LEAVE_FROM'] = $DATE_LEAVE_FROM; $saverec['DATE_LEAVE_TO'] = $DATE_LEAVE_TO; $saverec['HOURS_LEAVE'] = $HOURS_LEAVE; $saverec['TYPE_LEAVE'] = $TYPE; $saverec['STATUS_LEAVE'] = $Approve; $saverec['DATE_PROCESS'] = $today; $insertSQL = $conn2->GetInsertSQL($RsLeaveLog, $saverec); $conn2->Execute($insertSQL); } session_write_close(); header("Location:LeaveEmp.php"); exit(); ?> And here is the LeaveForm.tpl <script> window.onload = function() { var ul = document.getElementById('searchpayroll'); var links = ul.getElementsByTagName('a'); var i = 0; document.onkeyup = function(e){ e = window.event || e; var key = e.charCode || e.keyCode; if (key == 40) { if (i < links.length - 1) i++; links[i].focus(); } else if (key == 38) { if (i > 0) i--; links[i].focus(); } } } function ApproveLeaveOP(){ var EMP_NO = document.getElementById('EMP_NO').value; var DATE_LEAVE_FROM = document.getElementById('DATE_LEAVE_FROM').value; var DATE_LEAVE_TO = document.getElementById('DATE_LEAVE_TO').value; var NAME = document.getElementById('NAME').value; var HOURS_LEAVE = document.getElementById('HOURS_LEAVE').value; var Approve = document.getElementById('Approve').value; var TYPE = document.getElementById('TYPE').value; var dateprocess = document.getElementById('dateprocess').value; var H = document.getElementById('H').value; document.sampleform.action="AddLeave.php?EMP_NO="+EMP_NO+"&DATE_LEAVE_FROM="+DATE_LEAVE_FROM+"&DATE_LEAVE_TO="+DATE_LEAVE_TO+"&NAME="+NAME+"&HOURS_LEAVE="+HOURS_LEAVE+"&Approve="+Approve+"&TYPE="+TYPE+"&dateprocess="+dateprocess+"&H="+H; document.sampleform.submit(); } </script> <script type="text/javascript" src="op_from_date_leave.js"> </script> <script type="text/javascript" src="op_from_date_leave_conf2.js"></script> <div id="Search"> <form> <p class="serif"><b>Search Lastname:</b></p> <input type="text" name="search_" size="20" onkeyup="searchEmpLeaveOP(this.value);"> <div> <hr /> <ul id="searchpayroll" style="overflow:auto; height:380px; width:100%; margin-left:2px;"> {section name=co_emp loop=$personalAll} <li><a href="LeaveData.php?queryEmpID={$personalAll[co_emp].EMP_ID}">{$personalAll[co_emp].FULLNAME}</a></li> <hr /> {sectionelse} <li>No records found</li> {/section} </ul> </div> <div id="EmpOTForm"> <fieldset> <legend>Overtime and Leave Form</legend> <input type="button" name="OTbtn" value="Overtime" onclick="OtFrmOP()")> <input type="button" name="Leavebtn" value="Leave" onClick="LeaveOP()"> <p class="serif"> <label id="LeaveEmpIDLabel">EMP ID</label><label id="LeaveNameLabel">NAME</label><label id="FromLeave"><a href="Javascript:showCal('RegCalendar1')">FROM DATE </a></label><label id="DaysLabel"><a href="Javascript:showCal('RegCalendar2')">TO DATE</a></label><label id="TypeLeave">LEAVE TYPE</label><label id="HLeave">H</label> <input type="text" name="EMP_NO" id="EMP_NO" value="{$empno}" size="8" > <input type="text" name="NAME" id="NAME" value="{$fullname}" size="35" > <input type="text" name="DATE_LEAVE_FROM" id="DATE_LEAVE_FROM" value="{$DATE_LEAVE_FROM}" size="9"> <input type="text" name="DATE_LEAVE_TO" id="DATE_LEAVE_TO" value="{$DATE_LEAVE_TO}" size="9"> {html_options id=TYPE name=TYPE options=$LeaveStatus selected=$TYPE style="width:140px;"} <input type="checkbox" name="H" id="H" value=""> <input type="button" name="Approve" value="Approve" id="Approve" onclick="ApproveLeaveOP()"> <input type="button" name="Edit" value="Edit" id="Edit" onclick="EditLeaveOP()"> <input type="hidden" name="dateprocess" id="dateprocess" value="{$dateprocess}"> </p> <input type="hidden" id="HOURS_LEAVE"> </fieldset> <div> <p> <table border="1" class="stat" width="auto"> <tr> <th>EMP ID</th> <th>NAME</th> <th>FROM DATE</th> <th>TO DATE</th> <th># of HOURS</th> <th>TYPE</th> <th>STATUS</th> </tr> {section name=att loop=$getleave} <tr> <td id = "EMPNO">{$getleave[att].EMP_NO}</td> <td >{$getleave[att].FULLNAME}</td> <td id = "DATE_LEAVE" onclick="opleave_edit('{$getleave[att].DATE_LEAVE_FROM}')">{$getleave[att].DATE_LEAVE_FROM}</td> <td >{$getleave[att].DATE_LEAVE_TO}</td> <td >{$getleave[att].HOURS_LEAVE}</td> <td >{$getleave[att].TYPE_LEAVE}</td> <td >{$getleave[att].STATUS_LEAVE}</td> </tr> {/section} </table> </p> </form> </div> </div> </div> I attach the sample form. Thank you |