Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,186 members, 7,953,672 topics. Date: Thursday, 19 September 2024 at 10:27 PM |
Nairaland Forum / Science/Technology / Programming / Please Help With This Simple Code (1065 Views)
Help A Java Newbie With This Simple Java Program / Please Help With Java Code For This Simple App.. / Simple Code Challenge: C#, Java, C, C++ (2) (3) (4)
Please Help With This Simple Code by yinkay007: 11:10am On Oct 08, 2014 |
Please I need a simple Web application that takes two integers between 1 and 20 from a user through an interface. When the user submits the form, JavaScript should be used to verify that each of the inputs from the user is not less than 1 and not greater than 20 and that input 2 is not less than input 1. Also array of 21 characters in your server side script should be Created. Finally Display all characters in the array that their positions fall in between the range of inputs from the user. |
Re: Please Help With This Simple Code by yinkay007: 11:13am On Oct 08, 2014 |
PHP code Preferreed Please |
Re: Please Help With This Simple Code by Nobody: 11:49am On Oct 08, 2014 |
read about 'regular expressions' in php or javascript. Its shortly called regex. It wouldnt take more than one line. I have used that before |
Re: Please Help With This Simple Code by Nobody: 12:31pm On Oct 08, 2014 |
PHP CODE: note that your form input is what i refer to as '$data' <php if (preg_match('/^[0-9] + $/' , $data) && $data < 21 ) { // do what you want to do here } ?> or if your intention is to prevent a zero too from beginning the number use this instead: <php if (preg_match('/^[1-9][0-9] * $/' , $data) && $data < 21 ) { // do what you want to do here } ?> |
Re: Please Help With This Simple Code by Nobody: 1:02pm On Oct 08, 2014 |
<php /* note that your first array entry is blank because that first entry is in zero position ie item[0] and ur user is not expected to enter 0. */ extract($_POST); function random_select($num){ $item= array( '', 'ipad', 'blackberryZ10', 'iphone 6', 'generator', 'coke drink', 'Pepsi drink', 'wall clock', 'black hat', 'violet suit', 'orange drink', 'lemon fruit', 'brown pillow', 'chocolate tea', 'pink bag', 'ashes and dust', 'golden shoes', 'silver chain', 'tomato sauce', 'onion', 'gutter water', 'blablabla' ); return 'you won '.$item[$num]; } if (preg_match('/^[0-9] + $/' , $data) && $data < 21 ) { /* optional: if you don't want the number to directly match the result in all cases */ $data= ceil(($data + rand(1,20))/2); /* remove the above line if u want it to match exactly in all cases */ random_select($data); // do other stuff here } else { echo 'Invalid selection! Please pick a number between 1 and 20'; } ?> |
Re: Please Help With This Simple Code by techwizard(m): 1:59pm On Oct 08, 2014 |
<?php $input = $input if ($input < 1 || $input > 20 ) { echo "number range exceeded or below expected" } ?> |
Re: Please Help With This Simple Code by yinkay007: 3:26pm On Oct 08, 2014 |
dexmundo: Thanks Brother for the code . Really Appreciate. But what i do need is from the array of 1 to 20. If the user select 3 to like 9. Program should display all the content of d array within that range. Like from the fruits and co U supplied. Thanks so much. Really New to Php. Just need to get a working code and then build from this. |
Re: Please Help With This Simple Code by yinkay007: 4:43pm On Oct 08, 2014 |
I have solved this problem. Thanks Guys |
Re: Please Help With This Simple Code by Nobody: 6:52pm On Oct 08, 2014 |
yinkay007: the code is technically thesame. Pls delete that line with '$data= ceil($data + rand(1,20))/2);', it will only be useful for a gamble scenario. i dont know exactly a case study for what you intend doing. But i guess you are talking about creating a multi-dimensional array. I need more insight to know what array functions will be used. Or are you imagining a situation where the user selects not a number but a range of numbers at a single selection? Then specify the ranges here. If that is the case its also possible without using a multi-dimensional array. I don't fully get what the task look like! |
(1) (Reply)
Project Management Professional (PMP) / Wow Learn Web Development And Graphic Design For 5k / Dns Server Problem ( My Site Not Opening) Help I Will Pay
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18 |