PHP - How Do You Get An Unspecified Amount Of Inputs To Sort?
I am working on php script that takes an input (using html) stores those values in a separate text file. I managed to get that to work. But the thing is I wanted to be able to sort those values alphabetically. Since their are an unspecified amount of inputs I tried using an explode on my output in order to store those values as an array so that I can sort them. Problem is explode makes each input its own separate array instead of one whole array. I figure some type of loop is needed to fix that but I can't figure out how to go about creating it [I'm pretty new to php]. Any advice? Heres my code for my PHP script:
$filename = "ip_collection.txt"; $fileHandle = fopen('ip_collection.txt', 'a+') OR die("can't open file\n"); $ip = $_SERVER['REMOTE_ADDR']; $name = stripslashes($_POST['lastName'] . ", " . $_POST['firstName']); $textFromForm = $name. " | " . $ip; $newtext = $textFromFile . $textFromForm; $output = explode("\n",$newtext); sort($output); foreach($output as $key => $value) { fwrite($fileHandle, $value."\n"); } fclose($fileHandle); Similar TutorialsHeres what i got... function printLB1 (){ $result = mysql_query("SELECT * FROM leaderboards ORDER BY CollegeFootballPoints DESC"); while ($row = mysql_fetch_object($result)) { $leaderboard[] = $row->Username; $leaderboardPoints[] = $row->CollegeFootballPoints; } $num = mysql_num_rows($reault); //I know from here to ................... needs to be in a var or echo or something. <tr> <td>1.</td> //This will auto increment too like i++ but i cna do that myself! <td>echo $leaderboard;</td> <td>echo $leaderboardPoints;</td> </tr> //Here......................................................... } I need to pull a table row per user. But i want to somehow do it once in a function and then ill echo the function into a table after the php stuff is done. Like i want to pull every a table row per user in the function. then display the function below that way i don't have to write a whole extra query and table row per person. If you understand please help if not please let me know where i can explain more. Thanks Hi 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????? How is it possible, in PHP, to display an error message next to a form input text field if a user is attempting to submit a form with empty fields? Moreover, how is it possible to remove the same error message when the user fills in the input field with the required data and/or refreshes the page? I have 2 files that I am working with: application.php and process.php.
application.php mainly has the HTML of the form. I am very new to learning PHP (I started learning it last week) and I have been searching for hours for a clear answer. I have already tried different methods for generating the error message including: using empty(), !isset, $_POST["name"] = "", etc, and have tried using session_start();, $row, echo, print, and other variables to try and display error message on the page, and I have tried using unset();, and = null, to try and remove the error message once the input field has been filled but all to no avail. Either the method I try only half works, or nothing works, and I cannot pinpoint which is the part that is not working. I only have 2 files to work with, and as an example of what I want to do is:
1. If the first name field is empty when the user clicks submit, an error message should appear next to the input. Is this possible with PHP? Error: Code: [Select] No error, Im have trouble Having This Loop Select Only 2 Uploads At a Time. Code: Code: [Select] <?php //porperties $result = mysql_query("SELECT * FROM uploaded"); while($r=mysql_fetch_array($result)) { $link=$r["link"]; $name=$r["name"]; $type=$r["type"]; $size=$r["size"]; $date=$r["date"]; $id=$r["id"]; echo " The File $name Was Currently Updated<br> Size: $size<br> Type: $type<br> Date Updated: $date<br> Link: <a href='$link'>Download</a><br><br> "; } ?> Couldn't Think of a Good name For this,Sorry Code: [Select] ${$payment['amount']} using the $ next to it doesn't work I am using itinside double quotes, any way I can get that dollar sign right next to it? This for people buying stuff off my forum i want the amount to show right by the $ dollar sign And im working inside double quotes only Hi All, Im new to this forum but think its going to be a regular location for me for quite a while. Im very very new to php and the guy that was doing code for me is not responding to emails, so i thought id have a go myself...lol at the moment im going through the trial and error phase while learning. I have edited some of the code he has put on the site to create a new look. the original page: http://www.ridersguide.co.uk/county_accommodation.php?county=4; the one im trying:http://www.ridersguide.co.uk/county_accommodation2.php?county=4; The new bit is to add a preview of the text for the page. echo $row[ad_text]; the code above brings up all the text but i only want to show the first 4 or 5 lines. Is this possible in php? Any help appreciated Neil If I knew what to call it I could have searched it. So I will just explain. I have a form that is pulling from a database to build the following. echo <input name='$row[id]' type='text' id='$row[id]' size=2>; This will build a page that has this <input name='22' type='text' id='22' size=2> <input name='54' type='text' id='54' size=2> This part works great. The problem is when I try to add it to the database. How can I insert variables that I don't know the names of? They will be something like $_POST[22] $_POST[54] and so on. I hope someone can understand my jibberish. I am sure there is a WAY better way to do this. Hi, I have a database where I store times with the Now(); function. What I want is: When the viewer looks at the post, instead of it saying: Posted: 2010-11-14 19:08:14, I want it to say posted: x hours ago, Posted: x days ago, etc. Any help with this would be appreciated! Okay, I have a healing spell that allows people to regain stats when it's cast. But, how do I limit it so that they can't go over their maxhp? Here's what I have so far: $db->execute("UPDATE `users` SET `hp`=?, `mp`=?, `energy`=? WHERE `id`=?", array($player->hp + $healspell, $player->mp - $spell['mp'], $player->energy - $spell['energy'], $player->id)); What do I need to do in this to tell it to limit it to the maxhp but never going over it? $healspell is a random number, so it is quite possible to go over the maxhp at the moment. Thank you! Hey quick question on my homepage there is a featured content section, which is populated from my DB some of the posts are really long so I would like to limit the amount of text. how can I do this? also is there a way to make sure if does not cut a word in half? thanks I have a field in my database called description. How can I echo out only 500 out of the 2000 max characters? Like for example: Code: [Select] $query = mysql_query("SELECT description FROM `servers` ORDER BY votes DESC LIMIT 5"); If I were to echo out the value of description for the database, how can I only echo out the first 500 characters? Does anyone have any idea why my inputs shrink (to default height and weight) after i press submit button? I use css width and height for inputs. All other settings (border, background color etc.) remain like they should. For form submit i use PHP self function where i check if submit button has been pressed and then perform the form action. Thank you Hey Guys, I'm looking to echo a large amount of complicated text that includes a lot of symbols, such as this: Code: [Select] V\"`'8\"`%n\"`)*$6A`!K!`2D\"`-t\"`!8\"`,?$`!1#D`\"i!`&2%` 9!`$S\"`#C)`# \"`\">\"7`)Q$`+Y\"C`!B!`%2\"`(e\"`($-`&M(`&C#F`$Q$`-P(`$P\"`#Q-`\"A\"`$CA`1e1` 5(`2GC0%2`&x!`'\"#`,i$`%D\"77`\"W!`%K!`$A.`/q+`(V\"`!J\"`,/\"`//%6E`$:#`.%)`\"u!`.d&`,K$`&F#`#V%`!z%`#2\"` &\"`&0!`3.&`2V%`)#+`\"t\"`.,\"` E!`(m&`3P(7`%l*`#q\"`-Q!`(#\"``1)\"` kC`#P+`$I2`-#\"`3<66`0H!`,|&6`#d#`$}. Can anyone shed some light as to how I would be able to echo the above without having to backlash all the clashing symbols and preferably without having to include an external file? All your help is greatly appreciated. Extreme. Hi, I have a function where I want to sort an an array of this type: 0 = {'fruit' => 'banana', 'color' => 'yellow', 'age' => '1', 'weight' => '2', 'brand' => 'dole', 'store' => shaws'} 1 = {'fruit' => 'apple', 'color' => 'red', 'age' => '3', 'weight' => '5', 'brand' => 'fuji', 'store' => 'foodmaster'} etc etc. I then want to array_multisort this array with different parameters. One way is to sort by fruit, then color, then brand. The second way by age, then store. Code: [Select] //This is how I sort it for my first way //Let's call the array $rows function sort($rows) { foreach($rows as $key => $value) { $fruit[$key] = $value['fruit']; $color[$key] = $value['color']; $brand[$key] = $value['brand']; } array_multisort($fruit, SORT_ASC, $color, SORT_ASC, $brand, SORT_ASC $rows); } I want to try to reuse this function by passing in the keys that I want to sort with. Like doing sort($rows, array('fruit', 'color', 'brand')), then sort($rows, array('age', 'store')). Is this possible? Hi guys, Im a newb here, so bear with me! Still learning the ropes. I want to delete all files from my server that are 6 hours old since being uploaded to the site. I believe i need a cron job? I currently have this script in its framework... Code: [Select] <?php //time interval for deletion to occur... $x = 30; //timestamp $current_time = time(); //the file you wish to delete $file_name = 'file.txt'; //timestamp $file_creation_time = filemtime($filename); //extract difference $difference = $current_time - $file_creation_time; //if difference = $x...then delete file if ($difference == $x) { unlink($file_name); } Would that work automatically or would it need a cron to run it? And how could i test it, that bit worries me. Many thanks. if i want to display upcoming events and only want to show the top 3? Hi, I'm trying to code a page which displays the amount of users currently viewing a page. But how do I decrease the count if a user closes the page? Thanks So I wrote this piece of code that for the most part works. The problem is two things. One if I print out say $dir[$i] I get 7 directories back. if I try and store that in an array to later be access by some other function I get up to 24+ C's instead of C:/Path... Code: [Select] public function aisis_get_dir_array($dir){ if(!is_array($dir)){ aisis_get_dir($dir); return; } $count = count($dir); for($i = 0; $i<$count; $i++){ if(!is_dir($dir[$i])){ _e('Specified dir' . $dir[$i] . 'is not a directory.'); } $this->directory_list = $dir[$i]; //store these $handler = opendir($dir[$i]); while($file = readdir($handler)){ if($file != "." && $file != ".."){ $this->files[] = $file; } } } return $this->files; } right under the line: $this->directory_list = $dir[$i]; //store these if You put in echo $dir[$i] you get back directories. How ever down in this function: Code: [Select] public function load_if_extentsion_is_php($dir, $array = false){ $list = array(); $list_dir = array(); if($array == true){ $list = $this->aisis_get_dir_array($dir); $list_dir = $this->directory_list; } else{ $list = $this->aisis_get_dir($dir); } $count = count($list); for($i = 0; $i<$count; $i++){ if($array == true){ $countdir = count($this->directory_list); for($j = 0; $j<$countdir; $j++){ echo $list_dir[$j]; //echo $list_dir[$j]; //echo $list[$i]; } } //if(substr(strrchr($list[$i],'.'),1)=="php"){ //echo $list[$i]; //require_once($dir . $list[$i]); //} } } echoing out: echo $list_dir[$j]; gets you up to 24+ C's instead of the directories that are stored. You can view the whole class at Paste bin - there is an expiry: 12 hours. Essentially you call load_if_extentsion_is_php($dir, $array = false) pass it an array of directories and set the array part to true. To Summarize: If you pass in an array of directories you should get back: a echoed list of directories a echoed list of php files belonging in those directories. Currently you get the later and a series of C's for the first. Why? Hi, i have a folder with thumbnails and original big pictures of the thumbnails, i wanna make it so that the max amount of thumbs that can be displayed on one page would be 3 Code: [Select] <body> <div id=head></div> <div id=main> <?php include("loadimages.php") ?> //<- here i take all the thumbnails that are in my thumb folder </div> <div id=foot></div> </body> i have 5 thumbnails, it displays all of them in the "main" div, i wanna add an "Next" and "Previous" buttons, i mean, i want it to show only 3 thumbs at one time, once u click "next" it would remove the first 3 thumbs and load the other 2 thumbnails.. heres a pic maybe it will explain better of what im trying to do.. II have been told that I should sanitize my inputs, what does that mean? Isn't that what trim does? |