Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds

Reply
 
Thread Tools
  #1  
Old 04-17-2010, 04:55 PM
TwinkleTits TwinkleTits is offline
Member
 
Join Date: Jan 2009
Thanks: 116
Thanked 17 Times in 16 Posts
Default Trouble with PHP loops

I don't really know how to use loops yet. I been using the for loop for everything but the problem I keep running into is sometimes I wanna do something once within a loop but since its a loop it ends up doing it about 50 times. Heres the code
Code:
	$replace = preg_replace(array_keys($bbcode), array_values($bbcode), $replace);
	$br_pieces = explode('~T~',$replace); 

	for ($ibr=0; $ibr<=50; $ibr++) { 

		if ($ibr % 2) { $br_number = "odd"; } else { $br_number = "even"; }

		if ($br_number == "even") { $br[$ibr] = nl2br($br_pieces[$ibr]); }
		else { $br[$ibr] = $br_pieces[$ibr]; }



		$pieces = explode('~@*',$br[$ibr]); 

		for ($i=0; $i<=50; $i++) { 

			if ($i % 2) { $number = "odd"; } else { $number = "even"; }

			if ($number == "even") { 
				//echo $pieces[$i];

				$pieces_tex = explode('~TeX~',$pieces[$i]);

				for ($i_tex=0; $i_tex<=2; $i_tex++) {  
					if ($i_tex % 2) { $number_tex = "odd"; } else { $number_tex = "even"; }
					if ($number_tex == "even") { echo $pieces_tex[$i_tex];  }
					else { 

						$latextext = "[tex]\sqrt{2}[/tex]"; 
						include_once('include/latexrender/latex.php'); 

						$latextext=latex_content($latextext);
						echo $latextext;
						
					}

				}

			}

			else { 
				$settings = explode('~',$pieces[$i]); 
				display_tables("$settings[0]","$settings[1]","$settings[2]","$settings[3]","$settings[4]","RAND()");
			}

		}

	}
Theres 3 loops the 2nd loop is inside the 1st loop and the 3rd loop is inside the 2nd loop. Problem I'm having is when I echo that $latextext variable it echos it about 100 times because the 2 parent loops are set to $i<=50. The reason I have all these loops is because I need to explode the text field to run PHP functions for the special BBCode tags. What am I doing wrong?
Reply With Quote
  #2  
Old 04-17-2010, 05:02 PM
The Cheshire Cat The Cheshire Cat is offline
AFK
 
Join Date: Jan 2009
Location: Everywhere
Thanks: 463
Thanked 925 Times in 580 Posts
Send a message via MSN to The Cheshire Cat
Arrow Re: Trouble with PHP loops

I think the problem you're having is that you are nesting a loop for a single function when it shouldn't be. You could probably accomplish your task by using a single For loop and a While loop. Then again, I may be wrong since my PHP is super fuzzy.
__________________
Quote:
Originally Posted by -SpectraL View Post
Some people just like to be argumentative about stuff nobody should really give a fuck about.
Reply With Quote
  #3  
Old 04-17-2010, 06:37 PM
PirateJoe's Avatar
PirateJoe PirateJoe is offline
Moonmeister
 
Join Date: Jan 2009
Location: Big Rock Candy Mountains
Thanks: 119
Thanked 286 Times in 165 Posts
Default Re: Trouble with PHP loops

What is it exactly you're trying to do in this code? Why do you sometimes need to do it 50 times and sometimes only once? What I would do is put that code into its own function, which you can then call inside of a for loop or a while loop to control how many times it gets executed. Alternatively, you could use just use a while or do while loop like this:

Code:
$iternum=whatever; 
while(i<iternum) { <above code here> i++; }
But really, if you're going to be doing this operation multiple times in different parts of your program, best be putting it in its own function.

also: quick primer on loops:
for(<declaration>;<test>;<increment>) {}
the for loop executes as long as the <test> condition is met, based on <declaration> and <increment>, for ex
for(int i=0;i<4;i++){}
will loop 4 times (as long as i<4, incrementing i by one each time through the loop)

while(<test>){}
this loops as long as the test condition is true, without any built-in counter. ex
while(true) loops forever
while(input!=0) loops until the user enters zero

do{}while(<test>) same as a while loop, except whats in the brackets is guaranteed to be executed at least once. in the previous example, input may have been undefined, or a nonsense value, the first run through, so it would have been better to use a do while loop, ex:

do{ input=getinput(); } while (input!=0);
__________________
[COLOR="Red"]l[/COLOR][COLOR="Orange"]o[/COLOR][COLOR="Yellow"]l, [/COLOR][COLOR="Lime"]f[/COLOR][COLOR="DeepSkyBlue"]o[/COLOR][COLOR="Blue"]r[/COLOR][COLOR="Plum"]u[/COLOR][COLOR="Purple"]m[/COLOR]

Last edited by PirateJoe; 04-17-2010 at 07:50 PM.
Reply With Quote
  #4  
Old 04-17-2010, 07:54 PM
Zok's Avatar
Zok Zok is offline
Autonomato-noma-napoton
 
Join Date: Dec 2006
Location: Ohio
Thanks: 64
Thanked 857 Times in 335 Posts
Default Re: Trouble with PHP loops

Yeah - what is this code trying to do? You should consider commenting it thoroughly, too. Sometimes when I comment the crap out of something I notice an inherent design flaw because I have to verbalize my code. I too would like to know why this needs to run 50 times...

Also, this won't help your problem - but here are a few pointers about your code:

