PHP - What's The Proper Way To Do "sort By" Selection Options?
I am trying to replicate a "Sort By" selection exactly like how Freelancer has it here https://www.freelancer.com/search/projects/ Sort by - Oldest - Newest - Lowest Price - Highest Price - Lowest Bid - Highest Bid So far I was only able to do the sort by oldest and newest. Here is my code. This shows my code for the page to retrieve records and use pagination to split them. My Tables: 1. Projects (project_id, title, description, min_budget, max_budget, posted_date, expired_date, status) 2. Bids (bid_id, project_id, details, posted_date, b_status) 3. Images (image_id, project_id, user_id, image_path, posted_date) 4. Currency (currency_id, currency_name) 5. Users (user_id, username)
As you can see the setup I have works only for sorting by DESC and ASC, which is used for Oldest and Newest. Now I would like to know how I can do the same thing for lowest price, highest price using the same query. Lowest and Highest Bid have a separate table. i was wondering if that could also be sorted in the same query as well? <div id="sort-by-wrap"> <select id="sort-select" onchange="if(this.value != '') document.location = 'explore-projects?p=3&c1=<?php echo $url_c1; ?>&category=<?php echo $url_category; ?>&name=<?php echo $url_name; ?>&sort_by=' + this.value"> <option value="DESC"<?php if(isset($_GET['sort_by']) && $_GET['sort_by'] == 'DESC') {echo 'selected="selected"';} ?>>Newest</option> <option value="ASC"<?php if(isset($_GET['sort_by']) && $_GET['sort_by'] == 'ASC') {echo 'selected="selected"';} ?>>Oldest</option> </select> <div id="sort-txt">Sort by:</div> </div> <?php $url_category = trim(!empty($_GET['category']) ? $_GET['category'] : null); $url_name = trim(!empty($_GET['name']) ? $_GET['name'] : null); $url_sort_by = trim(!empty($_GET['sort_by']) ? $_GET['sort_by'] : null); $url_c1 = trim(!empty($_GET['c1']) ? $_GET['c1'] : null); $url_page_no = trim(!empty($_GET['page_no']) ? $_GET['page_no'] : null); $page_url = basename($_SERVER['REQUEST_URI']); $get_page_url = preg_replace('/[?&]page_no=\d+/', '', $page_url); if(!empty($url_page_no)) { $page_no = $url_page_no; } else { $page_no = 1; } $total_records_per_page = 10; $offset = ($page_no - 1) * $total_records_per_page; $previous_page = $page_no - 1; $next_page = $page_no + 1; $count_records = $db->prepare("SELECT COUNT(*) FROM projects WHERE category_id = :category_id AND publish = :publish"); $count_records->bindParam(':category_id', $url_category); $count_records->bindValue(':publish', 1); $count_records->execute(); $total_records = $count_records->fetchColumn(); $total_no_of_pages = ceil($total_records / $total_records_per_page); $second_last = $total_no_of_pages - 1; // total page minus 1 $find_records = $db->prepare("SELECT images.image_id, currency.currency_name, users.username, users.user_id, projects.* FROM projects LEFT JOIN images ON projects.project_id = images.project_id LEFT JOIN currency ON projects.currency_id = currency.currency_id LEFT JOIN users ON projects.user_id = users.user_id WHERE projects.category_id = :category_id AND projects.publish = :publish ORDER BY projects.project_id $url_sort_by LIMIT $offset, $total_records_per_page"); $find_records->bindParam(':category_id', $url_category); $find_records->bindValue(':publish', 1); $find_records->execute(); $result_records = $find_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0) { foreach($result_records as $row) { $project_id = $row['project_id']; $title = $row['title']; $description = $row['description']; $min_budget = $row['min_budget']); $max_budget = $row['max_budget']); $posted_date = $row['posted_date']; $expired_date = $row['expired_date']; $status = $row['status']; $image_id = $row['image_id']; $currency_name = $row['currency_name']; $user_id = $row['user_id']; $username = $row['username']; // COUNT TOTAL BIDS FOR EACH PROJECT $count_bids = $db->prepare("SELECT COUNT(*) FROM bids WHERE project_id = :project_id"); $count_bids->bindParam(':project_id', $project_id); $count_bids->execute(); $total_bids = $count_bids->fetchColumn(); // HTML CODE TO OUTPUT EACH PROJECT RESULT include '../snippets/record-box-wrap.php'; } } else { ?> <div id="no-content"> <span>No projects</span> </div> <?php } ?> <div class="pagination-wrap"> <div class="inner-pagination"> <ul> <li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>> <a <?php if($page_no > 1){ echo "href='$get_page_url&page_no=$previous_page'"; } ?>>Prev</a> </li> <?php if($page_no <= 6) { for($counter = 1; $counter < 6; $counter++) { if ($counter == $page_no) { echo "<li class='active'><a>$counter</a></li>"; } else { echo "<li><a href='$get_page_url&page_no=$counter'>$counter</a></li>"; } } } else { for($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) { if($counter == $page_no) { echo "<li class='active'><a>$counter</a></li>"; } else { echo "<li><a href='$get_page_url&page_no=$counter'>$counter</a></li>"; } } } ?> <li <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>> <a <?php if($page_no < $total_no_of_pages) { echo "href='$get_page_url&page_no=$next_page'"; } ?>>Next</a> </li> <?php if($page_no < $total_no_of_pages) { echo "<li><a href='$get_page_url&page_no=$total_no_of_pages'>Last ››</a></li>"; } ?> </ul> </div> </div>
Similar TutorialsCan someone please help me with an array problem i can not figure out. I need the array to be numbered from 1 to how ever many fields that are needed in the form and have a mysql field name and the title of the field also in the array. 1, "mysql_field_name", "Title of form field" 2, "", "" and so on then the form will be shown based on the array. I have the following draft code which I am working with. any suggestions on how i may do this array ? Code: [Select] <?php $options = array( '1'=> array('fieldtext'=>'option1', 'mysqlfield'=>'option1'), '2'=> array('fieldtext'=>'option2', 'mysqlfield'=>'option2'), '3'=> array('fieldtext'=>'option3', 'mysqlfield'=>'option3'), '4'=> array('fieldtext'=>'option4', 'mysqlfield'=>'option4'), ); // $options = array(1 => "option1", "option2", "option3", "option4"); // the line above works but i want to include the name of the mysql field as well. $userid = 1; ?> <div style="align: center; margin: 12px; font-family:Tahoma;"> <br><br><?php if ($_POST['Update'] != "Update") { // check if form submitted yet, if not get data from mysql. $res = db_query("SELECT * FROM `users` WHERE `userid` = '" . $userid . "'"); foreach($options as $key => $value) { $_POST[$key] = mysql_result($res, 0, $value); } $ok_to_update = "no"; } elseif ($_POST['Update'] == "Update") { // check if form submitted yet, if so get POST data. // error checking // foreach($options as $key => $value) { // $_POST[$i] = ""; // } $ok_to_update = "yes"; } if ($_POST['Update'] == "Update" && $ok_to_update == "yes") { // $res = db_query("INSERT INTO `users` () VALUES ()"); // add user details to database. ?><p><br><br><br>Thank you for updating</p><?php } else { ?><form name="form1" method="post" action=""> <?php foreach($options as $key => $value) { ?><p><?php echo($value); ?>: <input type="text" name="<?php echo($key); ?>" value="<?php echo($_POST[$key]);?>"></p> <?php } ?> <input name="Update" type="submit" value="Update"> </form> <?php } ?> </div> Hi, bit stuck on how to find and replace "<" and ">" with "<" and ">". I basically have a database record that outputs to screen and I need the code in the <code> tags to be rendered to the screen. I therefore need it to go through the whole array variable from the db and change the symbols just inside the code tags. Please be aware that the code tags might happen more than once here's an example below Code: [Select] <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1> </code> <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1> </code> the desired output would be: <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1 </code> <p>blah blah blah</p> <p>blah blah blah</p> <p>blah blah blah</p> <code> <h1>hello</h1 </code> help on this would be great Cheers Rob Ok i'm trying to make a dropdown menu to sort content on my site. The dropdown looks like this: Code: [Select] <select> <option selected="selected">Date</option> <option>Clicks</option> <option>Ratings</option> <option>Comments</option> </select> The content that the dropdown needs to sort are taken from my MySQL database and put on the site using these lines of code: (there is alot more code to do other stuff aswell, but that isn't importand for my problem) $sql = "SELECT * FROM links LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $dyn_table = '<table class="main_table">'; while ($row = mysql_fetch_assoc($result)) { etc... The dropdown and the php script that inserts the content on the page are (and needs to be) in two seperate files. So how can i make so that when a user selects "Sort by: Clicks" for example, the page refresh and maybe inserts "ORDER BY clicks ASC" into "$sql = "SELECT * FROM links LIMIT $offset, $rowsperpage";"or something?? I'm really open for ideas here, Javascript and jQuery is also welcome if it needs to be used! Hi everyone: Can someone show me an example of the proper way to request an id, and then use "If Then Else" statements to display data depending on the id selected. I am use to doing this with ASP, but never with PHP. I have found so many different examples on GOOGLE, but would like some input from this forum. That always helps me. I have a page roster.php: Code: [Select] <html> ... <a href="players.php?PlayerID=1">Player 1</a><br /> <a href="players.php?PlayerID=2">Player 1</a><br /> <a href="players.php?PlayerID=3">Player 1</a><br /> <a href="players.php?PlayerID=4">Player 1</a><br /> ... </html> Then it goes to players.php: Code: [Select] <?php $myvar = $_REQUEST['PlayerID']; echo $myvar; ?> IS THE ABOVE THE PROPER WAY TO REQUEST AN ID? <html> ... <% If PlayerID=1 Then %> PLAYER 1 PHOTO AND BIO <% ElseIf PlayerID=2 Then %> PLAYER 2 PHOTO AND BIO <% ElseIf PlayerID=3 Then %> PLAYER 3 PHOTO AND BIO <% Else %> PLAYER 4 PHOTO AND BIO <% End If %> ... </html> I would write it somewhat like that with ASP. How do I do this with PHP? Thanks! I've never done salting before. Usually just use md5, but the rainbow tables kinda scare me. Anyway, I read up a little on salting and does this look right?..right meaning effective Also, my salt string, is it case sensitive when I crypt it? <?php if (isset($_POST['submit'])) { $salt = "someStringThatonlYYiWillKnow!"; $username = stripslashes(mysql_real_escape_string(strtolower($_POST['username']))); $password = stripslashes(mysql_real_escape_string(md5($_POST['password']))); $ePass = crypt($password,$salt); $query = mysql_query("SELECT active,username,password FROM usernames WHERE username = '" . $username . "' AND password = '" . $ePass . "'") or die(mysql_error()); $num = mysql_num_rows($query); if ($num == 1) { $result = mysql_fetch_array($query); if ($result['active'] == 'yes') { $_SESSION['user']['username'] = $result['username']; $_SESSION['user']['authed'] = true; header("Location: main.php"); } else { $error = " <div class=\"notification information\"> <div>We located your account, however, it has not been activated by an admin yet.</div> </div>"; } } else { $error = " <div class=\"notification error\"> <div>Invalid Username / Password.</div> </div>"; } } ?> I have a varchar field called date that looks like this "2/06/2011 TUE" or like this "10/05/2011 WED". When I do an Order BY date Desc in php it does Not sort properly. Thanks for your assistance Steve Hi guys I am a newbie with PHP and I have been trying to solve this problem for 3 days.. Ive set up a booking form on my website to be sent directly to my email once the client clicked on the send button.. The problem I am having while checking these pages on wamp is that the booking form is ok but when I click on the send button the following message error appears "Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\fluffy_paws\booking_form2.php on line 27" When I check online I have this message "500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed." Are my codes wrong??? My webhosting is Blacknight.com , Im on Windows Vista, my internet is a dongle with O2 ireland Thanks for your help! Hi guys I'm struggling a bit, I need to replace a word that occurs multiple times in text with an array("up","down","forward","backwards") of words. $find = "left"; $replace = array("up","down","forward","backwards"); $text = "left left left left"; echo str_replace($find,$replace,$text); The Output is: array array array array Did try this with a foreach statement as well, but no luck. Is there a better way of doing this? Thanks cant work out this mysql syntax error "operation":"medupdate","medid":"","name":"ibo","medyear":"5","medmonth":"21","medday":"1","recuser":1,"SuccFail":"fail","SuccFailMessage":"error occured You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''WHERE med_id=' at line 2","ResultData":""} Code: [Select] $sql="update metodology set med_description='".$req['name']."', med_year='".$req['medyear']."', med_month='".$req['medmonth']."', med_day='".$req['medday']."', med_recorddate= now(), med_recorduserid= 1'"; $sql.= " WHERE med_id=".$req['medid']; I am getting the following error when using composer: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? Just started. I am pretty sure composer.json wasn't changed. journalctl doesn't show anything. Maybe Doctrine related, however, nothing seems to help.
Any ideas? [michael@devserver www]$ php -v PHP 7.3.4 (cli) (built: Apr 2 2019 13:48:50) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies with DBG v9.1.9, (C) 2000,2018, by Dmitri Dmitrienko [michael@devserver www]$ composer -V Composer version 1.4.2 2017-05-17 08:17:52 [michael@devserver www]$ yum info composer Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.us.oneandone.net * epel: mirror.rnet.missouri.edu * extras: mirror.us.oneandone.net * remi-php73: mirror.bebout.net * remi-safe: mirror.bebout.net * updates: mirror.us.oneandone.net Installed Packages Name : composer Arch : noarch Version : 1.8.4 Release : 1.el7 Size : 1.8 M Repo : installed From repo : epel Summary : Dependency Manager for PHP URL : https://getcomposer.org/ License : MIT Description : Composer helps you declare, manage and install dependencies of PHP projects, : ensuring you have the right stack everywhere. : : Documentation: https://getcomposer.org/doc/ [michael@devserver www]$ composer update Loading composer repositories with package information Updating dependencies (including require-dev) [ErrorException] "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? update [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--lock] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [--with-dependencies] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [-i|--interactive] [--root-reqs] [--] [<packages>]... [michael@devserver www]$
This topic has been moved to Linux. http://www.phpfreaks.com/forums/index.php?topic=318175.0 This topic has been moved to PHP Installation & Configuration. http://www.phpfreaks.com/forums/index.php?topic=347922.0 I have a registration form on my website and after Registration I want the user to activate first his account. I don't have problem on sending this email to the user. What I want to appear is like this.. GoodDay "username", Blah Blah! how can you actually do that? I know that you can do that through session variables but the thing is email's can't accept scripts on sending emails. how can I get the value from a session variable for example and convert it to plain html text so that i can OUTPUT it to the email.. HELP PLEASE! Hi, I currently have an if, elseif, else program that starts off with Code: [Select] if( $v_name == "" || $v_msg == "" ) echo "something" how do I turn this into a switch? but the very next elseif I have Code: [Select] elseif( strcspn( $_REQUEST['msg'], '0123456789' ) == strlen( $_REQUEST['msg'] ) ) echo "something" is it possible to turn this into a switch with 2 different strings like that to evaluate? Hey guys! I have the following doubt: I have a php file that does the following alongside with other code not displayed he $fp = fopen("emails.txt", "w"); It opens a txt file called emails.txt that is in the same folder where the php is saved but how do I tell $fp = fopen("/folderbefore/emails.txt", "w"); ?? In other words, how do I write the path on the script that the emails.txt is located in a previous folder where the php file is located?? Thanks in advance for your help Cheers! hello, I was wondering what is the point of writing for a dropdown: <?php if ($_POST[inputfieldID] == "value") { echo "selected"; } ?> or for a checkbox/ radio button: <?php if ($_POST['cb/radioID'] == "value") { echo "checked"; } ?> I guess it's for after submit, if there is any error, the values filled will still be echoed in the inputfield, or am i wrong? But then if that is true, i have a php search box where i use AJAX object to to send all the input they filled in after submit. But after pushing submit the inputted values remain. I guess when using AJAX to send the information i don't need <?php if ($_POST[inputfieldID] == "value") { echo "selected"; } ?> in my html forms? Little confused Can maybe somebody bring clearness Hi all, Using a form with check boxes divided into different categories, I collect data selected by the user. I now want to combine each choice in each category with each choice in the other categories and list the combinations to the user. If the user chooses one or more options in each category, it is relatively easy to nest foreach loops, since the number of categories will always be the same. For example, if the form contains three categories of options, the code will be: Code: [Select] foreach ($form['cat_1'] as $k1 => $val1) { foreach ($form['cat_2'] as $k2 => $val2) { foreach ($form['cat_3'] as $k3 => $val3) { echo $val1.' _ '.$val2.' _ '.$val3.'<br/>'; } } } Things get complicated if the user does not select options within a given category. More generally, I want to be able to generate the list regardless of the number of categories in which the user selects options. Thank you in advance for your advice. I'm a very new PHP user (as in, I just started today) and need to make a set of navigation buttons which will take the user to "next" or "previous" pages. I assume that the $_GET function is involved, but I don't know how to do it. It needs to work like this: If the URL looks like "http://website.com/page1.php" I need the "next" button (a href image) to change it to "/page2.php", and while on page 2, for the "previous" button to take the user to "/page1.php" and so forth. How can I cause the page number to increment or decrease respective to the href image being clicked? I've tried everything I could think of to resolve this issue and just can't solve it. I created a CMS, but I get this error when I add a user that has been previously deleted. For example, I create a user with username "Alicia", then I no longer need that username so I delete it. Then later I get another person named alicia, but when I try to add a user again with username Alicia, I get this error. This only happens when the username has been previously deleted. I don't have the username field set as unique in the database and it is also not set as a key. Any ideas? In the following code the sign "\" is used before "/index.php?....". What does this sign mean in a link statement? Does it mean it automatically adds the url of the webfolder or phpfile? Code: [Select] <?php echo "<a href=\"/index.php?sid=".$name."&question=".$nextquest."\">next</a>"; ?> |