PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
glennnz
Forum Newbie
Posts: 9 Joined: Thu Aug 14, 2008 7:16 am
Post
by glennnz » Mon Aug 25, 2008 1:49 am
Hi
I'm using this code:
Code: Select all
<?php
$row = 1;
$no_purchased = $_POST['no_purchased'];
while($row <= $no_purchased)
{
$serial = "serial".$row;
$strings = array_fill(1, $no_purchased, $serial);
$row++;
}
foreach ($strings as $testcase)
{
if (ctype_digit($testcase))
{
$valid = "Yes";
}
else
{
$valid = "No";
}
}
if ($valid == "No")
{
print_r ($strings);
}
else
{
print_r ($strings);
}
?>
The array it generates is:
[1] => serial2
[2] => serial2
This is wrong. I want it to generate:
[1] => serial1
[2] => serial2
[3] => serial3
.......
Why doesn't it work??
Help...
Thanks
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Mon Aug 25, 2008 2:06 am
Why are u use array_fill()? Read documentation
http://php.net/
Replace 8 line code:
glennnz
Forum Newbie
Posts: 9 Joined: Thu Aug 14, 2008 7:16 am
Post
by glennnz » Mon Aug 25, 2008 2:13 am
Cool, it works with this:
Code: Select all
$strings[] = $_POST['serial'.$row];
but the array starts at [0], how can I get it to start at [1]?
Thanks
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Mon Aug 25, 2008 2:19 am
It's really simple task
Learn PHP!
glennnz
Forum Newbie
Posts: 9 Joined: Thu Aug 14, 2008 7:16 am
Post
by glennnz » Mon Aug 25, 2008 2:23 am
Great, thanks heaps!!
Now I need to get this part working:
Code: Select all
foreach ($strings as $testcase)
{
if (ctype_alnum($testcase))
{
$valid = "Yes";
}
else
{
$valid = "No";
}
}
I want it to loop through, and as soon as $valid = "No", exit the loop and keep $valid = "No"
At the moment it loops through the entire array, and $valid ends up as being for the last entry in the array.
G
Ziq
Forum Contributor
Posts: 194 Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh
Post
by Ziq » Mon Aug 25, 2008 2:31 am
If I have correctly understood
Code: Select all
foreach ($strings as $testcase)
{
if (ctype_alnum($testcase))
{
$valid = "Yes";
}
else
{
$valid = "No";
break;
}
}
or
Code: Select all
$valid = "Yes";
foreach ($strings as $testcase)
{
if (!ctype_alnum($testcase))
{
$valid = "No";
break;
}
}
Look at
continue; and
break; in PHP
glennnz
Forum Newbie
Posts: 9 Joined: Thu Aug 14, 2008 7:16 am
Post
by glennnz » Mon Aug 25, 2008 2:44 am
Brilliant!!
Thanks heaps!
Glenn