Why are you setting a variable to "even" or "odd" if you only test it once? It seems pointless if you're only going to use that value once. Why not do something like:
Code:
if ($ibr % 2) { ...
Instead of:
Code:
if ($ibr % 2) { $br_number = "odd"; } else { $br_number = "even"; }
if ($br_number == "even") { ...
Typically when we loop over arrays, it's easier to use the foreach loop:
Code:
foreach($array as $array_item) { ... }
or
Code:
foreach($array as $array_index=>$array_item) { ... }
This makes sure that every index of the array gets hit once and saves you the grief of off-by-one errors and array offsets ($array_item instead of $array[$offset]).
__________________
There used to be a bunch of assholes that posted on this part of zoklet here... But we systematically removed them like you would any kind of termite or roach.
Reply With Quote
  #5  
Old 04-17-2010, 09:44 PM
TwinkleTits TwinkleTits is offline
Member
 
Join Date: Jan 2009
Thanks: 116
Thanked 17 Times in 16 Posts
Default Re: Trouble with PHP loops

Heres what the code does. At the top all the BBCodes have been replaced with HTML so the rest of the code is for the special tags that run PHP functions. It explodes the variable the odd numbered pieces are text to be echoed and the even numbered parts are functions to be executed. The functions are usually shit getting pulled out of databases. It was working fine up until I tried to put in LaTeX tags in. The problem is when I do echo $latextext; it prints it out about 50 times. The reason it repeats it that many times is cuz I don't know how many parts the variable is gonna be exploded into it depends how many times those special BBCode tags are used on the page.
Reply With Quote
  #6  
Old 04-19-2010, 12:18 AM
Zok's Avatar
Zok Zok is offline
Autonomato-noma-napoton
 
Join Date: Dec 2006
Location: Ohio
Thanks: 64
Thanked 857 Times in 335 Posts
Default Re: Trouble with PHP loops

Quote:
Originally Posted by TwinkleTits View Post
The reason it repeats it that many times is cuz I don't know how many parts the variable is gonna be exploded into it depends how many times those special BBCode tags are used on the page.
Use
Code:
for($i=0;$i<=count($the_array);$i++) {
__________________
There used to be a bunch of assholes that posted on this part of zoklet here... But we systematically removed them like you would any kind of termite or roach.
Reply With Quote
  #7  
Old 04-19-2010, 07:12 PM
TwinkleTits TwinkleTits is offline
Member
 
Join Date: Jan 2009
Thanks: 116
Thanked 17 Times in 16 Posts
Default Re: Trouble with PHP loops

I reduced it all to a single while loop
Code:
	$br_pieces = explode('~S~',$replace); 
	$el_count = (count($br_pieces));
	$ibr=0;

	while($ibr<$el_count) { 

		if ($ibr % 2) { $number = "odd"; } else { $number = "even"; }
		

		//echo "<p>el_count = $el_count<br>ibr = $ibr<br>ftype = $ftype<br>number = $number</p>";


		// IF NUMBER OF PIECE OF EXPLODED VARIABLE IS AN EVEN NUMBER ECHO IT
		if ($number == "even") { echo nl2br($br_pieces[$ibr]); }
		
		// ELSE PROCESS IT AS A FUNCTION
		else { 
			$final_text = substr($br_pieces[$ibr],3);
			$ftype =  substr($br_pieces[$ibr], 0, 3);
			if ($ftype == "_T_") { 
				// echo "TYPE = TABLE";
				echo $final_text;
			}
			elseif ($ftype == "_D_") { 
				// echo "TYPE = DISPLAY";
				$settings = explode('~',$final_text); 
				display_tables("$settings[0]","$settings[1]","$settings[2]","$settings[3]","$settings[4]","RAND()");
			}
			elseif ($ftype == "_L_") { 
				// echo "TYPE = LATEX"; 


				$latextext = reversecleanvars($final_text);


				//$latextext = "[tex]\\sqrt{3}[/tex]";
				//$latextext = '[tex]\frac{8}{9}[/tex]';
				//echo "<p>1.) $final_text<br>2.) $latextext<br>3.) $final_text2</p>";


				include_once('include/latexrender/latex.php'); 

				$latextext=latex_content($latextext); 
				echo $latextext;
			}

		}

		$ibr++;
	}
now I got LaTeX up and running too. Can you get LaTeX to actually process maths? I'm setting up this maths/physics/chemistry problems database and all the variables are randomly generated so you can keep practicing the same type of question question without the same values. When it comes to maths I'm gonna need to get the PHP to differentiate and integrate functions and it would make things a whole lot simpler if I could get LaTeX to do it.
Reply With Quote
  #8  
Old 04-20-2010, 08:02 PM
Zok's Avatar
Zok Zok is offline
Autonomato-noma-napoton
 
Join Date: Dec 2006
Location: Ohio
Thanks: 64
Thanked 857 Times in 335 Posts
Default Re: Trouble with PHP loops

LaTeX is a markup language and won't let you actually do computations - you'll have to do that in PHP.

Either that, or use a program like mathematica and WITM to do the work for you. Check out: http://witm.sourceforge.net/
__________________
There used to be a bunch of assholes that posted on this part of zoklet here... But we systematically removed them like you would any kind of termite or roach.
Reply With Quote
Reply

Bookmarks

Tags
loops, php, trouble

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
First Foray into Fruity Loops Anima Mundi The Musician's Playground 12 06-12-2011 07:55 PM
Trying to learn fruity loops Nuts Music! 8 08-09-2009 12:52 PM
Fruity Loops mxs Music! 0 04-10-2009 03:34 AM
U want trouble? I'll give u trouble! u dont know me! Craigslist.org Bat Country 3 03-03-2009 09:48 PM


All times are GMT. The time now is 08:49 PM.


Hot Topics
On IRC
Users: 4
Messages/minute: 0
Topic: "http://www.zoklet.net/..."
Users: 19
Messages/minute: 0
Topic: "ask ibm why atlantis is real"
Users: 10
Messages/minute: 0
Topic: "vaginaboob"
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.