# A while statment inside of an if statement? (PHP)



## knight_47 (Mar 15, 2006)

Is that possible? To have a while statement _inside_ of an if statement. For example:


```
if ($_POST['Submit'])
{
$feedtitle = 1;
$feeddisc = 1;
$feedurl = 1;

echo "<?xml version=\"1.0\"?> <rss version=\"2.0\">	<channel>

{$_POST['sitename']}

{$_POST['siteurl']}</link>

<description>{$_POST['sitedescription']}</description>

<docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>www.knight47.com</generator> <pubDate>{$_POST['date']}</pubDate>";

while ($start < $number)
	{
echo "<item>{$_POST['title$feedtitle']}<description>{$_POST['description$feeddisc']}</description>{$_POST['url$feedurl']}</link><pubDate>{$_POST['date']}</pubDate></item>";
$feedtitle ++;
$feeddisc ++;
$feedurl ++;
	}
}
```
Because I don't think it's working, and I just want to make sure it's even possible...

Thanks.


----------



## Rockn (Jul 29, 2001)

Absolutely possible.

If statement=something do this else do this. Just nest the while statement inside of the side of the if statement you want it to show up. You might be better off using something like CASE. I can't recall what it is called in PHP.
Case condition1
do this
case condition 2
do this

Much more efficient than nested inside an if statement.

Edit:
It is called SWITCH in PHP


----------



## brendandonhu (Jul 8, 2002)

If that's your whole code, you need to set $start and $number somewhere, and increment $start in your loop.


----------



## knight_47 (Mar 15, 2006)

Thanks Rockn.

And no, that's not my whole code, I just wanted to make sure it was possible, and give a small example..

But any reason why it's not working?

Here is the whole code:

```
<?php

$number = $_POST['number'];
$start = 0;
$title = 1;
$disc = 1;
$url = 1;
echo "";
echo "   
 ";
echo "   
";
echo "

";

while ($start < $number)
{
echo "  
";
echo "  
";
echo "

";
$start ++;
$title ++;
$disc ++;
$url ++;
}
echo " ";

// let's create the xml code now

if ($_POST['Submit'])
{
$feedtitle = 1;
$feeddisc = 1;
$feedurl = 1;

echo "<?xml version=\"1.0\"?> <rss version=\"2.0\">	<channel>

{$_POST['sitename']}

{$_POST['siteurl']}</link>

<description>{$_POST['sitedescription']}</description>

<docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>www.knight47.com</generator> <pubDate>{$_POST['date']}</pubDate>";

while ($start < $number)
	{
echo "<item>{$_POST['title$feedtitle']}<description>{$_POST['description$feeddisc']}</description>{$_POST['url$feedurl']}</link><pubDate>{$_POST['date']}</pubDate></item>";
$feedtitle ++;
$feeddisc ++;
$feedurl ++;
	}
}
echo "</channel></rss>";
?>
```
link, www.knight47.com/rss_project

It's not made to generate the XML file yet, I'm just slowly putting it together, but it's not generating the rest of the code, only the site name, url, and description.

*Edit*: Wow that was blazing fast. I can't believe I missed that. Your honestly a genius Brendan, thanks for all your help.

I've yet to test it, because I have a few other things I need to get done first. But i'll let you know if it works.


----------



## brendandonhu (Jul 8, 2002)

[tsg=yourewelcome]yourewelcome[/tsg]


----------



## knight_47 (Mar 15, 2006)

Ok, I've added that, and it's still not working. I don't know why, I've told it to keep generating a specific line of code if condition is true, which it should be.

www.knight47.com/rss_project


----------



## brendandonhu (Jul 8, 2002)

You need to post your code and tell us what the problem is exactly.


----------



## knight_47 (Mar 15, 2006)

The codes almost the same as above, I just added a new variable, and told it to increment in the second while loop.


```
<?php

$number = $_POST['number'];
$starttwo = 0;
$start = 0;
$title = 1;
$disc = 1;
$url = 1;
echo "";
echo "   
 ";
echo "   
";
echo "

";

while ($start < $number)
{
echo "  
";
echo "  
";
echo "

";
$start ++;
$title ++;
$disc ++;
$url ++;
}
echo " ";

// let's create the xml code now

if ($_POST['Submit'])
{
$feedtitle = 1;
$feeddisc = 1;
$feedurl = 1;

echo "<?xml version=\"1.0\"?> <rss version=\"2.0\">	<channel>

{$_POST['sitename']}

{$_POST['siteurl']}</link>

<description>{$_POST['sitedescription']}</description>

<docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>www.knight47.com</generator> <pubDate>{$_POST['date']}</pubDate>";

while ($starttwo < $number)
	{
echo "<item>{$_POST['title$feedtitle']}<description>{$_POST['description$feeddisc']}</description>{$_POST['url$feedurl']}</link><pubDate>{$_POST['date']}</pubDate></item>";
$starttwo ++;
$feedtitle ++;
$feeddisc ++;
$feedurl ++;
	}
}
echo "</channel></rss>";
?>
```
It's not giving any error, but it's not outputting this code

```
echo "<item>{$_POST['title$feedtitle']}<description>{$_POST['description$feeddisc']}</description>{$_POST['url$feedurl']}</link><pubDate>{$_POST['date']}</pubDate></item>";
```
 . Even though it should because as long as $starttwo is smaller than $number, it should. But for some reason it's not outputting the code. I hope that made sense.


----------



## brendandonhu (Jul 8, 2002)

$_POST['number'] isn't set. You've only submitted it on the first form, it's empty when you submit the 2nd form.


----------



