Page 1 of 1

while loops

Posted: Sat Sep 03, 2005 4:05 am
by Vercz
ya, tis me again /blum.gif' class='bbc_emoticon' alt=':P' />

well im in the process of re-writing the tarot install script.. this version is going to be pretty much automatic, hence the while loop... the problem im having at the moment, is its just flooding out, maybe someone here could point me in the right direction?

Code: Select all

<?php
if ($_GET['suit'] && $_GET['suit'] != "table") {
	echo "table";
}
else {
	$suit = $_GET['suit'];
	$file = $suit . '.txt';
	$num = 2;
	$image = 'images/' . $num . $suit . '.gif';
	$title = $num . 'Of' . $suit;

	while ($num <= 10) {
  if ($num = 10) {
  $num = 'Ace';
	}
	elseif ($num = 'Ace') {
  $num = 'Page';
	}
	elseif ($num = 'Page') {
  $num = 'Knight';
	}
	elseif ($num = 'Knight') {
  $num = 'Queen';
	}
	elseif ($num = 'Queen') {
  $num = 'King';
	}

	echo '$image $title';
	}
}
?>
much appreciated /biggrin.gif' class='bbc_emoticon' alt=':D' /> [/color]

while loops

Posted: Sat Sep 03, 2005 4:33 am
by Jim
you set "$num = 2;" but then never increase $num. Abit too rusty to remember how to do it but you need to increase / decrease $num at some point. Unless I just missed something /blum.gif' class='bbc_emoticon' alt=':P' />

while loops

Posted: Sat Sep 03, 2005 4:39 am
by Vercz
ah yes, good point

and for reference

Code: Select all

To Increase: $variable++;

To Decrease: $variable--;
[/color]

while loops

Posted: Sat Sep 03, 2005 8:32 am
by Josh
Never knew how to increase or decrease, thanks for posting that one Verczeh /blum.gif' class='bbc_emoticon' alt=':P' />

while loops

Posted: Sat Sep 03, 2005 7:00 pm
by Vercz
lol, no problem. this thread is helping everyone cept me /blum.gif' class='bbc_emoticon' alt=':P' />

updated code:

Code: Select all

<?php
if (!$_GET['suit']) {
	echo "please select a suit";
}
	elseif ($_GET['suit']) {
	$suit = $_GET['suit'];
	$num = 2;
	$image = 'images/' . $num . $suit . '.gif';
	$title = $num . 'Of' . $suit;
	
	while ($num <= 10) {
  echo '$image $title';
  $num++;
	}
}
?>
ive just trimmed a lot of stuff out of it, i figure ill get the main part working..then slowly add to it /smile.gif' class='bbc_emoticon' alt=':)' /> [/color]

while loops

Posted: Sun Sep 04, 2005 1:56 am
by Scott
What are you trying to get it to do? /unsure.gif' class='bbc_emoticon' alt=':unsure:' />

while loops

Posted: Sun Sep 04, 2005 2:07 am
by Vercz
guess i should of stated this before /blum.gif' class='bbc_emoticon' alt=':P' />

ok, ultimately i want it to go through and enter all the cards, with their descriptions and image links, in a mysql table..automatically.

at the moment, i just want it to display something like this:
say suit = cups

Code: Select all

images/2cups.gif 2Ofcups
images/3cups.gif 3Ofcups
images/4cups.gif 4Ofcups
images/5cups.gif 5Ofcups
images/6cups.gif 6Ofcups
images/7cups.gif 7Ofcups
images/8cups.gif 8Ofcups
images/9cups.gif 9Ofcups
images/10cups.gif 10Ofcups
follow? i hope so[/color]

while loops

Posted: Sun Sep 04, 2005 2:17 am
by Scott

Code: Select all

<?php
if (!$_GET['suit']) 
{
	echo "please select a suit";
}
elseif ($_GET['suit']) 
{
	$suit = $_GET['suit'];
	$file = $suit . '.txt';

	for($num = 2;$num <= 10;$num++) 
	{
  $image = 'images/' . $num . $suit . '.gif';
  $title = $num . 'Of' . $suit;
 	 echo $image." ".$title."<br />";
	}
}
?>
Don't say I'm not nice to you /blum.gif' class='bbc_emoticon' alt=':P' />

If you want an explination, feel free to ask.

while loops

Posted: Sun Sep 04, 2005 2:37 am
by Vercz
you're a legend *gently tosses you a bag of cookies* /biggrin.gif' class='bbc_emoticon' alt=':D' />

now ill start working on the rest of it /blum.gif' class='bbc_emoticon' alt=':P' />

while loops

Posted: Sun Sep 04, 2005 3:59 am
by Vercz
well, still working on the same file..so i may aswell keep the thread

(8:49:20p) &Vercz` | using php, is there a way to search for certain text in a .txt file, then use that line of text?

well...is there?

example... say i want to search for "pizza"

[quote name='file contents']toga greatness maximillian
pizza goes great with be- err, cordial
chicken is tasty
powerpuff girls! who said that?[/quote]

is it possible to have a script run a search on the file, then output "pizza goes great with be- err, cordial" ?

thankies anyway /smile.gif' class='bbc_emoticon' alt=':)' />

while loops

Posted: Sun Sep 04, 2005 4:16 am
by Scott

Code: Select all

<?php
$lines = file('test.txt');

foreach ($lines as $line_num => $line) {
   $linenum = (int) $line_num;
   $linenum++;
   $t = strpos($line, "pizza");
   if($t !== false) 
   {
    echo "Pizza found in line #<b>{$linenum}</b> : " .$line. "<br />\n";
   }
}

?>

/smile.gif' class='bbc_emoticon' alt=':)' />

Make sure you use the correct text file...

while loops

Posted: Sun Sep 04, 2005 3:39 pm
by Vercz
hmm...no wonder i didnt find that on my own /blum.gif' class='bbc_emoticon' alt=':P' />

anyone reckon they could explain that code to me? line by line is fine. kthx /bigwink.gif' class='bbc_emoticon' alt=';)' />

while loops

Posted: Wed Sep 07, 2005 6:47 am
by Vercz
ok..moving along, im trying something else now...

im trying to find a way to convert a number, into its text form... 1 = one, 2 = two...etc i need the numbers 2-10... any suggestions/help? ive been trying different things all night, and none of them have worked (might be cause im stressed/tired though *shrugs*)

thanks again /smile.gif' class='bbc_emoticon' alt=':)' />

while loops

Posted: Wed Sep 07, 2005 9:04 am
by Scott
This should work (added documentation to help):

Code: Select all

<?php
$short = 6; //wherever this comes from

//an array of all the full text (we'll leave the keys and deal with the offset later)
$full = array("Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");

//if we don't have an integer, we'll have a problem...run a check
if (is_int($short)) {
      //we've got an int, lets subtract 2 to count for the offset (Zero..one..two [we 

started at two])
      $text = $full[$short-2];
}
else {
      //its not an integer, so convert it into one
      $short = intval($short);
      //as above
      $text = $full[$short-2];
}
//output full text
echo $text;

?>
As for the other code you asked about, here it is with some comments added in:

Code: Select all

<?php
//basic function which reads the contents of the file into $lines as an array
$lines = file('test.txt');

//include this line if it helps to understand
//var_dump($lines);

//using a foreach (shouldn't...but it keeps it simple) to split the array into $line_num (the key) and $line (the content).
foreach ($lines as $line_num => $line) {
  //make sure $line_num is an int, then increment it
  $linenum = (int) $line_num;
  $linenum++;
  //quick check (as opposed to strstr()) which will return an int of the first position, or a boolean FALSE if not found
  $t = strpos($line, "pizza");
  //check to see whether or not it returned the boolean
  if($t !== false)
  {
   //if not, it returned an int, therefore the text is found in the file on $linenum
   echo "Pizza found in line #<b>{$linenum}</b> : " .$line. "<br />\n";
  }
}

?>
Let me know if that doesn't clear things up.

while loops

Posted: Mon Sep 26, 2005 8:02 am
by Vercz
woo resurrect /smile.gif' class='bbc_emoticon' alt=':)' /> ohh, ive taken this back to a version that works... my attempts at increments arent in there

Code: Select all

<?php

$tnum = "Five";
$file = "Cups.txt";
$desc = find_desc($tnum, $file);
echo $desc;
	 
function find_desc($text, $file)
{
$lines = file($file);

foreach ($lines as $line_num => $line) {
  $linenum = (int) $line_num;
  $linenum++;
  $t = strpos($line, $text);
  if($t !== false)
  {
   echo "Text found in line #<b> " .$linenum. " </b> : " .$line. "<br />\n";
  }
}
}

?>
thanks /biggrin.gif' class='bbc_emoticon' alt=':D' /> [/color]

while loops

Posted: Mon Sep 26, 2005 8:33 am
by Scott
You're probably trying to confuse it a bit much. Try this:

Code: Select all

<?php
 
 $tnum = "Five";
 $file = "Cups.txt";
 $found = FALSE;
 $desc = find_desc($tnum, $file);
 echo $desc;
 
 function find_desc($text, $file)
 {
 $lines = file($file);
 
 foreach ($lines as $line_num => $line) {
   $linenum = (int) $line_num;
   $linenum++;
   $t = strpos($line, $text);
 if ($found === TRUE)
 {
   echo $line;
 }
   if($t !== false)
   {
 $found = TRUE;
   }
 }
 }
 
 ?>

Not tested it, but it *should* work /unsure.gif' class='bbc_emoticon' alt=':unsure:' /> [/color]

while loops

Posted: Mon Sep 26, 2005 8:51 am
by Vercz
err...that code returns the entire contents of the file /blum.gif' class='bbc_emoticon' alt=':P' /> lol thanks anyway?

while loops

Posted: Mon Sep 26, 2005 9:30 am
by Scott
Ah, yeah. After echo $line; put $found = FALSE; That should stop it returning anything else.

while loops

Posted: Mon Sep 26, 2005 5:30 pm
by Vercz
...now it returns nothing :|

while loops

Posted: Tue Sep 27, 2005 4:51 am
by radar
Why are cups kept in a file? :\

while loops

Posted: Tue Sep 27, 2005 4:58 am
by Vercz
probably for the same reason that suits, swords and shields are...


that reason? nfi tbh /blum.gif' class='bbc_emoticon' alt=':P' />

while loops

Posted: Tue Sep 27, 2005 7:32 am
by Scott

Code: Select all

<?php
 
 $tnum = "Five";
 $file = "Cups.txt";
 $desc = find_desc($tnum, $file);
 echo $desc;
 
 function find_desc($text, $file)
 {
 $found = FALSE;
 $lines = file($file);
 
 foreach ($lines as $line_num => $line) {
 $linenum = (int) $line_num;
 $linenum++;
 $t = strpos($line, $text);
 if ($found === TRUE)
 {
 echo $line;
 return;
 }
 if($t !== false)
 {
 $found = TRUE;
 }
 }
 }
 
 ?>

All workie. At least, thats what I think you're trying to do.

while loops

Posted: Tue Sep 27, 2005 6:38 pm
by Vercz
...it still doesnt show anything /cry.gif' class='bbc_emoticon' alt=':(' />

while loops

Posted: Tue Sep 27, 2005 8:02 pm
by radar
Can I have a copy of the file please?

while loops

Posted: Tue Sep 27, 2005 8:04 pm
by Vercz
Cups.txt? sure... [url="http://vercz.killanet.net/tst/Cups.txt"]http://vercz.killanet.net/tst/Cups.txt[/url] =)

while loops

Posted: Wed Sep 28, 2005 12:19 am
by Scott
Ah...that makes sense. It is working, working too well. The script will print the line directly following the word "Five". Since your following line is a blank line...you see a blank line. Remove the empty lines and it'll work /smile.gif' class='bbc_emoticon' alt=':)' />

while loops

Posted: Wed Sep 28, 2005 12:51 am
by Vercz
...im not stupid /angel_not.gif' class='bbc_emoticon' alt='(not)' />


thanks scott /biggrin.gif' class='bbc_emoticon' alt=':D' /> and you too radar /biggrin.gif' class='bbc_emoticon' alt=':D' /> /good.gif' class='bbc_emoticon' alt='(Y)' /> /good.gif' class='bbc_emoticon' alt='(Y)' />