Jump to content

Recommended Posts

Ok I have been coding my script for some time and for some reason it wont pass on a variable accross an include. I was told its something to do with functions and that my code is very poor :(

 

Yet ive learnt from tutorials... so how do people know whats efficient over something else cos thats something tutorials don't really explain.

 

Any way i got this script but i need to get it so that the code will store the variable so i can load that variable up on in my html using short tags but its not doing it.

 

include("include.php");


//RAND
$Wins[1] = "Result: You search the train station from top to bottom and find $1!";
$Wins[2] = "Result: You search the train station from top to bottom and find $3!";
$Wins[3] = "Result: You search the train station from top to bottom and find $5!";
$Wins[4] = "Result: You search the train station from top to bottom and find $8!";
$Wins[5] = "Result: You search the train station from top to bottom and find $10!";


$rand = rand(0,(count($Wins)-1)); 
$QuoteResult = $Wins[$rand];

 

Now the above script is "included" in a script that is also "included" in the page that the user loads directly which contains the html of:

 

<? 
if (isset($DoCrime['DoCrime'])){
include("UnderBridgeCrimeScript.php");
}
?>


<div id="bv_" style="position:absolute;left:307px;top:348px;width:262px;height:64px;z-index:14" align="center">
<?
echo $QuoteResult;
if($QuoteResult == 'Result: You search the train station from top to bottom and find $1!')
{
echo '<font style="font-size:16px" color="green" face="Arial">';
echo $QuoteResult;
echo '</font></div>';
}else if($QuoteResult == 'You failed to find any money, better try some other time!')
{
echo '<font style="font-size:16px" color="red" face="Arial">';
echo $QuoteResult;
echo '</font></div>';
}else if($QuoteResult == 'Result: You search the train station from top to bottom and find $3!')
{
echo '<font style="font-size:16px" color="green" face="Arial">';
echo $QuoteResult;
echo '</font></div>';
}else if($QuoteResult == 'Result: You search the train station from top to bottom and find $5!')
{
echo '<font style="font-size:16px" color="green" face="Arial">';
echo $QuoteResult;
echo '</font></div>';
}else
{
echo '<font style="font-size:16px" color="red" face="Arial">';
echo 'Server is having difficulties admin is working on it!';
echo '</font></div>';
}
?>

 

the includes do work how ever because i did some echo's to test sessions etc.. but it just wont pass the value across. Also the rand seems to only load the last result every time. I tried with $Wins[1] and with $Wins[] but both gave same result. Yet when put the exact same script on its own without all the includes that play a part it works :/

Link to comment
https://forums.phpfreaks.com/topic/65435-solved-making-this-efficient/
Share on other sites

The reason someone told you it was inefficient is probably because you are using a lot of the same data in a bunch of different variables. There are a lot of ways to do what you are doing, but to keep it similar to your code, you can make it more efficient by doing something like this:

//RAND
$Wins[1] = 1;
$Wins[2] = 3;
$Wins[3] = 5;
$Wins[4] = 8;
$Wins[5] = 10;


$rand = rand(0,(count($Wins)-1)); 
$QuoteResult = $Wins[$rand];

 

Then in your other file:

if($QuoteResult == 1)
{
echo '<font style="font-size:16px" color="green" face="Arial">';
echo "Result: You search the train station from top to bottom and find \$$QuoteResult!";
echo '</font></div>';
}else if
etc...

 

As for it not working, what is being printed out and what do you want to be printed out?

First off, your code for selecting a quote is wrong. You are selecting a random number from 0 - 4, but your quotes are numbered 1-5! Don't hard code the index numbers for the array - let PHP do it automatically and it will start at 0.

 

I think you need to echo the variable you are having a problem with at different stages of the processing to see where it is being lost. And, where is the quote for No money found in the array? Plus, you should not use the FONT tag, it has been depricated for a long time. Are you sure DoCrime is set?

 

However, one area that might be causing the problem is that you are making it more difficult than it needs to be by having the code dynamically select a quaote and then testing for that quote.

 

try this:

 

<?php

include("include.php");

//RAND
$Wins = array (
   'You failed to find any money, better try some other time!'
   'Result: You search the train station from top to bottom and find $1!',
   'Result: You search the train station from top to bottom and find $3!',
   'Result: You search the train station from top to bottom and find $5!',
   'Result: You search the train station from top to bottom and find $8!',
   'Result: You search the train station from top to bottom and find $10!'
   );

$rand = rand(0,(count($Wins)-1)); 
?>

 

<?php

<? 
if (isset($DoCrime['DoCrime'])){
include("UnderBridgeCrimeScript.php");
}

echo '<div id="bv_" style="position:absolute;left:307px;top:348px;width:262px;height:64px;z-index:14" align="center">';

if (isset($Wins[$rand])) {

 if ($rand == 0) {
   echo '<span style="font-size:16px;color:red;font-name:Arial">';
 } else {
   echo '<span style="font-size:16px;color:green;font-name:Arial">';
 }
 echo $Wins[$rand];
 echo '</span>';

} else {  //rand not set
 echo '<span style="font-size:16px;color:red;font-name:Arial;">';
 echo 'Server is having difficulties admin is working on it!';
 echo '</span></div>';
}

echo '</div>';
?>

there can't be any fails^ it sounds strange but thats cos fails are on a different .php !

 

Basically this is the logic:

 

UnderBridgeCrimeScript.php which is included as shown on my first post:

 

Rand if exp is less than 100 20 % chance of success :

fail.php

or

success.php

 

 

Rand if exp is more than 100 80 % chance of success :

include fail.php

or

include success.php

 

 

then in Success.php

theres another rand cos theres 5 different types of wins

success creates $resultQuote as 5 possible types:

 

"u win 5 bucks"

"u win 3 bucks"

etc 5 types.

 

 

in fail.php

theres only one type of fail so there is no rand.

Fail creates $ResultQuote as "You failed blah blah"

 

 

then back on the page that the user is seeing which is the first post i posted to you with all the echo's it should show the result of what ever is created from the php ^

why does the modify button on this forum keep disappearing :S!

 

Any way i tried your code idea:

 

//RAND
$Wins = array (
    'Result: You search the train station from top to bottom and find $1!',
    'Result: You search the train station from top to bottom and find $3!',
    'Result: You search the train station from top to bottom and find $5!',
    'Result: You search the train station from top to bottom and find $8!',
    'Result: You search the train station from top to bottom and find $10!'
    );

$Rand= rand(0,(count($Wins)-1)); 

 

i don't get how that would assign the quote to the variable $QuoteResult

why does the modify button on this forum keep disappearing :S!

 

Any way i tried your code idea:

 

//RAND
$Wins = array (
    'Result: You search the train station from top to bottom and find $1!',
    'Result: You search the train station from top to bottom and find $3!',
    'Result: You search the train station from top to bottom and find $5!',
    'Result: You search the train station from top to bottom and find $8!',
    'Result: You search the train station from top to bottom and find $10!'
    );

$Rand= rand(0,(count($Wins)-1)); 

 

i don't get how that would assign the quote to the variable $QuoteResult

 

It won't - you don't need to use the variable. You already have the array $Wins defined. It is wasteful to set the $QuoteResult and then test that agaist the string values later on. Doing so is error prone because you must ensure that you create the string in EXACTLY the same way in two places.

i see what you mean but if i had :

 

 

if == 2

 

it would get confusing to people who come in later on when they want to use my scripts.. so i used it fully to make it more self explanatory... would it cause much of a slow down doing the way i done it ? or is it barely much difference in speed?

 

 

This particular thing only occurs on one page it never occurs on any other pages.

i see what you mean but if i had :

 

 

if == 2

 

it would get confusing to people who come in later on when they want to use my scripts.. so i used it fully to make it more self explanatory... would it cause much of a slow down doing the way i done it ? or is it barely much difference in speed?

 

 

This particular thing only occurs on one page it never occurs on any other pages.

 

And why would you want to use if==2? By the code you originally posted it appeared to me that if the quote is anything other than the "you failed to find money" that you wanted it displayed in a particular fashion, and the "failed" statment was displayed another way. So, why would you want to have to hard-code the same statements twice?

 

It's not a matter of speed, it is a matter of usability and flexability. By hard-coding the same text twice in two different places is just bad practice. With the method I proposed all someone has to do is add or remove lines from the $Wins array and it all just works. You don't have to go back and modify a bunch of IF statements. Granted, it might not work exactly as I wrote it as I don't know the scenarios that govern how $DoCrime['DoCrime'] is set or where the "failed" quote is set, but the logic is still valid. Perhaps you need to review your methodology.

 

By the way, I read your post on reply#3 and I didn't respond, because it wasn't making any sense to me.

 

You are free to do whatever works for you, I'm just trying to propose a better method of accomplishing the same thing. If I was to need to modify the code and I saw an array of quotes in an include file, how would I know that I need to also add those quotes in another place?

no one has understand post 3 yet lol if they did i think everything would make sense both ways cos the suggestions just cannot work. because theres other features that will be using these codes at a later date. :/

 

ok so lets say i win and it chooses in the array and assigns $Wins

 

how do i do an if statement then to check what $Wins is to quote the sentence to the user..

 

which is why i said:

 

if == 2

$ResultQuote = "Result: you found $8';

 

then same for if == 3 "you found $3";

 

you get me ? so numbers would seem harder to remember :/ if you came back to it at a later date

ok so lets say i win and it chooses in the array and assigns $Wins

 

how do i do an if statement then to check what $Wins is to quote the sentence to the user..

 

which is why i said:

 

if == 2

$ResultQuote = "Result: you found $8';

 

then same for if == 3 "you found $3";

 

you get me ? so numbers would seem harder to remember :/ if you came back to it at a later date

 

Yes, I get you, but you are not getting me! Why do you need to know what number was selected? Why do you need to do an if statement to know which string to show? You have the $Rand value and the array $Wins, correct? Then just display $Wins[$rand] - which is the string you want to show.

 

Remember, an include file is just run in-line as if the code was directly written into the page (although there can be some issues when referencing file paths, but that is not valid here). So, look at this simple example:

 

(I also implemented the use of array_rand() which makes more sense in this implementation)

<?php

//Start Include code
$Wins = array (
    'Result: You search the train station from top to bottom and find $1!',
    'Result: You search the train station from top to bottom and find $3!',
    'Result: You search the train station from top to bottom and find $5!',
    'Result: You search the train station from top to bottom and find $8!',
    'Result: You search the train station from top to bottom and find $10!'
    );
$RandKey= array_rand($Wins);  //Selects a random key from the array (0-4)
//End Include code

//Verify there is a value for the selected array key
if (isset($Wins[$RandKey])) {
  echo $Wins[$RandKey];
} else {
  echo "There was a problem";
}

?>

 

Actually that if/else statment is not really needed unless you have some code going on between the two sections that may alter $Wins or $RandKey. But, I am overly cautions in validating.

 

Now that code will randomly display one of the quotes every time it is run - no long list of if statements needed. And, now if you want to add/remove quotes, just modify the array to your heart's content. You don't have to remember what key goes with what quote or anything like that when you go back later to edit - you only need to modify the array - nothing else. If you want the quotes to be displayed differently then you should either include the display code within the array values as well or utilize a multidimensional array.

 

Think of it like this. What if you were grabbing the quotes from a database? Would you want to have a section of code that randomly selects a quote from the database and then have a section of code with a bunch of if statements to test the value? Makes no sense.

Argh i think i get it now.... would sumin like this work :

 

$QuoteResults = array();
			$QuoteResults[] = 'Result: You search the train station from top to bottom and find $1!';
			$QuoteResults[] = 'Result: You search the train station from top to bottom and find $3!';
			$QuoteResults[] = 'Result: You search the train station from top to bottom and find $5!';
			$QuoteResults[] = 'Result: You search the train station from top to bottom and find $8!';
			$QuoteResults[] = 'Result: You search the train station from top to bottom and find $10!';

		$QuoteKey = array_rand($QuoteResults);
		$QuoteResult = $QuoteResults[$QuoteKey];

 

then i can echo "QuoteResult" ? Pretty similar to yours but yout array layout is different.. why is that? Does it make a difference?

Well you can certainly define the string in the include file if you want. But, because of the "other" possible outcomes that you had in your original code (i.e. "You failed..." and "Server difficulties.." I left the displaying of the text to the display page. I just hope you are not planning on putting a lot of if statements to see if the value of $QuoteResult equals a specific string.

 

That array process will work as well, but the manner I created the array would take less characters. Again you were asking about efficiency, right?

 

Personally, I would only include the array in the include file. And have all the processing in your main page. This separates the data from the code which is good practice. Once the array is defined you can display a random item in the array at any time using this:

 

$arrayName[array_rand($arrayName)]

 

No muss, no fuss.

I just hope you are not planning on putting a lot of if statements to see if the value of $QuoteResult equals a specific string.

 

 

 

LOL oops :P thats exactly what i did. I code with brain logic you see rather than knowing all the short cuts of php (self taught without manuals) it still works obviously and it is working but what umm down side is there to having if's all over the place?? slower loading or :S?

I think I've already answered that several times in this thread. There is absolutely no reason to do that. It is making the code more complex and more error prone. Assume you were givin someone else's code and were told that you need to add some additional "quotes" to the code. Would it be easier to open a quotes file and just add them there because the code was written well or would you want to have to hunt down several places to add the quotes and making sure they are exactly the same.

 

You say you are using "brain logic" and don't know the shortcuts of PHP. Sorry, but you aren't using logic at all and this has nothing to do with shortcuts. This is just basic programming fundamentals. I have had no formal training in programming and I understand the logic of programming and I know people that have had several years of formal training that couldn't program their way out of a paper bag.

 

I really have no clue why you insist on doing an IF statement to test the value of the quote whne you display it the same way for each one. But, it's your program. Good luck.

each quote gives different xp amounts and different money amounts to the user the only way to check that is by using an if statement to see which one the rand had chose. The add or subtract the xp or money amounts to what ever specified in the if statement.

No that is not the only way to do that and it is not the most efficient. I have not seen the entirety of your code, but I know from experience that the method you have chosen is not the most efficient (that was the topic of your post wasn't it). I gave you a more flexible solution for the problem you posed. That particular solution may not work in your particular situation due to dependencies in other parts of the code. But, your current method is still flawed.

 

Even though several suggestions have been made you still keep defending your methodology. Which leads me to believe that you really aren't interested in getting other opinions. Good luck to you.

well i had to decide if efficiency has more benefits than the hassle of learning all the new methods that im not used to... time is money for me which is also another problem. maybe ill adapt to change in the future ... but can't at the moment will take too long

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.