## knight_47 (Mar 15, 2006)

brendandonhu said:


> $_POST['number'] isn't set. You've only submitted it on the first form, it's empty when you submit the 2nd form.


Wow, you're so freaking smart bro. hehe, I'll try it in a bit.

Thanks a lot.


----------



## knight_47 (Mar 15, 2006)

How can I store $number, so it can be used on another page?


----------



## brendandonhu (Jul 8, 2002)

echo it into a "hidden" input field.


----------



## knight_47 (Mar 15, 2006)

Smart idea. But how can the PHP file read a number in a hidden field?


----------



## brendandonhu (Jul 8, 2002)

$_POST['number']


----------



## knight_47 (Mar 15, 2006)

brendandonhu said:


> $_POST['number']


oh oh oh ok! I think I got it, thanks! :up:


----------



## knight_47 (Mar 15, 2006)

Ok, so I set up a new field for $_POST['number'] called $feeds for generating the items in the rss feed.

but for some reason, the while loop in the if statment is still not working.


```
<?php

$number = $_POST['number'];
$starttwo = 0;
$start = 0;
$title = 1;
$disc = 1;
$url = 1;
$feeds = $_POST['feeds'];

echo "
";
echo "Items in this Feed:  
";
echo "   
 ";
echo "   
";
echo "

";

while ($start < $number)
{
echo "  
";
echo "  
";
echo "

";
$start ++;
$title ++;
$disc ++;
$url ++;
}
echo " ";

// let's create the xml code now

if ($_POST['Submit'])
{
$feedtitle = 1;
$feeddisc = 1;
$feedurl = 1;

echo "<?xml version=\"1.0\"?> <rss version=\"2.0\">	<channel>

{$_POST['sitename']}

{$_POST['siteurl']}</link>

<description>{$_POST['sitedescription']}</description>

<docs>http://blogs.law.harvard.edu/tech/rss</docs> <generator>www.knight47.com</generator> <pubDate>{$_POST['date']}</pubDate>";

while ($starttwo < $feeds)
	{
echo "<item>{$_POST['title$feedtitle']}<description>{$_POST['description$feeddisc']}</description>{$_POST['url$feedurl']}</link><pubDate>{$_POST['date']}</pubDate></item>";
$starttwo ++;
$feedtitle ++;
$feeddisc ++;
$feedurl ++;
	}
}
echo "</channel></rss>";
?>
```
link: www.knight47.com/rss_project

do you know why the second while loop isn't generating any code?


----------



## brendandonhu (Jul 8, 2002)

The loop is running. You need to echo the right variable for the values in the XML.
You have this: $_POST['title$feedtitle']
Keep in mind, PHP doesn't parse variables in single quotes.


----------



## knight_47 (Mar 15, 2006)

hm, so I can't have a variable inside a $_POST['']?


----------



## knight_47 (Mar 15, 2006)

that said, should it be something like this:


```
$title = $_POST['title'];
```
and then:


```
$title$feedtitle
```


----------



## brendandonhu (Jul 8, 2002)

It would be $_POST['title' . $feedtitle]
But your loop still isn't right...think about what the variables will be equal to each time it goes through. You don't need separate variables for $starttwo, $feedtitle, $feeddisc, and $feedurl.


----------



## brendandonhu (Jul 8, 2002)

Here's a start

```
<?php
foreach($_POST as $key => $value)
{
	$_POST[$key] = htmlspecialchars($value);
}

switch($_POST['submit'])
{
	case 'Next':
	?>

	Items in Feed: size=1 />

	<?php
	for($i = 1; $i <= $_POST['feeds']; $i++)
	{
		echo '
';
		echo '
';
		echo '

';
	}
	echo '';
	break;

	case 'Generate':
	header('Content-Type: text/xml');
	echo '<?xml version="1.0"?>';
	?>
	<rss version="2.0"> 
	<channel>
		<?php echo $_POST['sitename'] ?>
		<?php echo $_POST['siteurl'] ?></link>
		<description><?php echo $_POST['sitedescription'] ?></description>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<generator>www.knight47.com</generator>
		<pubDate><?php echo $_POST['date'] ?></pubDate>
		<?php
		for($i = 1; $i <= $_POST['feeds']; $i++)
		{
		?>
			<item>
				<?php echo $_POST['title' . $i] ?>
				<description><?php echo $_POST['description' . $i] ?></description>
				<?php echo $_POST['url' . $i] ?></link>
				<pubDate><?php echo $_POST['date' . $i] ?></pubDate>
			</item>
		<?php
		}
	echo '</channel></rss>';
	break;

	default:
	?>

RSS Feed Generator

	Items Required:

currently in progress, code not done...

	<?php
}
?>
```
I don't see a "date" field in your form though.


----------



## knight_47 (Mar 15, 2006)

What does a . do in PHP?

and I still don't understand what's wrong with the loop.

And thanks for the code, I took a quick glance at it, and it's still too advanced for me! lol i'm still a beginner, and don't really know much. I didn't know you could have a php file _inside_ of a php script, that's cool.

And yea I haven't added the date yet, i'm just trying to get the gist of the script first.


----------



## brendandonhu (Jul 8, 2002)

. is the concatenation operator.


----------



## knight_47 (Mar 15, 2006)

ok thanks.

which loop did you say wasn't setup correct, the first? second? or both?!


----------



## brendandonhu (Jul 8, 2002)

Both of them. You only need to increment 1 variable in a loop.


----------



## knight_47 (Mar 15, 2006)

hm, because the first loop seems to be working fine, but it's the second that's giving me problems. let me see if I can figure this out on my own. thanks.


----------

