Any Way To Make Else-ifs Shorter

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Any Way To Make Else-ifs Shorter

Feelay
Hey! I know I am asking lots of questions theese days, sorry tongue.gif

I was wondering: Is there anyway to make the following code shorter =?

CODE
#If user is level 1
if($user['level']=1){
if($enemy['level']=1{
$winner=mt_rand(50,100);}
elseif($enemy['level']=2){
$winner=mt_rand(45,100);}
elseif($enemy['level']=3){
$winner=mt_rand(40,100);}
elseif($enemy['level']=4){
$winner=mt_rand(35,100);}
elseif($enemy['level']=5){
$winner=mt_rand(30,100);}
elseif($enemy['level']=6){
$winner=mt_rand(25,100);}
elseif($enemy['level']=7){
$winner=mt_rand(20,100);}
elseif($enemy['level']=8){
$winner=mt_rand(15,100);}
elseif($enemy['level']=9){
$winner=mt_rand(10,100);}
elseif($enemy['level']=10){
$winner=mt_rand(5,100);}}


Because I am going to make this for every single level =/ atm i only have 10 levels.. but what wil happen when I have 100 levels =?

Thanks for any replie

//Feelay

 

 

 


Reply

Eggie
you can use this function...
CODE
for($level=1;$level<11;$level++)
for($string=50;$string>4;$string=$string-5)
if($enemy['level']=$level)
$winner=mt_rand($string,100);

i think that should be correct code...u can use that for all 100 levels,just change the first value in the for loops which are starting values of the strings and second values which are ending values....

Reply

Feelay
hmm.. I ain't the best with for loops. But ty for the code biggrin.gif Can you please explain it a bit, so i know what values i should change, if I ever need to change something =?

Reply

Eggie
i'll be back in few minutes to explain it
EDIT:i'm back
that code i made for you can be put instead of your BIG code...just remember to put "<?php" and "?>" because it ain't gonna work without it....
if you want it for 100 levels just put this :$level<101 instead of $level=11 and it will work but i don't see what $winner=mt_rand($string,100) is for from your code so i don't know what you should edit in that...i can see that it goes from 50 to 5, and it goes down by 5, but as there is no lower numbers (except 0 and minuses) than i don't know whats with that....

Reply

sparkx
Little confused what your are attempting? First off you only used one = in your if which will set a variable in some cases. Also you covered the entire code with if($user['level']=1){ twice not exactly sure why but it makes the elseif() pointless because 1 cant also be equal to 7 ect.

Anyway... It looks as if you might be making a linear pattern. If so you could just run a while($count<10){ then just increase $count by one every time. Then you run an if $count==$enemy['level'] if it comes true then you could simply subtract ($count-1)*5 from 50 do you get what I am saying? This however will only work if your change is a linear equation otherwise you could
A: make a function.
B: just simply type it out and require the file.
C: make a small database table with all the levels and run a while (recommended, easiest).

Here is another one of my untested examples:
CODE
while($count<11){
if($count==$enemy['level']){
$count=$count+1;
$equation=50-(($count-1)*5);
$winner=mt_rand($equation, 100);
}
}


@Eggie: Why don't you use brackets? As far as I am aware is that they are required?

Maybe this will help out a bit,
Sparkx

 

 

 


Reply

Eggie
as far as i know: when you put function "if" and after it only one function stands than you dont need brackets...
like this:
CODE
if(1==1)
$a=1;

and if you have more than one function after "if" than you need brackets
like this:
CODE
if(1==100){
$a=12;
$s=12;}

so in my code up there i wrote one "for"and in it theres another "for" -so no need for brackets...
in that "for" stands if ,and in it there's a number which goes to variable... so again no need for brackets...
while writing this i saw a mistake...
i wrote
CODE
if($enemy['level']=$level)


instead of
CODE
if($enemy['level']==$level)

so that should do it... smile.gif
i'm gonna download "apache" to try it out...i'm gonna edit this post when i do and try it out

CODE
$string=50;
for($level=1;$level<11;$level++)
$string=$string-5;
if($enemy['level']==$level)
$winner=mt_rand($string,100);


this is my final code...if it doesn't work i dont know what you should do...

Reply

Feelay
me = confused tongue.gif Come on guys tongue.gif I've never used loops before tongue.gif Can't you explain to me, like when you explain to a 3 years old baby or something tongue.gif

Reply

Quatrux
here you can find something about loops:

http://en.wikipedia.org/wiki/For_loop

Also, here is a quote:

QUOTE
You've heard the term loop before, but you may not really be sure what it is. A loop is a series of commands that will continue to repeat over and over again until a condition is met. For example, let's say you have the names of people in an array, and you wanted print a list of all of the names. You could setup a loop that would print the first persons name, and then move on to the next and print her name, etc (the continuing to repeat itself part.) The condition would be that it stops once all of the names have been used. Our example would best be used in a foreach loop. The other types of loops that are used are while and for loops.

Reply

Feelay
ok.. maybe I should explain a little more.. because now, I need the code, and I can't figure out yours tongue.gif

The thing I wan't to do is, a loop that checks values from the database. Lets say, I have a strenght stat.
I want the for loop to do the following.
If the strenght stat is set to 1, the player will hit 5 more damage. Else if the strenght stat is set to 2, the player will hit 10 more damage, and so on..
I want to use this function in more than one situation. I hope that you got a view of what I want.. When I created this post, I didn't know how to explain stuff tongue.gif I was a noob. the Only thing I did was asking the question and then let people firgure out what I wanted to do.
I know how loops works, but in this case, I am confused.. My brain isn't good enought to think out something like this alone tongue.gif

Thanks //Feelay

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : make, ifs, shorter

  1. Center Update
    Make the center of the page change only (5)
  2. Is It Possible To Make Php Scripts Executed Without A Cron?
    (2)
    Title says it all really. Just wondering if it's possible in an way? If so could anyone tell
    me?....
  3. Make Money Online With Cashcrate - A Scam?
    (4)
    Nope, it's not a scam. I was surprised too, as most of these sites turn out to be scams.
    CashCrate , however, actually pays, and at an extremely fast rate as well. Basically, at the site,
    you complete listed offers and then the site will credit you with dollars. If you're wondering
    about the speed of the crediting, the site does it as soon as the company listing the offer tells
    them you've completed an offer. You'll also get an email letting you know you've
    finished and that they've confirmed the offer. You can cash out once you've reached &....
  4. Anyone Willing To Make A Text-based Game With Me?
    for all interested view the idea first (4)
    Here we are. A few days ago while playing slavehack (my fav text-based game, which is currently
    laggy and ddosed), i got an idea for a game based on similar principles. In this game, you are an
    instrument player eg. a Bass player, a guitar player, a drummer, a singer etc. you can earn and
    improve your skills by practicing. You have to choose your playing style(metal, rock, death metal,
    trash metal, jazz etc. you can buy guitars, amps, pedals, processors etc. when you get good enough
    you can form a band with other players. you can than make your gigs, where you earn cash,....
  5. Get Paid To Search Yahoo!
    New way for you to make money online (10)
    Hi buddies, Is this a good news for you? I've got paid for the first month from this site. Here
    is how you can earn: After you sign up, they ask you to set their page as homepage and install a
    search box.Everyday, when you search once, you will earn up to 3p. How much you can earn depends on
    where you live. I earned 1.5p per search. So, if you search 40 times per day, how much you will earn
    a month? It's very easy, right? In addition, when you refer friends, you will earn more. They
    offer 4 referral levels: 50%, 10%, 5% and 2.5%. If you are interested, sign up a....
  6. Make It Impossible To View Page Source
    (11)
    Hi! I was just wondering if it is possible to make username unable to view the page source.. I
    know that you can disable right click, but still, they can go to the page source from the navigation
    menu on the top of the browser.. if possible, I would really like a code that works on all browsers.
    Or if you know different codes that works on different browsers.. Thanks //Feelay....
  7. How To: Make A Simple Php Forum System
    (4)
    Hello, One of my first ever projects I embarked on when I began to understand PHP well was a forum
    system. I've decided to begin writing a tutorial to help people start one of their own. The code
    for this project is based on that of OakumBoard/cBoard, my own forum software which can be seen
    running at www.sonicxtremegm.co.cc Anyways, time to start off, you must run this SQL query on an
    SQL database with a name of your choice: CODE s CREATE TABLE `forums` (
      `id` int(3) NOT NULL auto_increment,   `name` varchar(80) NO....
  8. Love Calculator
    How to make a love calculator for a mobile site (1)
    In this tutorial we will build a Love Calculator to calculate love percentage between two persons.
    Remember, it is just a funny game popular in mobile sites. It can not calculate the real love
    percentage. We will make this Love Calculator for mobile sites (WAP sites). To make this Love
    Calculator, you need a little knowledge of PHP and WML. We will use a rand() function to generate a
    random value to show the love result. We will also use input box and anchor to collect the name of
    partners who wants to calculate their love. To make the script easier, we will divide thi....
  9. Mmorpg
    I want to make kids MMORPG (1)
    I have an idea for a kids game that has the potential to make go profit. I'm looking for
    someone that knows how to code games and use flash to make games. The game will involve the user
    making a character and traveling in the world we create or I create. It will have to be easy enough
    that a 8 or 9 year old can use it. It will use educational games, traveling, harvesting, selling to
    help the players make money; also will have a little fighting but not the gore kind a simple
    fighting system. I can explain later once we decided to work together. If you want to wo....
  10. How To Make Your Own Web Host
    (2)
    QUOTE There are many reasons why you would want to have your website professionally hosted.
    However, some people are better off hosting their site themselves. For example, if you are starting
    a new business or playing with an idea that you are not sure will work, you might not want to pay a
    hosting company to host your site, at first. Hosting the site yourself will allow you to save money
    that you can use to grow your business. Once your business starts growing and your site starts
    getting a lot of visitors, then you will find it necessary to pay a web hosting compa....
  11. How To Make A Private Message System.
    (9)
    Hey! Today, I am going to teach you how to make a Private Message (PM) script in PHP. Before
    we start, I want to tell you what you should know, and what files we will create. Then we will
    continue with the codes, and descriptions. I would like if you learned something from this tutorial.
    If you find any errors (Even if I spell something wrong), I would like you to post it in this
    thread. What you should know: You should know HTML. Just a bit (forms, and maybe a little design
    if you would like that). You should know much about PHP and Mysql. You should know how ....
  12. How To Make An Test-based Rpg Game!
    This is a step by step tutorial of how to make a RPG test-based game (4)
    Welcome Hello this is Convictlife and i have made an RPG game and for you noobs out
    there i will be explaining Here is a breif structure of this post: 1. Getting your codes. 2.
    Getting your host. 3. Uploading your codes. 4. Configuring your codes. 5. MySQL 6. phpMyAdmin 7.
    CronJobs 8. Editing 9. Finalizing Getting Your Codes! First of all you need codes that
    will power your website. If you are a master coder (like Dabomstew) then you can code these codes
    yourself, but if you are "so-so" then you will need to get codes from somewhere. I got....
  13. How To Make A PM (Personal Message) System?
    (Should I Store The Data In A Database?) (4)
    Hey! I just wanted to know, if I want to make a PM system, should I store the PMs in a database
    or how should I do it? And if I should store them in a database, how do I do that. Because I have
    acctually no idea, if I should create 50 tables to store 50 messages /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> (And a little OT question
    ). How can i change the timestamp into real date?....
  14. Make A Wish And It'll Come True...if....
    Common email chain letters (6)
    Do you ever come across email chain letters that say something like: QUOTE Now make a wish and
    scroll down ((scrolls down about 15 seconds)) STOP!!! Send this to ## people and your
    wish will come true in ## minutes/hours/days. Or things like this: QUOTE 13 ways to tell if
    someone loves you ... (a list) ... Send this to 20 of your friends and you will get a date within
    the next day! Well I am on the hunt for an EXTREMELY important chain letter that is of the
    2nd variety. It was quite lengthy, and consisted of maybe 50-120 lines on tips to get....
  15. Access
    Is this easy to make a login/password? (17)
    I was looking to make a site where people need to create an account, and log in to view the main
    pages. Is it easy, or do-able, to use msoft access to make a database where people are able to sign
    up with a username and password via my website?....
  16. Powerpoint Based Games!
    I know how to make them! (18)
    Hi, have you ever heard of Tic Tac Toe/X's and O's on PowerPoint slides. I'm trying to
    make one but is HTML. xxxx-jozh-xxxx....
  17. Looking For Linux
    2 choices that I will choose - make it easy to install and MAINTAIN (34)
    Ok I have tried Linux before, it was ok, until something with the kernel screwed up and it left me
    with a command line interface that I didn't know what to do. I am looking for 2 different
    distributions of Linux here, my first one is for: (Currently running Windows XP SP2) A computer
    with 512 MB RAM Pentium 4 processor Internet Access GNOME or KDE desktop, doesn't matter Minimal
    command line work!!! Absolutely no command line work when installing. I have tried
    Ubuntu, it was easy enough. Anything easier than Ubuntu and I'll give it a try. M....
  18. How To Make Windows Xp Faster
    (30)
    hello does somwane have tips to let windows xp start up faster are there special programmas for or
    somthing like thate thanks fore youre time....
  19. A Beginner Wanting To Learn How To Make A Web Game
    (4)
    I'm pretty new to html and stuf cuz i dont know it ut i want to make a game like Combatgrounds
    which is located at http://www.combatgrounds.com . But i don't know where 2 start so can any1
    give me any pointer. Your help will be appreacitaed Spelling and grammar are important, as stated
    in our forum rules, but also part of your membership with AstaHost. Refrain from using slangs. "U"
    "R" smarter than "2" does not mean TO... Topic title must be specific. Modified. Last verbal
    warning. ....
  20. Psybnc - Howto
    How to make a psyBNC on Linux (4)
    Installation First, fetch the latest source code from http://www.psybnc.at QUOTE cd wget
    http://www.psybnc.at/download/beta/psyBNC-2.3.2-7.tar.gz tar -zxvf psyBNC-2.3.2-7.tar.gz cd psybnc
    Configuration QUOTE make menuconfig Now go in `Bouncer Config', next set Listening
    Port and Ip. Next select `User' Here you will be able to create a user for your bouncer.
    You must set Ident, Realname and Password, and vhost too. So return to main menu, and choose exit.
    When prompted for saving select Yes. Launching psyBNC QUOTE make ./p....
  21. How To Make Realistic Fire!
    real fire in photoshop (13)
    Hello and welcome to my tutorial, I’m going to teach you how to make realistic
    fire!!!! this is how I figured it out, I was makeing smoke in photoshop one day,
    but i wanted color to be added to it, so I added some color balance to it and ""click"" it hit
    me.... it looked alot like fire. --I would rate this tutorial easy and on a scale from 1 to 10 of
    difficulty, I would say a 3 --this is how I did it. 1. first I started with a blank page.. duh
    it doesn't mater what the size is but I used 400 X 400 pixels 2. Ok, we got our blank document
    now w....
  22. Real Driving
    Does driving for real make you better at racing games? (17)
    Does driving for real make you better at racing games? And if so does it help to drive faster in
    real life? What do you think? I think it helps, especially with simulation racers! It helps you
    understand certain concepts better! Like two wheel drive vs four wheel drive. And camber on the
    road, things like that! Im not saying drivers are better than non drivers but i think it gives
    them an edge!....
  23. How To Run A Proxy On a Web-Server?
    How to make a proxy on website (20)
    Does anyone know how to set up a proxy on a website? I want to set up a proxy on my web space, that
    i can access from anywhere, well mainly school since most websites are blocked at school. I know
    there are a bunch of free ones, but i want my own so i can learn how it works and configure it. and
    i want to be able to set up a global username and/or password to be able to use it....
  24. Site To Create Your Own MMORPGs
    (33)
    If You Ppl All Want To Make MMORPGS Just Go To http://www.touchofdeathproductions.com And Download
    Eclipse!!!! God... They May Be 2D But Their Still MMORPGS.......
  25. How To Make A CS 1.6 Map ?
    (15)
    I just started playing CS .. err .. I wont comment but i started playing cause my class mates were
    into it and i just needed some time out after a days work. And FPS games are my fav /wink.gif"
    style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> .. Well getting back to the
    point .. I have played on few maps like Mansion, dust, dust2, poolside, assault etc .. I was
    wondering if i could make a map too ? ... 1) Does it include scripting or programming ? 2) Or what
    are the best tools that are used ? 3) Is there a tutorial ? 4) Is there a good site ou....
  26. Photoshop Tutorial: How To Make A Userbar For Signatures
    (34)
    How to make a userbar by pyost Lately the userbars have become very popular with people. They
    are mostly putting them in their forum signatures, which is very practical. They are small, but can
    be used for linking. A lot of userbars can be found at www.userbars.com and www.userbars.org ,
    but sometimes people can't find exactly what they want. This is why I have decided to make a
    tutorial on how to make you own userbar. The pictures in this tutorial are made in Photoshop 6.0,
    so don't be surprised if you're Photoshop is different. I will be making a....
  27. Make Your Own FPS Game
    (32)
    i happend to be searching the internet, as i usually am doing from 5-9 on a daily basis...and i came
    accross this advertisement that sparked my intuitive imaginiation (wow big words...what do they mean
    ?) ne ways...heres what the ad said... First Person Shooters are one of the most enduringly
    popular game genres. Now, for the first time, anyone can make their own action packed FPS games
    without any programming or 3D modelling knowledge. Bringing a new benchmark to game creation, the
    innovative and multi-featured FPS Creator has been three years in development and off....
  28. Online Multiplayer Game
    What language to learn in order make it? (16)
    If you look at e.g. THIS LINK What language would you use to make something like that?
    /wink.gif' border='0' style='vertical-align:middle' alt='wink.gif' /> ....
  29. Enhanced Audio Cd's
    Learn to Make Your Own Enhanced Audio CD (2)
    Some may be familiar with the concept of Enhanced Audio CD's. For those that aren't, an
    Enhanced CD is much like a traditional Red-Book audio CD (the normal format for audio cd's -
    what you would purchase in a store). The only difference is that, in addition to being able to put
    your CD into a CD player and listen to music, you can also put the disc in a computer and access
    more features such as graphics files, more songs, or even create custom navigatable menu's and
    programs. All this enhances a consumers experience of your media. There is much to be....
  30. How To Make A Text Based Online Game Script ?
    (23)
    Does anyone know if there is a easy was to make a text base online game, Like a free templet, or if
    anyone know how to make one and can help me.....

    1. Looking for make, ifs, shorter

Searching Video's for make, ifs, shorter
Similar
Center
Update -
Make the
center of
the page
change only
Is It
Possible To
Make Php
Scripts
Executed
Without A
Cron?
Make Money
Online With
Cashcrate -
A Scam?
Anyone
Willing To
Make A
Text-based
Game With
Me? - for
all
interested
view the
idea first
Get Paid To
Search
Yahoo! -
New way for
you to make
money online
Make It
Impossible
To View Page
Source
How To: Make
A Simple Php
Forum System
Love
Calculator -
How to make
a love
calculator
for a mobile
site
Mmorpg - I
want to make
kids MMORPG
How To Make
Your Own Web
Host
How To Make
A Private
Message
System.
How To Make
An
Test-based
Rpg
Game! -
This is a
step by step
tutorial of
how to make
a RPG
test-based
game
How To Make
A PM
(Personal
Message)
System? -
(Should I
Store The
Data In A
Database?)
Make A Wish
And
It'll
Come
True...if...
. - Common
email chain
letters
Access - Is
this easy to
make a
login/passwo
rd?
Powerpoint
Based
Games! -
I know how
to make
them!
Looking For
Linux - 2
choices that
I will
choose -
make it easy
to install
and MAINTAIN
How To Make
Windows Xp
Faster
A Beginner
Wanting To
Learn How To
Make A Web
Game
Psybnc -
Howto - How
to make a
psyBNC on
Linux
How To Make
Realistic
Fire! -
real fire in
photoshop
Real Driving
- Does
driving for
real make
you better
at racing
games?
How To Run A
Proxy On a
Web-Server?
- How to
make a proxy
on website
Site To
Create Your
Own MMORPGs
How To Make
A CS 1.6 Map
?
Photoshop
Tutorial:
How To Make
A Userbar
For
Signatures
Make Your
Own FPS Game
Online
Multiplayer
Game - What
language to
learn in
order make
it?
Enhanced
Audio
Cd's -
Learn to
Make Your
Own Enhanced
Audio CD
How To Make
A Text Based
Online Game
Script ?
advertisement




Any Way To Make Else-ifs Shorter



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE