while loops

Support, Snippets & Projects

Moderators: Moderator, Global Moderator

Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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]<?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\';
}
}
?>[/code]

much appreciated /biggrin.gif\' class=\'bbc_emoticon\' alt=\':D\' /> [/color]
Last edited by Vercz on Sat Sep 03, 2005 11:08 am, edited 1 time in total.
Image

Image
Jim
Administrator
Administrator
Posts: 1324
Joined: Thu Jun 17, 2004 12:37 am

while loops

Post 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\' />
Love is all a matter of timing.

It's no good meeting the right person too soon or too late.

If I'd live in another time or place...

...my story might have had a very different ending.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]ah yes, good point

and for reference
[code]To Increase: $variable++;

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

Image
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

while loops

Post by Josh »

Never knew how to increase or decrease, thanks for posting that one Verczeh /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]lol, no problem. this thread is helping everyone cept me /blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />

updated code:

[code]<?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++;
}
}
?>[/code]

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]
Last edited by Vercz on Sun Sep 04, 2005 9:07 am, edited 1 time in total.
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

What are you trying to get it to do? /unsure.gif\' class=\'bbc_emoticon\' alt=\':unsure:\' />
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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]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[/code]

follow? i hope so[/color]
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

[code]<?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 />";
}
}
?>[/code]

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.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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\' />
[/color]
Image

Image
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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=\':)\' /> [/color]
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

[code]<?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";
  }
}

?>[/code]
/smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />

Make sure you use the correct text file...
Last edited by Scott on Sun Sep 04, 2005 11:16 am, edited 1 time in total.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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=\';)\' /> [/color]
Image

Image
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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=\':)\' /> [/color]
Image

Image
Scott
Administrator
Administrator
Posts: 1651
Joined: Sun Apr 25, 2004 1:08 pm

while loops

Post by Scott »

This should work (added documentation to help): [code]<?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;

?>[/code]

As for the other code you asked about, here it is with some comments added in: [code]<?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";
 }
}

?>[/code]

Let me know if that doesn\'t clear things up.
Vercz
Hero Member
Hero Member
Posts: 1915
Joined: Sun May 09, 2004 6:13 am

while loops

Post by Vercz »

[color=\"green\"]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]<?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";
}
}
}

?>[/code]

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

Image
Post Reply

Return to “PHP &amp; MySQL”